Jsp-jsp Built-in objects

Source: Internet
Author: User
Tags html header stack trace throwable unique id

Introduction to built-in objects

1. The JSP built-in object is a set of objects created by the Web container, and the built-in objects that "do not use the New keyword" can be used. Example: Out

2, JSP nine large built-in objects:

5 Common objects: Out (output), request (requests), response (response), session (Reply), Application (application)

4 infrequently used: page PageContent exception config

Request response mode for Web applications:

User sends request (requests)
Server to User response (response)

One, what is buffer 1, buffer: buffer, so-called buffers is an area of memory, used to save temporary data 2, IO output is the most primitive one byte output, the efficiency is very low. Buffers can be read out of multiple bytes, once again the output, improve efficiency two, out object 1, out object is an instance of the JspWriter class, is to the client (here refers to the browser) output content of the common object. 2. Common methods: · void println () printing characters to the client · void Clear () clears the contents of the buffer. If called after flush, an exception is thrown · void Clearbuffer () also clears the buffer contents, but does not throw an exception after flush · void Flush () outputs the buffer contents to the client · int GetBufferSize () returns the size (in bytes) of the buffer, if no buffer is set to 0 · int getremaining () returns how much of the buffer is remaining available · Boolean Isautoflush () returns whether the buffer is automatically emptied when the buffer is full · void Close () to close the output stream

There are two ways to submit a form: Get and post.
Defined in <form action= "dologin.jsp" name= "LoginForm" method= "Submit by" ></form>1.get: Submit data by URL in clear text mode, The data is "visible" in the URL. The submission data does not exceed "2KB" at most. The security is lower, but the efficiency is higher than the post method. It is suitable to submit data with little data and low security requirements: For example: Search, Query and other functions. 2.post: Encapsulates user-submitted information within the HTML header. It is suitable for submitting user information with large amount of data and high security. Such as: registration, modification, upload and other functions.
Request object (1) Content: The client's requested information is encapsulated in the requests object to understand the customer's needs and then respond. It is an instance of the HttpServletRequest class. The request object has a requesting domain, which is valid until the client's request is completed. (2) Method: 1 Object getattribute (String name) returns the property value of the specified property 2 enumeration Getattributenames () returns an enumeration of all available property names 3 String Getcharacterencoding () returns the character encoding 4 int getcontentlength () returns the length of the request body (in bytes) 5 String getcontenttype () Gets the MIME type of the request body 6 ServletInputStream getInputStream () gets the binary stream of a row in the request body 7 string GetParameter (string name) returns the parameter value of the specified parameter for name 8 enumeration Getparameternames () Returns an enumeration of the available parameter names 9 string[] Getparametervalues (String name) returns an array of all values that contain the name of the parameter, String Getprotocol () Returns the protocol type and version number of the request with a string Getscheme () returns the plan name of the request, such as: Http.https and FTP, etc. string getServerName () returns the server host name that accepts the request Getserverport () returns the port number used by the server to accept this request BufferedReader Getreader () returns the decoded request body, String getremoteaddr () Returns the client IP address that sent the request
Request.setcharacterencoding ("Utf-8");//Solve the Chinese garbled problem, unable to solve the URL to pass the Chinese garbled problem.
Response.setcontenttype ("Text/html;charset=utf-8");//Set the Mimi type of the response
Rrintwriter outer = Response.getwriter (); Get the output stream object
The response PrintWriter object output takes precedence over the JSP built-in out object (PrintWriter is the Java IO package), and the solution out object calls the Flush () method. This will ensure that the out object output is before PrintWriter
Response.sendredirect ("index.jsp");//Request redirection
Difference between request forwarding and request redirection one, request redirection:
Server-side Responce.sendredirect ("xx.jsp") redirection.
"Client Behavior": that is, the client accesses two times, immediately after the first visit, it jumps to the second redirect page, "essentially equals two requests", and the request object encapsulated by the previous requests is not saved, and the URL address of the address bar changes. Second, request to forward:
Service-side Request.getrequestdispatcher ("xx.jsp"). Forward (request,response) request forwarding.
Forward (Request,response) is used to save the built-in object request and response. "Server Behavior": The server will be in place of the client to access the forwarding page, "from the Essence is a request", after forwarding the request object will be saved, the Address bar URL address will not change.

What is a Session1, session represents the client and the server at a time of 2, the web of sessions refers to: the user when browsing a website, from the site to the browser to close the time elapsed, that is, users spend time to browse the site. 3, from the above definition can be seen, the session is actually a "specific time concept" 4, the server's memory, save the same user session.

The 
 (1) session is a built-in object of the JSP and is an instance of the HttpSession class. (2) When the client opens the browser and connects to the server, the client closes the browser window and disconnects from the server, the process becomes a session. (3) When customers switch between different pages of the same site and access, the server is through the session to determine whether these requests from the same customer. (4) The session usually has a time limit, the long time does not operate may cause the session to fail. The attribute values stored in the original session after the session failure are all lost. (5) Setmaxinactiveinterval (int i) This method can directly set the duration of the session, over which the session will be recreated. (in seconds)-------------------------------the usual methods for session objects are: Long GetCreationTime (): Returns the creation time of the session; public String getId (): Returns the unique ID number of the session (this ID was created by the JSP engine when the session was generated) public Object setAttribute (String name,object value): Saves a property in the session as a key-value pair (the property is an object type) public Object GetAttribute (String name): Returns the value of the property of the specified name, if the property of the name does not exist, Returns null) string[] GetValueNames (): Returns an array that contains all the properties available in this session. int Getmaxinactiveinterval (): Returns a time that indicates how long the current session interval will expire (in seconds). 

Session life Cycle 1. Create:
When a client accesses a JSP or servlet for the first time, the server creates a SessionID for the current session, and each time the client sends a request to the server, the SessionID carries the past. This sessionid is checked by the server 2. Activity: ① A new page opened by hyperlinks in a session belongs to the same session ② as long as the current session page is not all closed, the reopened browser window accesses the same project resource when the same session ③ unless all the pages of this session are closed after re-accessing a JSP or S Ervlet will create a new session Note: Note that the original session still exists, but the old one remains on the server, but no more clients will carry it and then hand it over to the server Check 3. Destroy: Three ways ① called Session.invalidate () Method ②session expires (timed out) ③ server restarts

The Tomcat default session time-out is 30 minutes. 1, Session.setmaxinactiveinterval (time);//units are seconds 2, <session-config> <session-timeout> 10//units for minutes </ Session-timeout> </session-config>

 Application object: 1, Application object to achieve the sharing of data between users, can hold global variables, in javase equivalent to the global variable 2, application starts at the server start, terminates at the server shutdown.  3, in the user's front-and-back connection or the connection between different users, you can operate on the same property of the Application object.  4, the operation of the Application object property anywhere will affect the other user's access to it.  5, the start and shutdown of the server determines the life of the Application object. 6. The Application object is an instance of the ServletContext class.  Common methods: 1, public void SetAttribute (String name,object value) binds an object to this session using the specified name.  2, public object GetAttribute (String name) returns the objects that are bound together by the specified name in this session, and returns null if no object is bound under that name.  3, enumeration Getattributename () returns an enumeration of all available property names. 4, String Getserverinfo () returns the JSP (servlet) engine name and version number. 

The 
 Page Object Page object is a pointer to the current JSP page itself, a bit like this in the class, which is an instance of the Java.lang.object class.  The common methods are as follows: 1, class GetClass () returns the classes of this object.  2, int hashcode () returns the hash code of object.  3, Boolean equals (object obj) to determine whether this object is equal to the specified object objects.  4. void copy (object obj) copies this object to the specified object.  5. Object Clone () clones this object.  6. String toString () converts the object to the object of the String class.  7, void Notify () wakes up a waiting thread.  8, void Notifyall () wakes all waiting threads.  9, void Wait (int timeout) causes a thread to wait until timeout ends or is awakened. 10. void Wait () causes a thread to wait until it wakes up. 

PageContext Object (1) The PageContext object provides access to all objects and namespaces in the JSP page (2) The PageContext object can access the session of this page, Any property value of the Application object that can access this page (3) The PageContext object is a common method of PageContext objects for all features in a page: (1) jspwriter getout () Returns the current client corresponding to the JspWriter stream (out) (2) HttpSession getsession () returns the current Page HttpSession object (session) (3) object GetPage () Returns the object object of the current page (page) (4) ServletRequest getrequest () returns when the previous ServletRequest object (Request) (5) Servletresponse GetResponse () returns the current page of the Servletresponse object (response) (6) void SetAttribute (strign name, object attribute) setting properties and property values (7) object GetAttribute (string name, int scope) takes the property value in the specified range (8) int Getattributescope (string name) returns the scope of a property (9) void Forward (string Relativeurlpath) make the current page jump to another page (server forwarding) void include (String relativeurlpath) contains another file at the current location
The Config object is an instance of the Javax.servlet.ServletConfig class. This object allows the developer to access the initialization parameters of the servlet or JSP engine, such as the file path.

ServletContext Getservletcontext () returns a ServletContext object containing information about the server string Getinitparameter (string name) Returns the value of the initialization parameter enumeration Getinitparameternames () returns an enumeration of all parameters required by the servlet initialization

1. Need to catch the exception of the page, write the property in the page directive errorpage= "error jumps to the page" 2. In the error handling page of the jump to enter the property iserrorpage= "true" JSP built-in object- Exception (1) Exception is an instance of the Java.lang.Throwable Class (2) The Exception object is generated when an exception occurs on a page run. (3) If a JSP page is to use the exception object, You need to set the value of Iserrorpage to true--------------------------------------common methods for exception objects: (1) String getMessage () Returns information describing the exception (2) String toString () returns a short description of the exception (3) void Printstacktrace () display exception and its stack trace (4) throwable fillinstacktrace () Rewrite the exception's execution stack trajectory


http://www.imooc.com/video/3604

Jsp-jsp Built-in objects

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.