JSP built-in objects and JSP built-in objects

Source: Internet
Author: User

JSP built-in objects and JSP built-in objects

Concept:

JSP built-in objects are a set of objects created by Web containers. They can be directly used without the new keyword.

Common JSP built-in objects:

Out object

Buffer-Buffer, a block of memory, is used to save temporary data.

An out object is an instance of the JSPWriter class and is often used to output content to the client.

Common out object methods include:

  • Void println () prints a string to the client;
  • Void flush () to output the buffer content to the client;
  • Void clear () clears the buffer. If it is called after flush, an exception is thrown;
  • Void clearBuffer () clears the content of the buffer. After being flushed, the call will not throw an exception;
  • Int getBufferSize () returns the size of the number of buffer bytes. If no buffer is set, the value is 0;
  • Int getRemaining () returns how much available the buffer is;
  • Boolean isAutoFlush () indicates whether to automatically clear or throw an exception when the returned buffer is full;
  • Void close () to close the output stream

Difference between get and post

Two forms can be submitted: get and post.

Submission Method Method of submitting content Data size Security Efficiency Applicability
Get Plain text, submit data through URL (data can be seen in the URL) Up to 2 kb Relatively low High Suitable for submitting data with a small amount of data and low security. For example, search, query, and other functions.
Post Encapsulate the information submitted by the user in the HTML HEADER Large data volume High   It is suitable for submitting user information with large data volumes and high security. For example, registration, modification, upload, and other functions.

 

Request object

The request information of the client is encapsulated in the request object, so that the client can understand the customer's requirements and then respond.

The request object is an instance of the HttpServletRequest class.

The request object has a request domain, that is, the object remains valid until the client completes the request.

Common methods include:

  • String getParameter (String name) returns the value of the specified parameter;
  • String [] getParameterValues (String name) returns an array containing all the parameter values of the specified parameter;
  • Void setAttribute (String, Object) stores the attributes in this request;
  • Object getAttribute (String name) returns the attribute value of the specified attribute;
  • String getContentType () to obtain the MIME type of the Request body;
  • Stirng getProtocol () returns the request protocol type and version number;
  • String getServerName () returns the Host Name of the server that receives the request;
  • Int getServerPort () returns the port number used by the server to receive the request;
  • String getCharacterEncoding () returns the character encoding method;
  • Int getContentLength () returns the length of the Request body (number of bytes );
  • String getRemoteAddr () returns the IP address of the client sending the request;
  • String getRealPath (String path) returns the actual path of a virtual path;
  • String getContentPath () returns the context path.

Response object

The response object contains information about the response to the customer request, which is rarely used directly in JSP.

The response object is an instance of the HttpServletResponse class.

The response object has a page scope, that is, when accessing a page, the response object in the page can only be valid for this access, and the response object of other pages is invalid for the current page.

Common Methods:

  • String getCharacterEncoding () returns the character encoding of the response;
  • Void setContentType (String type) sets the MIME type of the response;
  • PrintWriter getWriter () returns an object that can output characters to the client. (The output stream object generated by the response object appears before the out Built-in object)
  • SendRedirect (java. lang. String location) Redirects client requests.

Request forwarding VS request redirection

Method Client/Server Method Description URL content
Request redirection Client Behavior Response. sendRedirect () Essentially equivalent to two requests, the previous request object will not be saved URL change
Request forwarding Server Behavior Request. getRequestDispatcher (). forward (req, resp) One request. After forwarding, the request object will be saved. URL unchanged

Session Object

Session: A session between the client and the server

Session in the Web: the time that a user spends browsing a website, from entering the website to closing the browser.

Session is actually a specific concept of time.

The session object is automatically created when the first JSP page is loaded to start session management.

One session: starts when the client opens the browser and connects to the server, and ends when the client closes the browser and leaves the server.

Session object, which is an instance of the HttpSession class.

Common Methods:

 

Application Object

 

Page Object

PageContext object

Exception object

Config object

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.