Introduction to JSP built-in objects

Source: Internet
Author: User

 

1. Introduction to built-in objects

 

JSP built-in objects are very important in JSP. These built-in objects are created by WEB containers, so you do not need to create them yourself.

The main built-in objects include:

(1) request: javax. servlet. http. HttpServletRequest, indicating the customer's request.

Specific usage: request. getParameter ("name.

(2) response: javax. servlet. http. HttpServletResponse, indicating the server response.

(3) pageContext: javax. servlet. jsp. pageContext, indicating the JSP page.

(4) session: javax. servlet. http. HttpSession, indicating a session.

(5) application: javax. servlet. servletContext, indicating that all users share information.

(6) out: javax. servlet. jsp. jspWriter writes the page content.

(7) page: indicates an instance of a page.

(8) config: javax. servlet. servletConfig, which indicates the configuration file information.

 

2. Four attribute ranges

 

(1) page range: valid for one page. redirection is invalid.

(2) request range: valid for server redirect and invalid for client redirect.

(3) session range: The jump is valid and the new browser is invalid.

(4) application Scope: valid for all users and invalid for restarting the server.

The four objects have three methods:

(1) void setAttribute (String key, Object o );

(2) Object getAttribute (String key): it must be transformed downward after it is obtained.

(3) void removeAttribute (String key );

 

Iii. request object

 

The request object is the information sent from the receiving client, and the client sends the header information, content, transmission method, and cookie.

Common Methods:

(1) request. getParameter (String name): obtains the value of the name element.

(2) Enumeration enu = request. getParameterNames (): Get the names of all parameters and traverse them through the iterator.

(3) String [] strs = request. getParameterValues (String name): used to obtain multiple choices such as checkbox.

(4) Enumeration enu = request. getHeaderNames (): Get the name of all header information.

(5) String str = request. getHeader (String name): obtains the header information with the name.

(6) String str = request. getMethod (): get is post or get.

(7) request. setCharacterEncoding (String): Uniform request Encoding.

(8) request. isUserInRole (String name)

(9) Cookie [] c = request. getCookies (): obtain all cookies.

Add new users and new permissions to tomcat:

(1) In tomcat/conf/tomcat-users.xml can be set, refer to the admin configuration, and then restart the tomcat server to complete the loading of new users and permissions.

(2) set in web. xml so that the user name and password will pop out of the window before you log on to the webpage.

 

Iv. Differences between post and get

 

The most obvious difference is the difference in the address bar. When a post is passed, the address bar does not change, while get is passed, and the address bar displays the transmitted information. Therefore, the information transmitted by get is limited.

 

5. response object

 

The object that responds to the client.

 

1. Common Methods:

 

(1) addCookie: Add Cookie

(2) setHeader (String name, String value): sets the header information.

(3) sendRedirect (): Redirection, similar to client jump.

Common applications:

(1) timed refresh: response. setHeader ("refresh", "seconds"): refreshes every second.

(2) scheduled jump: response. setHeader ("refresh", "2; URL = hello. jsp"); jump to hello. jsp in 2 seconds.

(3) direct jump: response. sendRedirect ("hello. jsp ");

 

2. Cookie control:

 

Saves user information as a cookie stored on the client. Each request sends the cookie to the server.

Response. addCookie (Cookie e); add cookie.

The Cookie is located in javax. servlet. http. cookie.

Common Methods:

(1) Cookie c = new Cookie ("name", "value"): Create a new cookie.

(2) c. setMaxAge (int seconds): sets the maximum lifetime of a cookie because the original cookie is saved in the browser. If the browser is closed, the cookie is lost.

(3) c. getName (): Get the name.

(4) c. getValue (): obtain the value.

 

Vi. session Object

 

Common session methods:

(1) String getId (): obtains the session id.

(2) session. setAttribute ("", ""): sets attributes.

(3) session. getCreationTime (): Obtain the creation time.

(4) session. getLastAccessedTime (): Get the last access time.

(5) session. isNew (): whether the session is new.

(6) session. invalidate (): clears all session records.

Login-logout page:

(1) Use session. setAttribute () for registration.

(2) Use session. isNew () to determine whether the application has been registered.

 

VII. application Object

 

Common Methods:

(1) getRealPath (path): Get the real path.

(2) getContextPath (): Obtain the virtual path.

Application = this. getServletContext ()

 

8. config object

 

First from the configuration file, in every web application, there is a WEB-INF folder, this folder is safe, can not be seen by others.

If you put a hello. jsp file in the WEB-INF, if you can make it accessible.

The answer is to set it in WEB-INF/web. xml, the content is as follows:

<Servlet>

<Servlet-name> he </servlet-name>

<Jsp-file>/WEB-INF/hello. jsp </jsp-file>

</Servlet>

<Servlet-mapping>

<Servlet-name> he </servlet-name>

<Url-pattern> hello </url-pattern>

</Sevlet-mapping>

After the server is restarted, enter http: // localhost: 8080/test/hello in the address bar.

Common Methods of config:

(1) getInitParameterNames ();

(2) getInitParameter (String name );

What are initialization parameters?

The initialization parameters are configured in web. xml in the following format:

<Init-param>

<Param-name> name </param-name>

<Param-value> value </param-value>

</Init-param>

 

9. pageContext object

 

JSP context. You can obtain the request \ response \ session \ <jsp: forward> and so on through this instance.

From: xiazdong's column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.