some objects, such as ServletContext HttpSession, are frequently used in JSP development. PageContext and so on. If we need to use these objects in our JSP pages every time we create them ourselves, it will be very tedious. Sun company therefore, when designing the JSP, it automatically helps the developer to create these objects after the JSP page is loaded. Developers only need to use the appropriate object to invoke the appropriate method. These systems create good objects called built-in objects.
I. Nine large built-in objects
1. Built-in object/scope:
1, application------global scope
2, session------Sessions Scope
3, request------ scope
4, pagecontext------page Scope//context: Environment, Context
built-in objects:
5, Response------Response Object
6, out------The output stream object, the Main method: Out.println (a); the effect is equivalent to <%=a%>
7, page------An instance of the current Page object
8, exception------exception object
9, config------Configuration object, mainly divided into Servletconfig/filterconfig
2, priority (according to the length of life cycle):
Application > Session > Request > PageContext
3. How form forms are submitted on the page:
Post: Submit content is not visible, no length limit
get: There is a length limit and submissions are visible in the address bar
two. Resolve nine built-in objects
1.request Objects
The request object is an instance of the Javax.servlet.httpServletRequest class that represents the client's requested information and is primarily used to accept data transmitted to the server via the HTTP protocol, including header information, System information. Request and request parameters, etc.), the life cycle of the object is a single request.
Common method: Request.getparameter (string name)---Get the value of the request parameter name, the return type is String type;
request.getparametervalues (String name)---Gets the array with the request parameter of name, the return type is string[] type, similar to the check box;
request.setattribute (string,object)---Set the properties in the request scope;
request.getattribute (String)---Gets the property in the request scope, the return type is object type;
Request.getcontextpath ()---get the context path; example:/Project name
Request.getrealpath ("")---Gets the physical path that is passed in to the path, starting with the disk;
request.getremoteaddr ()---Get the IP address of the client sending this request; Example: 0:0:0:0:0:0:0:1
request.getservername ()---Get the host name of the requesting server: localhost
request.getserverport ()---Gets the port number of the server; Example: 8081
Request.getprotocol ()---Gets the protocol type and version number used for the request; Example: http/1.1
request.getcontentlength ()---Gets the length of the request body, in bytes; Example: -1
Request.getcontenttype ()---Gets the MIME type of the request body;
solve the problem of Chinese garbled characters in Request
Post mode: set by Using request.setcharacterencoding ("Utf-8") (Use this method more)
Get mode: set by modifying the Server.xml configuration file in the Tomcat server:
<connector connectiontimeout= "20000" port= "8080" protocol= "http/1.1" redirectport= "8443" URIEncoding= "Utf-8" />
2.response Objects
is an instance of the HttpServletResponse class that corresponds to the client, whose scope is valid only on the JSP page
Common methods: response.setcharactorencording ("Utf-8")---Set the corresponding page character encoding;
Response.setcontenttype ("Text/html;charset:utf-8")---set MIME type request header;
response.getwriter (). Append (String)---Output a string to the page;
3.out Objects
can be obtained by pagecontext.getout () for output information
the difference between Out.print and Response.getwriter ():
Response.getwriter (): Is the PrintWriter class (inherits from the writer Class), can directly output the result, takes precedence over the Out object, does not throw the exception;
Out object: is the JspWriter Class (abstract class), the content needs to be stored in the buffer, when the buffer is full or reached a certain condition before the output method will be called to output the content, you can use the Out.flush () method to refresh the buffer content in advance output, will throw an exception;
4.application Global Scope
is an instance of the ServletContext class, whose action period starts from the beginning of the server to the end, similar to the global variables of the system;
Common methods: SetAttribute (String, Object)---setting a property and property value
getattribute (String)---Get the value of a property
getattributenames ()---gets all the property names in the Application object
5.session Objects
is an instance of the HttpSession class, when the user first accesses a JSP or servlet, the server creates a session for the current access, tracks the user's operation status, and generates a SessionID. Each time a client sends a request to the server, the SessionID is passed over for verification.
life cycle: If the user does not perform various activities on the page, such as links, additional deletions, etc., or the browser is closed. Session in the system default 30 minutes after the automatic invalidation, at this time again access will create a new session, but the original session will still exist, but there is no request again with the old SessionID to let the server to verify.
Common method: Long GetCreationTime ()---Returns the time the Session object was created
String getId ()---Returns a sessionid that is unique
void SetAttribute ()---Set a property and a property value
Object getattribute ()---Gets the property value of a property
string[] getvaluenames ()---Gets the property names of all the available properties in the session object as an array
setmaxinactiveinterval (int)---Set how long the session expires (default 30 minutes)
getmaxinactiveinterval ()---Gets the expiration time (in seconds) of the session
3 Ways to destroy a session:
1. Call the Session.invalidate () method;//invalidate: Invalidate
2, the session itself expired;
setting the session expiration time can also be set in Web. XML
<session-config>
<session-timeout>
10<!--units are minutes --
</session-timeout>
</session-config>
3, the server restarts;
6.config Objects
The Config object is used by the JSP engine to pass information to it when a servlet is initialized. This information includes parameters to be used when the servlet initializes (through property names and property values) and information about the server (by passing a ServletContext object) the primary role of the Config object is to obtain the server's 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. Provides initialization parameters for servlet programs and JSP pages in the application environment in the Web. xml file.
the Web. xml file in Web-inf (the file cannot be accessed directly and is more secure) is loaded after the server is opened
Example: This parameter will be saved in the application scope
<context-param>
<param-name>admin</param-name>
<param-value>1234</param-value>
</context-param>
7.pageContext Objects
the function of the PageContext object is to obtain any range of parameters, through which 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.
8.page Objects
The Page object represents the JSP itself and is only legal within the JSP page. The page implied object essentially contains the variables referenced by the current servlet interface, similar to the this pointer in Java programming.
9.exception Objects
The purpose of the exception object is to display exception information that can be used only on pages that contain iserrorpage= "true", which will not compile JSP files in a generic JSP page. The Excepation object, like all objects in Java, has a system-provided inheritance structure. The exception object almost defines all exception conditions. In Java programs, you can use the Try/catch keyword to handle exceptions, and if an exception is not caught in the JSP page, the exception object is generated and the exception object is routed to the error page set in the page directive. The corresponding exception object is then processed in the error page.
JAVA Web Foundation 2-jsp nine built-in objects