私はソフトウェアを作成していますが、このソフトウェアの一部には、Javaデスクトップクライアントアプリとサーブレット間の通信が含まれています。サーブレットはデータベースからテキストデータを引き出し、それを1つのout.println()ステートメントで書き出します。問題は、サーブレットによって書き込まれたテキストデータが多い場合は常に、テキストの一部がクライアントに到達しないことです。これは、サーブレットでcontentlength response.setContentLength(text.length())を設定しているにもかかわらずです。何が起こっているのでしょうか?これはサーブレットです:
package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;'
import javax.sql.DataSource;
/**
*
* @author GBEMIRO
*/
public class MessageRetrieverServlet extends HttpServlet {
DataSource pool; // Database connection pool
private HttpSession session;
@Override
public void init() throws ServletException {
try {
// Create a JNDI Initial context to be able to lookup the DataSource
InitialContext ctx = new InitialContext();
// Lookup the DataSource, which will be backed by a pool
// that the application server provides.
pool = (DataSource) ctx.lookup("java:comp/env/jdbc/TestDB");
if (pool == null) {
throw new ServletException("Unknown DataSource 'jdbc/TestDB'");
}
} catch (NamingException ex) {
ex.printStackTrace();
}
}//end method init
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
session = request.getSession();
Connection conn = null;
try {
String email = request.getParameter("email");
conn = pool.getConnection();
PreparedStatement pstmt = conn.prepareStatement("SELECT MSG FROM POSTS_TABLE WHERE EMAIL = ? ");
pstmt.setString(1, email);
ResultSet rs = pstmt.executeQuery();
rs.next();
//The huge text
Clob clob = rs.getClob("MSG");
/**
* The text retrieved into msg is very large, but I checked with
* System.out.println(msg) statements it is complete up to this
* point, no part of it is lost. So the retrieval from the database
* is always successful.
*
*/
String msg = clob.getSubString(0, (int) clob.length());
out.print(msg);
}//end try
catch (SQLException exception) {
String errorMsg = "error3";
exception.printStackTrace();
out.print(errorMsg);
} catch (NumberFormatException numberFormatException) {
String errorMsg = "error4";
numberFormatException.printStackTrace();
out.print(errorMsg);
}
try {
} finally {
out.close();
try {
if (conn != null) {
conn.close();
} // return to pool
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}//end method
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Now this is the receiving method on the client where the problem arises:
public void retrieveMessages(){
try {
URL url = new URL("http://"+server_name_or_ip+":8080/SecureChatEngine/MessageRetrieverServlet");
HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();
urlCon.setDoInput(true);
urlCon.setDoOutput(true);
urlCon.setRequestMethod("POST");
urlCon.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
PrintWriter out = new PrintWriter( new OutputStreamWriter(
urlCon.getOutputStream( ), "8859_1"), true );
StringBuilder sb = new StringBuilder("");
sb.append(URLEncoder.encode("email","UTF-8"));
sb.append("=");
sb.append(URLEncoder.encode("gbenroscience@yahoo.com","UTF-8"));
String formData = sb.toString();
out.print(formData);
out.flush();
int contentLength = urlCon.getContentLength();
byte[] raw = new byte[contentLength];
int length = urlCon.getInputStream().read(raw);
System.out.println("len = "+length);
out.close();
/**
* The message retrieved here is incomplete..a truncated version of what is coming from the server, whereas
* it should be the full version.
*/
String serverMsg = new String(raw, 0, length);
}
catch(Exception e){
e.printStackTrace();
}
}//end method