What are the four major scopes of jsp
JSP built-in object scope
application: valid in all applications, that is, as long as the website is running, this scope is valid, this refers to the program working process.
session: valid in the current session, that is, starting from the browser accessing the server, this refers to an access process of the user, that is, from the beginning to the end of the session
request: in the current session Valid in the request, responsible for information sharing between multiple servlets, that is, putting the information into the request, it is valid during the entire request stage, that is, the user can obtain this data at this stage of access
page : Valid on the current page, that is, valid on a jsp page.
First of all, we must declare that the so-called "scope" is the "scope of information sharing", that is to say, the scope within which a piece of information can be effective.
The most basic unit of Web interaction is HTTP request. The process from entering the website to leaving the website for each user is called an HTTP session. During the operation of a server, multiple users will access it, which is multiple HTTP sessions. Scope is explained below.
application: The period from when the server starts to when it stops.
session: The period from the beginning to the end of the HTTP session.
request: The period from the beginning to the end of the HTTP request.
page: The time from opening to closing of the current page.
1. application scope
The application scope is the entire period from startup to shutdown of the server. The information set in this scope can be used by all applications. Information transfer on the application scope is implemented through ServletContext, and the main methods it provides are as follows:
Object getAttribute (String name): Get information from the application.
void setAttribute(String name, Object value): Set information to the application scope.
2. Session scope
The session scope is easier to understand. The same browser visits the server multiple times and transfers information between these multiple visits. It is the embodiment of session scope. Session is implemented through the HttpSession interface, and the main methods it provides are as follows.
Object HttpSession.getAttribute(String name): Get information from session.
void HttpSession.setAttribute(String name, Object value): Save information to the session.
HttpSession HttpServletRequest.getSession(): Get the session object where the current request is located.
The start time of the session is relatively easy to judge. It is considered that the session starts when the browser sends the first HTTP request. But the end time is difficult to judge, because the server is not notified when the browser is closed, so it can only be judged by the following method: if the client does not respond within a certain period of time, the session is considered to be over. Tomcat's default value is 120 minutes, but this value can also be set through the setMaxInactiveInterval(int interval) method of HttpSession. If you want to actively end the session, such as when the user clicks the "Logout" button, you can use HttpSession's invalidate() Method used to forcefully end the current session.
3. Request scope
The processing of an HTTP request may require the cooperation of multiple Servlets, and information can be transferred between these Servlets in some way. But this information is invalid after the request is completed.
Information sharing between Servlets is achieved through two methods of the HttpServletRequest interface.
void setAttribute(String name, Object value): Save the object value to the request scope with name as the name.
Object getAttribute(String name): Get the information of the specified name from the request scope.
The first parameter of the doGet() and doPost() methods in JSP is the HttpServletRequest object. Use the setAttribute() method of this object to transfer information.
So after setting the information, how do you pass the information to other Servlets? This requires using the forward() method of the RequestDispatcher interface, through which the request is forwarded to other Servlets.
RequestDispatcher ServletContext.getRequestDispatcher(String path): Get the Dispatcher for forwarding. path is the destination Servlet for forwarding.
void RequestDispatcher.forward(ServletRequest request, ServletResponse response): Forward request and response.
Therefore, you only need to set the corresponding attributes through the setAttribute() method in the current Servlet, then use the forward() method to jump, and finally use the getAttribute() method in the jumped Servlet. Information transfer can be achieved.
PHP programmers may not understand this paragraph very well, because there is no concept of forwarding in PHP, and a request can only be processed by one PHP file, so there is no concept of request scope in PHP. Servlets are different. Requests can be forwarded arbitrarily in the application, so the request scope is used to transfer information between different Servlets.
Note two points:
Forwarding is not redirection, forwarding is performed within the web application. PHP supports redirects but not forwarding.
Forwarding is transparent to the browser, that is to say, no matter how it is forwarded on the server, the address of the original Servlet is still displayed in the browser address bar. The redirected browser address changes.
4. Page Scope
The scope of the page object is limited to the current page requested by the user. The reference to the page object will be retrieved after the response is returned to the client. Released, or released after the request has been forwarded elsewhere. A reference to the page object is usually stored in the pageContext object.
The scope of the above introduction is getting smaller and smaller. The life cycles of request and page are short-lived. The difference between them is: a request can contain multiple pages (include, forward and filter).
The above is the detailed content of What are the four major scopes of jsp. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



jsp belongs to the backend. The essence of jsp is a servlet, and servlet is a server-side java application, so jsp is a back-end technology. JSP is deployed on the web server and can respond to requests sent by the client and dynamically generate web pages of HTML, XML or other format documents based on the request content, and then return them to the requester. JSP technology uses Java as a scripting language to provide services for user HTTP requests, and can handle complex business requirements together with other Java programs on the server.

The difference between jsp and html: 1. Operating mechanism; 2. Purpose; 3. Relationship with Java; 4. Function; 5. Relationship with back-end; 6. Speed; 7. Maintainability and scalability; 8. Learning and use Difficulty; 9. File suffixes and identification tools; 10. Community and support; 11. Security. Detailed introduction: 1. Operating mechanism. HTML is a markup language, mainly used to describe and define the content of web pages. It runs on the client and is interpreted and executed by the browser. JSP is a dynamic web page technology that runs on the server side, etc. wait.

After the new springboot project is created, the directory is as follows: Step 1: Add the jar package code required to configure jsp to the project's pom file: org.apache.tomcat.embedtomcat-embed-jasperprovided Step 2: Create a new directory webapp under the main path , create a new path WEB-INF under webapp, create a new path jsp under WEB-INF, and place the jsp file we want to use under this path. Step 3: Configure the access path and suffix code of the jsp file in the main configuration file: spring .mvc.view.prefix=/WEB-INF/jsp/sp

Implementation steps: 1. Introduce the JSTL tag library into the JSP page; 2. Obtain data from the database; 3. Paging the data; 4. Display the paging navigation bar in the page; 5. Display the number according to the current page number and each page. , just get the corresponding data from the paging data and display it on the page.

1. The core sql statement to implement paging query (1) The sql statement to query the total number of records in the database: selectcount(*)from+(table name); (2) The sql statement to query the number of records for each query: where: 0 is searched Index, 2 is the number of items searched each time. select*from table name limit0,2; 2. Code implementation* I wrote these two classes in the previous article, DBconnection class: used to obtain database connections, Author object class. Click on the link to view the code for these two classes. Click the link to view the DBconnection class and Author object class (1) Login page: index.jsp. Inserttitlehere user column

JSP is a dynamic web technology standard. Its file format is to insert Java program segments (Scriptlet) and JSP tags (tags) into traditional web page HTML files (.htm, .html) to form JSP files (*.jsp ).

Can JSP be replaced by PHP? JSP (JavaServerPages) and PHP (HypertextPreprocessor) are commonly used server-side scripting languages for dynamically generating web page content. Although they each have their own characteristics and advantages, can they completely replace each other in practical applications? This article will analyze the advantages and disadvantages of the two and compare them through specific code examples. First, let's take a look at the respective characteristics of JSP and PHP. JSP is a Java-based

JavaWeb development technology stack: Master JavaEE, Servlet, JSP, Spring and other technologies used for Web development. With the rapid development of the Internet, in today's software development field, the development of Web applications has become a very important technical requirement. As a widely used programming language, Java also plays an important role in the field of Web development. The JavaWeb development technology stack involves multiple technologies, such as JavaEE, Servlet, JSP, Spr
