当我点击小程序中的开火按钮时,为什么我看不到新的jsp页面?

时间:2012-07-06 15:36:09

标签: java jsp servlets applet httpconnection

我正在尝试通过applet与servletjsp进行通信。当单击applet中的按钮时,会向servlet发出请求,然后我尝试从该servlet转发到jsp页面。虽然请求成功地发送到servlet的doGet方法,但我既没有在浏览器中看到servlet页面,也没有看到jsp页面。这是为什么 ?我错过了什么?

小程序按钮点击代码:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    System.out.println("pressed the button !");
    try {
          URLConnection connection = new URL("http://localhost:8084/poll/servlet_1").openConnection();
          connection.setRequestProperty("Accept-Charset", "UTF-8");
          InputStream response  = connection.getInputStream();
          connection.connect();
    }catch(Exception exc) {
        exc.printStackTrace();
    }
}

servlet代码:

@Override
public void doGet(HttpServletRequest request,HttpServletResponse response) throws 
        ServletException,IOException {
    System.out.println("---inside the doGet method of servlet----");
    PrintWriter writer = response.getWriter();
    response.setContentType("text/plain");
    writer.println("You just landed on a servlet from an applet !");
    RequestDispatcher rd = request.getRequestDispatcher("jsp_1.jsp");
    rd.forward(request, response);
}

我在服务器日志中看到的是消息:---inside the doGet method of servlet----

当我触发事件时,doGet方法中的第一个语句被打印,但请求不会转发到jsp页面。那是为什么?

1 个答案:

答案 0 :(得分:0)

如果我正确找到你,你需要从applet重定向到servlet然后转发JSP页面吗?

after your servlet response
.....redirect to jsp from applet.

 AppletContext appletContext = getAppletContext();
 appletContext.showDocument(new URL(getDocumentBase(), "yourJsp.jsp"));