Day 06-2 域对象及请求转发重定向


.域对象的属性操作

Jsp或者Servlet通过向着这四个对象放入数据,从而实现Jsp和Servlet之间数据的共享。

1.和属性相关方法:

   Void setAttribute(String name,Object name):获取指定的属性

   Object getAttribute(String name,object name):获取所有属性的名字组成的Enumeration对象

   removeAttributeString name:移除指定的属性

   void setAttribute(Strng name,Object o):设置属性

                                     属性名    属性值

 pageContext,request,session,appliaction对象都有这些方法

Application:属性作用域为整个WEb应用,是范围最大的属性作用范围,只要在一处设置属性,在其他各处的JSPServlet中都可以获取到

Session:属性的作用范围仅限于一次会话(浏览器打开直到关闭称之为一次会话,再次期间不失效 )

Request:作用范围一次请求仅限于同一个请求(在有转发的情况下可以跨页面获取属性值)

PageContext:作用范围仅限于当前WEB应用(只要在一处设置Servlet属性,在其他各处的JSP或Servlet中都可以获取到)

范围从大到小


 例:

    <%
        pageContext.setAttribute("pageContextAttr", "pageContextValue");
        request.setAttribute("requestAttr", "requestValue");
        session.setAttribute("sessionAttr", "sessionValue");
        application.setAttribute("applicationAttr", "applicationValue");
     %>
       <br><br>
       pageContextAttr:<%=pageContext.getAttribute("pageContextAttr") %><br>
       requestAttr:<%=request.getAttribute("requestAttr") %><br>
      sessionAttr:<%=session.getAttribute("sessionAttr") %><br>
      applicationAttr:<%=application.getAttribute("applicationAttr") %><br>


二.请求的转发和重定向

1.请求的转发

  两个步骤:

 1)调用HttpServletRequest的getRequestDispatcher()方法 获取RequestDispatcher对象且调用getRequestDispatcher传送要访问的路径

    2)调用requestDispatcher.forward(request, response)进行请求的转发

     例:从HTML页面传入一个name为url的值,点击Submit将值传入到Forward页面,Forword页面将值转发到Servlet页面并输出

jsp.jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	 <form action="Forward" method="get">
	 	<input type="text" name="url" />
	 	<input type="submit" value="submit" />
	 </form>
</body>
</html>

Forword.java文件

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("ForwardServlet doGet");
		

		
		//定义路径,与跳转的页面同名
		String path="TestServlet";
		
		//调用HttpServletRequest的getRequestDispatcher()方法 获取RequestDispatcher对象
		//调用getRequestDispatcher传送要访问的路径
		RequestDispatcher requestDispatcher=request.getRequestDispatcher("/"+path);
		
		//调用requestDispatcher.forward(request, response)进行请求的转发            
		requestDispatcher.forward(request, response);
	}



request.getRequestDispatcher("/c.jsp").forward(request, response);     直接转发



Servlet.java文件

public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String url = request.getParameter("url");
		System.out.println(url);
		
	}



2.重定向:

 直接调用response.sendRedirect(path)方法


例:运行Redii页面跳转到TestServlet页面并输出

public class Redii extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    String path="Demo";
	    response.sendRedirect(path);
	}

}

public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	  System.out.println("TestServlet's doGet哈哈哈");
	  
	}


3.请求的转发和重定向的本质区别:请求的转发只发出了一次请求,而重定向则发出了两次请求

    具体特点:

      1)地址栏是初次发出的地址,而请求的重定向地址栏不再是初次发出的地址,地址栏为最后响应的地址

      2)在请求的转发最终的Servlet中,request对象和中转的request是同一个对象,而请求的重定向不是同一个对象

      3)请求转发只能转发到当前WEB应用的资源,请求的重定向可以重定向到任何资源

      4)请求 的转发:/代表是当前WEB应用的根目录

            请求的重定向:/ 代表的是当前WEB站点的跟目录

       注:当前Web应用的根目录:http:\\localhost:8989/文件名

               站点的根目录:http/localhost:8989/

例:

a.jsp

<body>

<h3>aaa page</h3>
<a href="b.jsp"> to b</a>
</body>

b.jsp(请求转发)

<body>

<h4>bbb page</h4>

<% 
  request.getRequestDispatcher("/c.jsp").forward(request, response);
%>

</body>


c.jsp

<body>
<h4>ccc page</h4>
</body>

结果:

运行a.jap ,点击 to b(注意地址栏由a.jsp调到b.jsp)





改变b.jsp(重定向)

<body>

<h4>bbb page</h4>

<% 
    response.sendRedirect("c.jsp");   
%>

</body>

结果:(地址栏由a.jsp调到c.jsp)




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值