需求:
我们从LoginServlet页面跳转到PrintServlet页面
目录结构:
页面的跳转,分为3种:
第一:sendRedirect表示跳转到其他页面。
–response.sendRedirect(“url”);
response.sendRedirect("http://www.goxuexi.com");
用法实例:
在MyServlet中-doGet方法中添加如下代码:
/** * 1.重定向到其他页面 * sendRedirect */ resp.sendRedirect("Http://www.goxuexi.com");当我们在浏览器中访问如下代码的时候,我们可以看到浏览器中的域名是不变的,但是里面直接访问的是www.goxuexi.com的内容。
MyServlet.java
package com.goxuexi.demo; 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 MyServlet extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { /** * 1.重定向到其他页面 * sendRedirect */ resp.sendRedirect("Http://www.goxuexi.com"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
第二种方法:请求转发(forward),当前域,不包含
–request.getRequestDispatcher(“url”).forward(request,response);
request.getRequestDispatcher("new_page.html").forward(request, response);
用法用例:
LoginServlet.java
package com.goxuexi.demo; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class LoginServlet */ //@WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LoginServlet() { super(); System.out.println("LoginServlet......."); } @Override public void init() throws ServletException { // TODO Auto-generated method stub super.init(); System.out.println("init......."); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub // System.out.println("do Get..........."); doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub // System.out.println("do Post................"); /** * 请求重定向 */ request.getRequestDispatcher("PrintServlet").forward(request, response); } @Override public void destroy() { // TODO Auto-generated method stub super.destroy(); System.out.println("destory........"); } }
package com.goxuexi.demo; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class PrintServlet */ @WebServlet("/PrintServlet") public class PrintServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public PrintServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub PrintWriter out = response.getWriter(); /** * 只是简单输出一个html文本 */ // out.print("<h1> Hello Servlet </h1>"); /** * 如果有更复杂的,请使用下面的。 */ StringBuilder sb = new StringBuilder(); sb.append("<html>"); sb.append("<head>"); sb.append("<title>"); sb.append(" Test Servlet Print "); sb.append("</title>"); sb.append("</head>"); sb.append("<body>"); sb.append("<ul>"); sb.append("<li>"); sb.append("Android"); sb.append("</li>"); sb.append("<li>"); sb.append("IOS"); sb.append("</li>"); sb.append("<li>"); sb.append("PHP"); sb.append("</li>"); sb.append("</ul>"); sb.append("</body>"); sb.append("</html>"); out.print(sb.toString()); } }
就会实现登陆页面,跳转到PrintServlet页面。
第三种方法:请求包含,当前域,包含
–request.getRequestDispatcher(“url”).include(request,response);
request.getRequestDispatcher("new_page.html").include(request, response);
当我们把上面LoginServle.java中doPose方法中的内容修改为:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub // System.out.println("do Post................"); /** * 请求重定向 */ PrintWriter out = response.getWriter(); out.print("<h1> Hello Servlet </h1>"); request.getRequestDispatcher("PrintServlet").include(request, response); }我们会发现页面包含有Hello Servlet。如果是forward方法,就不会被包含Hello Servlet。
另外补充一点:
获得Servlet初始化参数在doGet方法中。
/** * 获取web.xml中context-param的值 */ String value1 = getServletContext().getInitParameter("goxuexi"); System.out.println(value1);
<context-param> <param-name>goxuexi</param-name> <param-value>Http://goxuexi.com</param-value> </context-param>