Java EE JSP built-in object and expression language

Source: Internet
Author: User
Tags session id object object

First, JSP built-in objects

JSP provides built-in objects based on the Servlet API specification that developers can use to access these objects without declaring them in advance.

The JSP provides 9 built-in objects:

(a), Request brief:

The most commonly used object in JSP programming, representing a request from a client, invokes the corresponding method of the request object to obtain information about a customer request.

Examples of usage of common methods:
1<font size=5>2<br>the protocol used by the client is:3<%4String protocol =Request.getprotocol ();5 out.println (protocol);6%>7<br>get the page that receives the customer submission information:8<%9String Path =Request.getservletpath ();Ten out.println (path); One%> A<br>get the length of the customer submission information: -<% -         intLength =request.getcontentlength (); the out.println (length); -%> -<br> get HTTP header files in user-the value of the agent: -<% +String Headerfirst = Request.getheader ("User-agent"); - out.println (headerfirst); +%> A<br>gets the value of the accept in the HTTP header file: at<% -String Headersecond = Request.getheader ("Accept"); - out.println (headersecond); -%> -<br>gets the value of the host in the HTTP header file: -<% inString Headerthird = Request.getheader ("Host"); - out.println (headerthird); to%> +<br> get HTTP header files in accept-value of encoding: -<% theString Headerfourth = Request.getheader ("accept-encoding"); * out.println (Headerfourth); $%>Panax Notoginseng<br>get the IP address of the customer: -<% theString IP =request.getremoteaddr (); + out.println (IP); A%> the<br>get the name of the client: +<% -String ClientName =request.getremotehost (); $ out.println (clientName); $%> -<br>get the name of the server: -<% theString ServerName =request.getservername (); - out.println (serverName);Wuyi%> the<br>get the port number of the server: -<% Wu         intServerPort =Request.getserverport (); - out.println (serverport); About%> $</font>
(b), Response brief description:

This object is used to send data to the client, such as cookies, HTTP header information, and so on.

Through the Response object organization sends the information to the client, but the organization way compares the lower level therefore does not recommend the General Program developer to use, generally needs to send the text message to the client to use the Out object directly.

Usage notes and examples of common methods:

①, Addcookie (Cookie cookie): adds a cookie object to the response object to hold the client's user information and obtains the cookie through the Request object's GetCookies () method.

1 New Cookie ("name", "Dlut"); 2 Response.addcookie (cookie);

②, AddHeader (String name,string value): adds an HTTP header to the Response object, which is uploaded to the client, overwriting the original header if the file header exists with the same name.

③, Containsheader (String name): determines whether the HTTP file header for the specified name in the response object exists.

④, senderror (int SC): sends an error message to the client.

⑤, SetHeader (String name,string value): sets the value of the HTTP file header for the specified name, if the value exists, the change will be overwritten by the new value.

1 eg: let Web pages refresh every 5 seconds 2 response.setheader ("Refresh", "5");

⑥, setContentType (String value): sets the type of return response object.

⑦, sendredirect (String URL): redefine the request to a new page.

(c), out briefly:

An Out object represents an object that sends data to the client, and the content sent through the object is displayed on the browser and belongs to the text.

A buffer is contained inside the out object.

Common methods:

Print (), println (), Clear (), Clearbuffer (), flush (), getbuffersize (), and getremaining ()

(iv), session summary:

A session is a collection of multiple requests initiated by the same user, and when a user sends a request to the site for the first time, the server creates a Session object and assigns the object a unique session ID, which is passed as part of each request. This makes it easier for the server to remember the state of the client across multiple requests by matching the user to the session object.

The information that is saved and retrieved in the session must be a Java object object, not a basic data type such as Int.

Usage notes:

1. Get Session Object

Gets the session object for the current request by calling HttpServletRequest's GetSession (). It is common to create a session object automatically by setting the parameter of GetSession () to true so that there is no ready-made session.

1 HttpSession session = request.getsession (true);

The HttpSession object survives with the server, which is automatically associated to the requesting sender through a background mechanism such as a cookie or a URL.

2. Storing and reading attribute values

The Session object provides an built-in data structure that can hold any number of key-value pairs.

(1). Storage

The property value is stored by calling the SetAttribute (String Name,object Object) method of the Session object.

(2). Read

The property value is read by calling the GetAttribute (String name) method of the Session object.

(v), Application brief description:

This object represents the Web application running on the server, which is equivalent to the servlet context.

Once created, the object will persist until the server shuts down.

Usage notes for common methods:
Method name Method description
Getatteibute (String name) Returns the value of the Application object property of the name specified by name, which is an object.
SetAttribute (String name,object Object) Initializes a property with object, which is specified by name.
Getatteibutenames () Returns the name of all Application object properties, which are instances of a enumeration (enum) class.
Getinitparameter (String name) Returns the application initialization parameter property value, which is specified by name.
Getserverinfo () Gets the information for the current version of the servlet compiler.
GetMimeType () Gets the type of the component mime.
Getrealpath () Gets the true path of the component on the server.

(vi), config description:

The config built-in object is an instance of the ServletConfig class that is used by the JSP engine to pass information to it (servlet) through config when the servlet is initialized. This information can be a property name/value matching parameter, or it can be information about the server that is passed through the ServletContext object. Config built-in objects are seldom used in JSP development, and are only used if you need to overload the servlet's init () method when writing a servlet.

Common methods:
Method name Method description
Getservletcontext () Returns a ServletContext object that contains information about the server.
Getinitparameter (String name) Returns the value of the initialization parameter
Getinitparameternames () Returns all parameters that are required to initialize the servlet, the return type is an enum type


(vii), PageContext brief description:

The PageContext built-in object is a special object that is the largest integrator of all other object features in the page, even though it provides access to all other objects on this page, such as the request, response, out, and Page objects already described earlier. Since objects such as request and response can be used directly by calling methods in JSP, PageContext objects are seldom used in the actual JSP development.

Common methods:
Method name Method description
Getrequest () Returns the request object from the current page.
GetResponse () Returns the response object in the current page.
GetSession () Returns the Session object in the current page.
Getservletcontext () Returns the Application object in the current page.
GetPage () Returns the Page object in the current screen.
Getout () Returns the Out object in the current page.
GetException () Returns the exception object in the current page.
Getservletconfig () Returns the Config object from the current page.
SetAttribute (String name) Sets the property value for the specified property name.
GetAttribute (String name) The corresponding property value is found based on the property name.
SetAttribute (String name, Object obj, int scope) Sets the corresponding property value within the given range.
GetAttribute (String name, int scope) Gets the corresponding property value within the given range.
Findattribute (String name) Looks for a property and returns null if it is not found.
RemoveAttribute (String name) Remove an attribute from the property name.
RemoveAttribute (String name, int scope) Deletes a property in a specified range.
Getattributescope (String name,int scope) Returns the scope of a property.
Getattributenamesinscope (int scope) Returns an enumeration of all property names within a specified range.
Release () Release all the information that PageContext occupies.
Forward (String Relativeurlpath) Redirects to another page using the current page.
Include (String Relativeurlpath) Use another page that is contained in the current location.






(eight), page description:

The Page object is somewhat similar to the this pointer in Java programming, meaning the current JSP page itself. The page is an object of the Java.lang.Object class. Page objects are not often used in the actual development process.

Common methods:
Method name Method description
GetClass () Returns the class of the object at that time.
Hashcode () Returns the hash code of the object at this time.
ToString () Converts the object class at this time into a string.
Equals (Object object) Compares whether this object is equal to the specified object.
Copy (Object object) Copies this object to the specified object.
Clone () Clones this object.

(ix), Exception brief description:

This object is used to handle all errors and exceptions that occur when the JSP file is executed.

Common methods:
Method name Method description
GetMessage () Returns an error message.
Printstacktrace () Output a stack of errors and errors in the form of a standard error.
ToString () Returns a description of the exception as a string.

Precautions: (Very important)

Inline object exception must be used in cases where the <% @page iserrorpage= "true"%>.

Second, JSP built-in object corresponding type and scope of action
JSP built-in objects corresponding type Function range
Request Javax.servlet.servletRequest Request
Response Javax.servlet.servletResponse Page
Session Java.servlet.http.HttpSession Session
Application Java.servlet.servletContext Application
Page Java.lang.Object Page
Out Java.servlet.jsp.JspWrite Page
PageContext Java.servlet.jsp.PageContext Page
Config Java.servlet.servletConfig Page
exception Java.lang.throwable Page

Third, expression language

Expression language (EL): El provides a more convenient way to generate dynamic Web pages than JSP scripts.

Java EE JSP built-in object and expression language

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.