Servlet 处理 applet 通信和 http 请求
我正在构建一个解决方案,其中 tomcat6 上的 servlet 通过提供 jsp 网页来处理来自另一台计算机的输入。我还希望同一个 servlet 与同一台计算机上的小程序进行通信,以显示一些结果并执行一些任务。
我可以使用 URLConnection 从 applet 连接到 servlet,但是当从 servlet 接收消息时,我从 applet 收到“无效的流标头”异常消息。
有什么想法吗?
编辑:错误消息:
java.io.StreamCorruptedException:无效的流标头:0A0A0A3C 在 java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783) 在 java.io.ObjectInputStream.
Applet 代码:
private URLConnection getServletConnection()
throws MalformedURLException, IOException {
// Open the servlet connection
URL urlServlet = new URL("http://localhost:8080/Servlet");
connection = urlServlet.openConnection();
// Config
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches (false);
connection.setDefaultUseCaches (false);
connection.setRequestProperty(
"Content-Type",
"application/x-java-serialized-object");
return connection;
}
private void onSendData() {
try {
// get input data for sending
String input = "Applet string heading for servlet";
// send data to the servlet
connection = getServletConnection();
OutputStream outstream = connection.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject(input);
oos.flush();
oos.close()
// receive result from servlet
InputStream instr = connection.getInputStream();
ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
String result = (String) inputFromServlet.readObject();
inputFromServlet.close();
instr.close();
// show result
textField.setText(result);
} catch (java.net.MalformedURLException mue) {
textField.setText("Invalid serlvetUrl, error: " + mue.getMessage());
} catch (java.io.IOException ioe) {
textField.setText("Couldn't open a URLConnection, error: " + ioe.getMessage());
} catch (Exception e) {
textField.setText("Exception caught, error: " + e.getMessage());
}
}
Servlet 代码:
public class Servlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=ISO-8859-1");
PrintWriter out = response.getWriter();
String defect = request.getParameter("defect").toString();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet at " + request.getContextPath() + "</h1>");
out.println("<p>Defect: " + defect + "</p>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
response.setContentType("application/x-java-serialized-object");
InputStream in = request.getInputStream();
ObjectInputStream inputFromApplet = new ObjectInputStream(in);
String servletText = "Text from Servlet";
// echo it to the applet
OutputStream outstr = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstr);
oos.writeObject(servletText);
oos.flush();
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
processRequest(request, response);
}
I'm building a solution where a servlet on tomcat6 handles input from another machine by providing a jsp web page. I'd also like the same servlet to communicate with an applet on the same machine to show some results and perform a few tasks.
I'm able to connect from the applet to the servlet using a URLConnection but when receving a message from the servlet I get a "invalid stream header" exception message from the applet.
Any ideas?
edit: Error message:
java.io.StreamCorruptedException: invalid stream header: 0A0A0A3C
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at applet.viewApplet.onSendData(viewApplet.java:126)
Applet code:
private URLConnection getServletConnection()
throws MalformedURLException, IOException {
// Open the servlet connection
URL urlServlet = new URL("http://localhost:8080/Servlet");
connection = urlServlet.openConnection();
// Config
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches (false);
connection.setDefaultUseCaches (false);
connection.setRequestProperty(
"Content-Type",
"application/x-java-serialized-object");
return connection;
}
private void onSendData() {
try {
// get input data for sending
String input = "Applet string heading for servlet";
// send data to the servlet
connection = getServletConnection();
OutputStream outstream = connection.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject(input);
oos.flush();
oos.close()
// receive result from servlet
InputStream instr = connection.getInputStream();
ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
String result = (String) inputFromServlet.readObject();
inputFromServlet.close();
instr.close();
// show result
textField.setText(result);
} catch (java.net.MalformedURLException mue) {
textField.setText("Invalid serlvetUrl, error: " + mue.getMessage());
} catch (java.io.IOException ioe) {
textField.setText("Couldn't open a URLConnection, error: " + ioe.getMessage());
} catch (Exception e) {
textField.setText("Exception caught, error: " + e.getMessage());
}
}
Servlet code:
public class Servlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=ISO-8859-1");
PrintWriter out = response.getWriter();
String defect = request.getParameter("defect").toString();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet at " + request.getContextPath() + "</h1>");
out.println("<p>Defect: " + defect + "</p>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
response.setContentType("application/x-java-serialized-object");
InputStream in = request.getInputStream();
ObjectInputStream inputFromApplet = new ObjectInputStream(in);
String servletText = "Text from Servlet";
// echo it to the applet
OutputStream outstr = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstr);
oos.writeObject(servletText);
oos.flush();
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
processRequest(request, response);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您尝试使用
ObjectInputStream
反序列化 servlet 的基于文本的响应正文,而它根本没有序列化,并且您应该为此使用Reader
。长话短说:如何触发和使用
URLConnection
? 处理 HTTP 请求。That's because you're attempting to deserialize the textbased response body of the servlet using
ObjectInputStream
while it isn't serialized at all and you should be using aReader
for this.Long story short: How to fire and handle HTTP requests using
URLConnection
?.