预览加载中,请您耐心等待几秒...
1/4
2/4
3/4
4/4

在线预览结束,喜欢就下载吧,查找使用更方便

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

1.重定向 在Servlet中的接口HttpServletRequest提供了sendRedirect()方法用于生成302相应码和Location响应头,从而通知客户端去重新访问Location响应头指定的URL,其定义如下: publicvoidsendRedirect(Stringlocation)throwsIOException; 对于sendRedirect()方法不仅可以重定向到当前应用程序中的其他资源,也可重定向到一个容器中的其他应用程序中的资源。 eg: //Servlet1.java packagecom.lkh.redrect; importjava.io.IOException; importjavax.servlet.RequestDispatcher; importjavax.servlet.ServletException; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; publicclassServlet1extendsHttpServlet{ @Override protectedvoiddoGet(HttpServletRequestrequest, HttpServletResponseresponse)throwsServletException,IOException{ //TODOAuto-generatedmethodstub this.doPost(request,response); } @Override protectedvoiddoPost(HttpServletRequestrequest, HttpServletResponseresponse)throwsServletException,IOException{ //TODOAuto-generatedmethodstub response.sendRedirect("Servlet2");//重定向到指定路径的资源 //RequestDispatcherdis=request.getRequestDispatcher("Servlet2"); //dis.forward(request,response); } } //Servlet2.java packagecom.lkh.redrect; importjava.io.IOException; importjava.io.PrintWriter; importjavax.servlet.ServletException; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; publicclassServlet2extendsHttpServlet{ @Override protectedvoiddoGet(HttpServletRequestrequest, HttpServletResponseresponse)throwsServletException,IOException{ //TODOAuto-generatedmethodstub this.doPost(request,response); } @Override protectedvoiddoPost(HttpServletRequestrequest, HttpServletResponseresponse)throwsServletException,IOException{ //TODOAuto-generatedmethodstub response.setContentType("text/html;charset=utf-8"); PrintWriterout=response.getWriter(); out.println("<html>"); out.println("<head><title>servlet2</title></head>"); out.println("<body>"); out.println("从Servlet2中获取请求参数name的值:"); out.println(request.getParameter("name")); out.println("</body>"); out.println("</