我正在尝试通过小程序与servlet
和jsp
进行通信。当单击小程序中的按钮时,请求被触发到 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
页面。 这是为什么呢?
没看错,你需要从小程序重定向到 servlet,然后转发 JSP 页面?
after your servlet response
.....redirect to jsp from applet.
AppletContext appletContext = getAppletContext();
appletContext.showDocument(new URL(getDocumentBase(), "yourJsp.jsp"));