In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you the JSP object and scope properties of the example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!
JSP technology uses Java programming language to write XML-like tags and scriptlets to encapsulate the processing logic for generating dynamic web pages. Web pages can also access the application logic of resources that exist on the server side through tags and scriptlets.
Objects in the JSP page, including user-created objects (for example, JavaBean objects) and implicit objects in JSP, have a scope property. The scope defines when and in which JSP page these objects can be accessed. For example, a session object can be accessed on multiple pages during a session. The application object is accessible throughout the life cycle of the Web application. In JSP, there are four ranges, as shown below.
I. page range
Objects with page scope are bound to javax.servlet.jsp.PageContext objects. Objects within this scope can only be accessed on the page where the object was created. You can call the getAttribute () method of the implied object pageContext to access objects of this scope type (the pageContext object also provides getAttribute methods to access other scope objects), and the pageContext object itself falls within the scope of page.
When the _ jspService () method of the Servlet class finishes, references to objects that fall within the scope of page are discarded. Page-scoped objects, created each time the client requests a JSP page, are deleted after the page sends back a response to the client or the request is forward to another resource.
II. Request range
Objects with request scope are bound to javax.servlet.ServletRequest objects, and the getAttribute () method of the implied object request can be called to access objects of this scope type. Objects in this range can be accessed on pages that are directed by calling the forward () method or on pages that are included in the call to the include () method. Note that because the request object is different for each customer request, objects in this scope are recreated and deleted for each new request.
III. Session range
Objects with session scope are bound to javax.servlet.http.HttpSession objects, and the getAttribute () method of the implied object session can be called to access objects of this scope type. The JSP container creates a HttpSession object for each session, during which objects within the scope of the session can be accessed.
IV. Application range
Objects with application scope are bound to javax.servlet.ServletContext, and the getAttribute () method of the implied object application can be called to access objects of this scope type. While the Web application is running, all pages can access objects in this scope.
Let's take a look at the application of these four range objects through a few simple examples.
1. Test the page range
Test1.jsp
<% pageContext.setAttribute ("name", "zhangsan"); out.println ("test1.jsp:"); out.println (pageContext.getAttribute ("name")); out.println ("< p >"); pageContext.include ("test2.jsp");% >
Test2.jsp
<% out.println ("test2.jsp:"); out.println (pageContext.getAttribute ("name"));% >
When you visit test1.jsp, you will see the following output:
Test1.jsp: zhangsan
Test2.jsp: null
Indicates that the properties saved in the pageContext object have a page scope and can only be accessed in the same page.
2. Test request range
Modify test1.jsp and test2.jsp, as shown below.
Test1.jsp
<% request.setAttribute ("name", "zhangsan"); out.println ("test1.jsp:"); out.println (request.getAttribute ("name")); out.println ("< p >"); pageContext.include ("test2.jsp");% >
Test2.jsp
<% out.println ("test2.jsp:"); out.println (request.getAttribute ("name"));% >
When you visit test1.jsp, you will see the following output:
Test1.jsp: zhangsan
Test2.jsp: zhangsan
Indicates that the properties stored in the request object have a request scope that can be accessed while the request object is alive. Set
PageContext.include ("test2.jsp")
To comment on this sentence, visit test1.jsp first, and then visit test2.jsp, and you can see the following output:
Test2.jsp: null
This is because the client has started a new request.
3. Test session range
Modify test1.jsp and test2.jsp, as shown below.
Test1.jsp
< session.setAttribute ("name", "zhangsan"); >
Test2.jsp
<% out.println ("test2.jsp:"); out.println (session.getAttribute ("name"));% >
Visit test1.jsp first, then visit test2.jsp in the same browser window, and you can see the following output:
Test2.jsp: zhangsan
Indicates that the properties saved in the session object have a session scope within which objects can be accessed during the session.
If we close the browser, reopen the browser window, and visit test2.jsp after visiting test1.jsp, we will see the following output:
Test2.jsp: null
This is because the client starts a new session with the server.
4. Test application range
Modify test1.jsp and test2.jsp, as shown below.
Test1.jsp
< application.setAttribute ("name", "zhangsan"); >
Test2.jsp
<% out.println ("test2.jsp:"); out.println (application.getAttribute ("name"));% >
First visit test1.jsp, then close the browser, then open the browser window, visit test2.jsp, and you can see the following output:
Test2.jsp: zhangsan
Indicates that the properties saved in the application object have an application scope that can be accessed while the Web application is running.
The above is all the content of the article "sample Analysis of object and scope Properties in JSP". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.