What is a JSP?
To interpret the JSP's nine built-in objects, you must first understand what is a JSP? JSP full name Java Service pages Chinese: Java Server page. In fact, see JSP learned. NET can think of ASP, yes, the two really look alike. ASP is to add C # to the HTML page for the display layer, such as passing data in the background. JSP is a Java language that is injected into the HTML page to facilitate the delivery of information to the back end. JSP analysis is essentially a simplified version of the servlet. The difference between a JSP and a servlet is that the JSP requires the servlet container to be compiled into a servlet class before it can be run.
After you publish the project in Tomcat, you can find the translated Xxx_jsp.java file and the Xxx_jsp.class file for that JSP file in Tomcat's working directory, open Xxx_ Jsp.java above default is the first to identify the JSP nine built-in objects, and these nine built-in objects are servlet API, compiled into a servlet using these nine objects to manipulate the information between the client and the server , And in the JSP file, so before the translation of the JSP file is written in the JSP has declared the nine objects, the default initial recognition, and did not show.
nine built-in objects
1.Request: the instance of Javax.servlet.http.HttpServletRequest, which represents the client's request information, is mainly used to accept data transmitted to the server via the HTTP protocol. (including header information, System Information, request method, and request parameters), the object must be used to obtain the client request parameters.
Common methods
return value |
Method |
Description |
String |
GetParameter (String name) |
Returns the parameter value in the request |
String[] |
Getparametervalues (String name) |
Returns the value of the collection property in the request parameter |
Void |
SetAttribute (String key, Object value) |
Set the value of a property |
Object |
GetAttribute (String name) |
Returns the property value of the specified property |
String |
Getcharacterencoding () |
Returns the character encoding method |
String |
getContentType () |
Returns the MIME type of the requested page |
2,response: javax.serlvet.http.HttpServletResponse instance, on behalf of the server response to the client, mainly to the Servlet container processed objects back to the client. Typically, the object is rarely used directly in response, but an out object is used. General response objects are often used for redirection.
Common methods
return value |
Method |
Description |
String |
Getcharacterencoding () |
Return response applied which character encoding |
Void |
setContentType (String name) |
Set the MIME type of the response |
Void |
Addcookies (Cookie cookie) |
Save cookies to the client |
Void |
Sendredirect (String URL) |
Page redirection |
Servletoutputstream |
Getoutputstream () |
Returns a binary output stream of the response |
3,out: javax.serlvet.jsp.JspWriter instance, represents the output stream of JSP page, used for outputting content, form HTML page.
4.session: an instance of Javax.serlvet.http.HttpSession that represents a session and is an object that is automatically created by the server that is related to user requests. The server generates a Session object for each user that holds the user's information and tracks the user's operational status. The session object uses the map class internally to hold the data, so the format of the saved data is "Key/value". The value of the session object can make complex object types, not just string types.
Common methods:
return value |
Method |
Description |
Void |
SetAttribute (String key, Object value) |
Set the value of a property |
Object |
GetAttribute (String name) |
Returns the property value of the specified property |
5.Application:An example of Javax.servlet.ServletContext, which represents the Web application skill that JSP belongs to, can be used for JSP pages, or to exchange information between Servlets. The Application object saves information in the server until the server shuts down, otherwise the information saved in the Application object will be valid throughout the application. The Application object has a longer life cycle than the session object, similar to the system's global variables.
Common methods:
return value |
Method |
Description |
Void |
SetAttribute (String key, Object value) |
Set the value of a property |
Object |
GetAttribute (String name) |
Returns the property value of the specified property |
RequestDispatcher |
Getrequestdispatcher (String Uripath) |
Returns the RequestDispatcher object for the specified resource |
String |
Getinitparameter (String paramname) |
Used to invoke initialization of parameters stored in Web. xml |
ServletContext |
GetContext (String Uripath) |
Returns the Application object for the specified WebApplication |
6,PageContext: javax.servlet.jsp.PageContext example. Represents the JSP page context, which can be used to access the shared data on the page, that is, to obtain any range of parameters, through which you can get the JSP page out, request, reponse, session, application and other objects. The creation and initialization of PageContext objects is done by the container, and the PageContext object can be used directly in the JSP page
Common methods:
return value |
Method |
Description |
ServletConfig |
Getservletconfig () |
Returns the ServletConfig object (config) of the current page |
ServletContext |
Getservletcontext () |
Returns the ServletContext object of the current page (application) |
7,config:javax.servlet.ServletConfig instance, the main function of the object is to obtain the server configuration information. A config object can be obtained by using the Getservletconfig () method of the Pageconext object. When a servlet initializes, the container passes some information through the Config object to the servlet. Developers can provide initialization parameters for servlet programs and JSP pages in the application environment in the Web. xml file.
8,exception:java.lang.Throwable instance, represents exceptions and errors on other pages, the object can only be used if the page is an error handling page, that is, the Iserrorpage property of the compile instruction page is true.
Common methods:
return value |
Method |
Description |
String |
GetMessage () |
Returns a message describing the exception |
Void |
Printstacktrace () |
displaying exceptions and their stack traces |
9, page: represents the pages themselves, that is, the servlet in the this, its type is to generate a servlet class, can use the page where you can use this.
Summary
The above is the JSP nine built-in objects, from their own projects are often used to distinguish the role of each object, the method of each object to do a certain cognition, the lack of a place to slowly check the leakage of the vacancy!
give yourself a belief, firmly believe that in the study of this matter, pay and harvest must be proportional to refueling for their own!
"Java Basics"--jsp nine built-in objects