【问题标题】:why don't i see a new jsp page when i click the fire button in the applet?为什么当我单击小程序中的启动按钮时看不到新的 jsp 页面?
【发布时间】:2012-07-07 02:17:18
【问题描述】:

我正在尝试通过小程序与servletjsp 通信。当单击小程序中的按钮时,请求被触发到 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 页面。这是为什么呢?

【问题讨论】:

  • 有一种可能是 jsp_1.jsp 与您当前的请求无关。如果您不提供 JSP 的“绝对路径”,服务器会尝试在 request.requestDispatcher 上查找相对于当前请求 URL 路径的 jsp_1.jsp。

标签: java jsp servlets applet httpconnection


【解决方案1】:

如果我的理解正确,您需要从小程序重定向到 servlet,然后转发 JSP 页面?

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

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

【讨论】:

    猜你喜欢
    • 2020-06-13
    • 2015-01-05
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    相关资源
    最近更新 更多