3、重定向:传参,访问顺序(doGet还是doPost)
index.jsp:
<a href="servlet/RedirOneServlet">重定向时,两个servlet都输出信息的问题、传参问题、访问顺序问题</a><br /> <form action="servlet/RedirOneServlet" method="post"> <input type="text" name="name" /><br /> <input type="submit"value="提交" /> </form>
web.xml:
<servlet> <servlet-name>RedirOneServlet</servlet-name> <servlet-class>cn.hncu.servlet.RedirOneServlet</servlet-class> </servlet> <servlet> <servlet-name>RedirTwoServlet</servlet-name> <servlet-class>cn.hncu.servlet.RedirTwoServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>RedirOneServlet</servlet-name> <url-pattern>/servlet/RedirOneServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>RedirTwoServlet</servlet-name> <url-pattern>/servlet/RedirTwoServlet</url-pattern> </servlet-mapping>
RedirOneServlet.java:
package cn.hncu.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class RedirOneServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.print("11111111111");//这是一个无效的输出,我们看不到!! response.sendRedirect("/myServletDemo4/servlet/RedirTwoServlet"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.print("11111111111");//这是一个无效的输出,我们看不到!! //out.flush(); //如果刷了,后面就不能故去了。 String name = request.getParameter("name"); System.out.println("11111para-name:"+name);//这个也只有这里能接收到,如果想要传过去,得通过 地址栏+?+name= 的方式,如最后一行 request.setAttribute("age", 23);//这一句是没用的,对方收不到! //response.sendRedirect("/myServletDemo4/servlet/RedirTwoServlet"); response.sendRedirect("/myServletDemo4/servlet/RedirTwoServlet?name="+name+"&age=23"); } }
RedirTwoServlet.java:
package cn.hncu.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class RedirTwoServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.print("doget........22222222<br/>"); String name = request.getParameter("name"); out.print("2222para-name:"+name); String age = request.getParameter("age"); out.print("<br/>2222para-age:"+age); //得到地址栏?号后的属性值 Integer age2 = (Integer) request.getAttribute("age"); out.print("<br>attr-age:"+age2);//null. web.xml 中没有设置 } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.print("doget........22222222<br/>"); } }
RedirTwoServlet.java:
package cn.hncu.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class RedirTwoServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.print("doget........22222222<br/>"); String name = request.getParameter("name"); out.print("2222para-name:"+name); String age = request.getParameter("age"); out.print("<br/>2222para-age:"+age); //得到地址栏?号后的属性值 Integer age2 = (Integer) request.getAttribute("age"); out.print("<br>attr-age:"+age2);//null. web.xml 中没有设置 } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.print("doget........22222222<br/>"); } }
小总结:
两个servlet都输出信息的问题,这里和转发的情况其实可以理解是相同的,你第一个servlet如果out调用flush(),一样的,不会再重定向到后面的第二个servlet中去了。
1)无论第一个是doGet还是doPost,第二个走的都是doGet
(可以理解成:通过地址栏访问的)
2)传参:第二个servlet中的request和第一个是完全不同的对象,因此无法通过:request.setAttribute()和request.getAttribute() 实现传参。
3)第二个servlet中是无法通过request.getParameter()的方式获取页面提交的参数数据
4)重定向方式下,如果要进行传参,可采用:在地址栏的url后添加类似如下的格式传参:?name=Jack&age=23
注意,采用地址栏url传参的方式,在浏览器地址栏是能够看到的,因此要注意隐私(安全)问题—如果有隐私参数,那么要加密!!!
5)转发只能在站内进行(路径默认在项目内,即路径不用带项目名),重定向可以在站外进行(如果是站外路径要带“http://”开头,站内路径要带项目名)。如果非要项目之间(站外)进行跳转,那么必须要选择重定向。
4、请求包含:传参,访问顺序(doGet还是doPost)
index.jsp:
<a href="servlet/IncludeOneServlet">请求包含时,两个servlet都输出信息的问题、传参问题、访问顺序问题</a><br /> <form action="servlet/IncludeOneServlet" method="post"> <input type="text" name="name" /><br /> <input type="submit"value="提交" /> </form>
web.xml:
<servlet> <servlet-name>IncludeOneServlet</servlet-name> <servlet-class>cn.hncu.servlet.IncludeOneServlet</servlet-class> </servlet> <servlet> <servlet-name>IncludeTwoServlet</servlet-name> <servlet-class>cn.hncu.servlet.IncludeTwoServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>IncludeOneServlet</servlet-name> <url-pattern>/servlet/IncludeOneServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>IncludeTwoServlet</servlet-name> <url-pattern>/servlet/IncludeTwoServlet</url-pattern> </servlet-mapping>
IncludeOneServlet.java:
package cn.hncu.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class IncludeOneServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.print("<html><body>"); out.print("Include111111..doGet..."); //传参,和转发时一样的! request.setAttribute("age", 25); RequestDispatcher rd = request.getRequestDispatcher("/servlet/IncludeTwoServlet"); rd.include(request, response); //它的机制可理解成函数调用。相当于把第二个servlet的doGet()方法中的代码拷到这里来运行。 //后面的还可以继续运行!!! out.print("<br/>Include11111...daGet...请求包含之后!"); out.print("</body></html>"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.print("<html><body>"); out.print("Include111111..doPost..."); //传参,和转发时一样的! request.setAttribute("age", 25); RequestDispatcher rd = request.getRequestDispatcher("/servlet/IncludeTwoServlet"); rd.include(request, response); //它的机制可理解成函数调用。相当于把第二个servlet的doPost()方法中的代码拷到这里来运行。 //后面的还可以继续运行!!! out.print("<br/>Include11111...doPost...请求包含之后!"); out.print("</body></html>"); } }
IncludeTwoServlet.java
package cn.hncu.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class IncludeTwoServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); //因为上一个servlet已经设过,这里可以不设 PrintWriter out = response.getWriter(); //out.print("<html><body>"); out.print("<br/>Include222222中文...doGet"); //接参 Integer age = (Integer) request.getAttribute("age"); out.print("<br/>Include222222...doGet...:"+age); //下面这两句会导致无穷递归 //RequestDispatcher rd = request.getRequestDispatcher("/servlet/IncludeOneServlet"); //rd.include(request, response); //out.print("</body></html>"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); //因为上一个servlet已经设过,这里可以不设 PrintWriter out = response.getWriter(); // out.print("<br/>Include222222中文...doPost"); //接参 Integer age = (Integer) request.getAttribute("age"); out.print("<br/>Include222222...doPost...:"+age); } }
小总结:
1)两个servlet的输出都有效!—中途调用flush,流不会关闭,后续的输出都会执行。如果在第一个servlet中执行了out.close(),那么后续的输出(无论是第一还是第二个servlet)都不会执行,但程序不会出异常!!!!
2)第一个是doGet,第二个走的也是doGet
3)第一个是doPost,第二个走的也是doPost
4)对于请求包含,第二个servlet在执行完之后,还会回到第一个servlet的rd.include()这行代码之后。
5)传参方面,和转发是完全一样的,因为都是共享同一个request和同一个response.
6)页面输出时,注意html标签不要输出冲突,如:在第一个servlet中输出了“<html><body>”和“</body></html>”,同时在第二个servlet中也输出这些标记。这样会出现html标记嵌套冲突!!
※重定向和转发:跳转之后不会回到原来的那个servlet中。
而“请求转发”在跳转之后会回到原来servlet的“rd.include()”这句代码之后继续执行。
演示:
可以看到,客户端只请求了一次!!!转发是请求两次的。
如果2个服务器都向客户端输出了<html><body> </body></html>
会出现: