码迷,mamicode.com
首页 > 编程语言 > 详细

JavaWeb总结(四)—JSP深入解析

时间:2016-04-11 00:09:02      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

一、JSP域对象

1.JSP属性范围(域对象范围)    

JSP提供了四个域对象,分别是pageContextrequestsessionapplication

pageContext: 属性范围仅限于当前JSP页面。一个属性只能在一个页面取得,跳转到其他页面无法取得。

request: 属性作用范围仅限于同一个请求。一个页面中设置的属性,只要经过了服务器的跳转,跳转之后的页面都可以继续取得。

session: 存储在session对象中的属性可以被属于同一个会话的所有Servlet和JSP页面访问。浏览器打开到关闭称作一次会话。

application: 存储在application对象中的属性可以被同一个Web应用程序的所有Servlet和JSP页面访问。

 

2.域对象的相关方法  

(1)setAttribute()

     设置属性的方法。之前所讲解的四种属性范围,实际上都是通过pageContext属性范围设置上的。打开pageContext所在的说明文档。

技术分享

PageContext类继承了JspContext类,在JspContext类中定义了setAttribute方法,如下:

技术分享

此方法中存在一个scope的整型变量,此变量就表示一个属性的保存范围。

后面有一个int类型的变量,在PageContext中可以发现有4种。

    public static final int PAGE_SCOPE = 1;

    public static final int REQUEST_SCOPE = 2;

    public static final int SESSION_SCOPE = 3;
 
    public static final int APPLICATION_SCOPE = 4;

      这个setAttribute()方法如果不写后面的int类型的scope参数,则此参数默认为PAGE_SCOPE,则此时setAttribute()方法设置的就是page属性范围,如果传递过来的int类型参数scope为REQUEST_SCOPE,则此时setAttribute()方法设置的就是request属性范围,同理,传递的scope参数为SESSION_SCOPE和APPLICATION_SCOPE时,则表示setAttribute()方法设置的就是session属性范围和application属性范围。

 

(2)getAttribute(String name)

     获取指定的属性。

 

(3)getAttributeNames()

     获取所有属性名字组成的Enumeration对象。

 

(2)removeAttribute(String name)

     移除指定的属性。

 

二、请求转发和重定向

 

1.用法

<html>
  <head>
    
    <title>My JSP ‘hello.jsp‘ starting page</title>
    
  </head>
  
  <body>
  
	<form action="ForwardServlet">
		年龄: <input type="text" name="age"><br>
		<input type="submit" value="提交">
		
	</form>
	  
  </body>
</html>

 

ForwardServlet.java

public class ForwardServlet extends HttpServlet{

	public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
		
		String age = req.getParameter("age");
		System.out.println("ForwardServlet: age = " + age);
		
		//请求的转发
		//1.调用HttpServletRequest的getRequestDispatcher()方法获取RequestDispatcher对象。
		String path = "TestServlet";
		RequestDispatcher dispatcher = req.getRequestDispatcher("/" + path);
		//2.调用RequestDispatcher对象的forward()方法
		dispatcher.forward(req,resp);
	}

	public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
			doGet(req,resp);
	}

}

 

TestServlet.java

public class TestServlet extends HttpServlet{

	public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
		String age = req.getParameter("age");
		System.out.println("TestServlet: age = " + age);
	}

	public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
			doGet(req,resp);
	}

}

结果:

ForwardServlet: age = 123
TestServlet: age = 123

 

如果把ForwardServlet内部重定向到TestServlet。

public class ForwardServlet extends HttpServlet{

	public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
		
		String age = req.getParameter("age");
		System.out.println("ForwardServletage = " + age);
		
		//请求的重定向
		String path = "TestServlet";
		resp.sendRedirect(path);
	}

	public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
			doGet(req,resp);
	}

}

结果:

ForwardServletage = 23
TestServlet: age = null

JavaWeb总结(四)—JSP深入解析

标签:

原文地址:http://www.cnblogs.com/yangang2013/p/5376438.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!