第4章:作为Servlet:请求和响应/4.5 重定向和请求分派

  • 重定向

  1. 在浏览器端进行重定向:可以是HTML,也可以是JSP
    1. 代码:
      package web;
      
      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 Redirect extends HttpServlet {
      
      	/**
      	 * Constructor of the object.
      	 */
      	public Redirect() {
      		super();
      	}
      
      	/**
      	 * Destruction of the servlet. <br>
      	 */
      	public void destroy() {
      		super.destroy(); // Just puts "destroy" string in log
      		// Put your code here
      	}
      
      	/**
      	 * The doGet method of the servlet. <br>
      	 *
      	 * This method is called when a form has its tag value method equals to get.
      	 * 
      	 * @param request the request send by the client to the server
      	 * @param response the response send by the server to the client
      	 * @throws ServletException if an error occurred
      	 * @throws IOException if an error occurred
      	 */
      	public void doGet(HttpServletRequest request, HttpServletResponse response)
      			throws ServletException, IOException {
      
      		response.setContentType("text/html");
      		PrintWriter out = response.getWriter();
      		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
      		out.println("<HTML>");
      		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
      		out.println("  <BODY>");
      		out.print("    This is ");
      		out.print(this.getClass());
      		out.println(", using the GET method");
      		out.println("  </BODY>");
      		out.println("</HTML>");
      		out.flush();
      		out.close();
      	}
      
      	/**
      	 * The doPost method of the servlet. <br>
      	 *
      	 * This method is called when a form has its tag value method equals to post.
      	 * 
      	 * @param request the request send by the client to the server
      	 * @param response the response send by the server to the client
      	 * @throws ServletException if an error occurred
      	 * @throws IOException if an error occurred
      	 */
      	public void doPost(HttpServletRequest request, HttpServletResponse response)
      			throws ServletException, IOException {
      
      		String sel = request.getParameter("sel");
      		if(sel.equals("1")){
      			//直接跳转到百度
      			response.sendRedirect("http://www.baidu.com");
      		}else if (sel.equals("2")){
      			//本应用的相对路径:
      			response.sendRedirect("form.html");
      		}else{ //其余的显示默认节目
      			response.setContentType("text/html");
      			PrintWriter out = response.getWriter();
      			out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
      			out.println("<HTML>");
      			out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
      			out.println("  <BODY>");
      			out.print("    This is ");
      			out.print(this.getClass());
      			out.println(", using the POST method");
      			out.println("  </BODY>");
      			out.println("</HTML>");
      			out.flush();
      			out.close();
      		}
      		
      	}
      
      	/**
      	 * Initialization of the servlet. <br>
      	 *
      	 * @throws ServletException if an error occurs
      	 */
      	public void init() throws ServletException {
      		// Put your code here
      	}
      
      }
      
      
      中的doPost方法
    2. 测试:
      1. 使用绝对路径:
        1. 地址:http://localhost:8089/myWeb3/redirect.html
        2. 界面:
        3. 结果:
      2. 使用相对路径:
        1. 地址:http://localhost:8089/myWeb3/redirect.html
        2. 界面:
        3. 结果:
  2. 重定向注意事项
    1. 不能在out.println或者write之后调用sendRedirect,也就是输出界面或者write是二选一的
    2. sendRedirect方法的相对路径有两种形式
      1. 前面加“/”:表示从本应用的根目录开始,比如要跳转到本应用的form.html(位于myWeb3目录下),这种情况就要写:/myWeb3/form.html
      2. 前面不加"/",也就是默认当前请求URL地址的相对根目录的路径,当前地址的相对目录路径是http://localhost:8089/myWeb3/,所以重定位地址就是http://localhost:8089/myWeb3/redirect.html
  • 请求分派

  1. 概念:servlet将请求分发给应用的其它部分完成,比如jsp,这部分的工作是在服务端完成的,对客户端来说是透明的
  2. 源代码:
    package web;
    
    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 Redirect extends HttpServlet {
    
    	/**
    	 * Constructor of the object.
    	 */
    	public Redirect() {
    		super();
    	}
    
    	/**
    	 * Destruction of the servlet. <br>
    	 */
    	public void destroy() {
    		super.destroy(); // Just puts "destroy" string in log
    		// Put your code here
    	}
    
    	/**
    	 * The doGet method of the servlet. <br>
    	 *
    	 * This method is called when a form has its tag value method equals to get.
    	 * 
    	 * @param request the request send by the client to the server
    	 * @param response the response send by the server to the client
    	 * @throws ServletException if an error occurred
    	 * @throws IOException if an error occurred
    	 */
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    
    		response.setContentType("text/html");
    		PrintWriter out = response.getWriter();
    		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
    		out.println("<HTML>");
    		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
    		out.println("  <BODY>");
    		out.print("    This is ");
    		out.print(this.getClass());
    		out.println(", using the GET method");
    		out.println("  </BODY>");
    		out.println("</HTML>");
    		out.flush();
    		out.close();
    	}
    
    	/**
    	 * The doPost method of the servlet. <br>
    	 *
    	 * This method is called when a form has its tag value method equals to post.
    	 * 
    	 * @param request the request send by the client to the server
    	 * @param response the response send by the server to the client
    	 * @throws ServletException if an error occurred
    	 * @throws IOException if an error occurred
    	 */
    	public void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    
    		String sel = request.getParameter("sel");
    		if(sel.equals("1")){
    			//直接跳转到百度
    			response.sendRedirect("http://www.baidu.com");
    		}else if (sel.equals("2")){
    			//本应用的相对路径:
    //			response.sendRedirect("form.html");
    			response.sendRedirect("index.jsp");
    		}else if(sel.equals("3")){
    			//进行请求分派
    			RequestDispatcher requestDispatcher = request.getRequestDispatcher("form.html");
    			requestDispatcher.forward(request,response);
    		}
    		else{ //其余的显示默认节目
    			response.setContentType("text/html");
    			PrintWriter out = response.getWriter();
    			out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
    			out.println("<HTML>");
    			out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
    			out.println("  <BODY>");
    			out.print("    This is ");
    			out.print(this.getClass());
    			out.println(", using the POST method");
    			out.println("  </BODY>");
    			out.println("</HTML>");
    			out.flush();
    			out.close();
    		}
    		
    	}
    
    	/**
    	 * Initialization of the servlet. <br>
    	 *
    	 * @throws ServletException if an error occurs
    	 */
    	public void init() throws ServletException {
    		// Put your code here
    	}
    
    }
    
    
    中的doPost方法
  3. 重定向和请求分派的区别
    1. 重定向在客户端进行,重定向后用户会看到他请求的URL变化的
    2. 请求分派在服务端进行的,这个对客户端是透明的,所以请求分派后客户端看到的URL是没有变化的
    3. 重定向可以重定向到其它应用(也就是外部网址都可以),但是请求分派只能是本应用的html或者JSP
  4. 重定向和请求分派的共同点是:输出(println或者write )和重定向(或者请求分派)只能二选一

》》》》》未完:易学笔记--Servlet和JSP--入门就看这一篇就够了》》》》》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

易学笔记(qq:1776565180)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值