在servlet和Jsp之间发布消息

时间:2015-12-23 09:47:08

标签: java jsp servlets

请问如何从servlet向jsp文件发送post消息? servlet和jsp位于different servers中。 在Java - sending HTTP parameters via POST method easily他们解释了如何发送Post消息,但是,我如何在JSP文件中收到此消息?

我在servlet中尝试了这段代码:

URL gwtServlet = null;
            gwtServlet = new URL("http://localhost:8080/connection/result.jsp");
            HttpURLConnection servletConnection = (HttpURLConnection) gwtServlet.openConnection();
            servletConnection.setRequestMethod("POST");
            servletConnection.setDoOutput(true);
            ObjectOutputStream objOut = new ObjectOutputStream(servletConnection.getOutputStream());
            objOut.writeObject("Test");
            objOut.flush();
            objOut.close();
            InputStream res = servletConnection.getInputStream();
            servletConnection.setInstanceFollowRedirects(true);

并在JSP文件中:

    <%
ObjectInputStream objIn = new ObjectInputStream(request.getInputStream()); //exception in this line
String rep = (String) objIn.readObject();
%>
Result:
<textarea rows="4" cols="50">
<%=rep %>
</textarea>

我遇到了这个例外:

    message java.io.EOFException

description Le serveur a rencontré une erreur interne qui l''a empêché de satisfaire la requête.

exception

org.apache.jasper.JasperException: java.io.EOFException
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:460)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
cause mère

java.io.EOFException
    java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2325)
    java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2794)
    java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:801)
    java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
    org.apache.jsp.result_jsp._jspService(result_jsp.java:75)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)

0 个答案:

没有答案