我正在尝试通过applet与servlet
和jsp
进行通信。当单击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
页面。那是为什么?
答案 0 :(得分:0)
如果我正确找到你,你需要从applet重定向到servlet然后转发JSP页面吗?
after your servlet response
.....redirect to jsp from applet.
AppletContext appletContext = getAppletContext();
appletContext.showDocument(new URL(getDocumentBase(), "yourJsp.jsp"));