4-1 Built-in JSP objects, standard actions, JavaBean, El

Source: Internet
Author: User
Tags ranges
1. built-in JSP objects

JSP has nine built-in objects: request, response, session, out, page, application, exception, pagecontext, config

1.1 request

The request information of the client is encapsulated in the request object, which can be used to understand the customer's needs and then respond. It is an instance of the httpservletrequest class.

The common method is as follows:

Object getattribute (string name) returns the attribute value of the specified attribute.
Enumeration getattributenames () returns the enumeration of all available attribute names
String getcharacterencoding () returns the character encoding method
Int getcontentlength () returns the length of the Request body (in bytes)
String getcontenttype () Get the MIME type of the Request body
Servletinputstream getinputstream () obtains the binary stream of a row in the Request body.
String getparameter (string name) returns the parameter value of the specified name parameter.
Enumeration getparameternames () returns the enumeration of available parameter names
String [] getparametervalues (string name) returns an array containing all values of the parameter name
String getprotocol () returns the protocol type and version number of the request.
String getscheme () returns the scheduler name used for the request, such as HTTP. HTTPS and FTP.
String getservername () returns the server host name that receives the request
Int getserverport () returns the port number used by the server to accept this request.
Bufferedreader getreader () returns the decoded Request body.
String getremoteaddr () returns the IP address of the client sending this request.
String getremotehost () returns the client host name that sends this request
Void setattribute (string key, object OBJ)
String getrealpath (string path) returns the actual path of a virtual path.

1.2 response

The response object contains information about the response to the customer request, but is rarely used directly in JSP. It is an instance of the httpservletresponse class.

The common method is as follows:

String getcharacterencoding () character encoding used to return the response

Servletoutputstream getoutputstream () returns a binary output stream of the response.

Printwriter getwriter () returns an object that can output characters to the client.

Void setcontentlength (INT Len) set the Response Header Length

Void setcontenttype (string type) sets the MIME type of the response

Sendredirect (Java. Lang. string location) Redirects client requests

1.3 session

A session object refers to a session between the client and the server. It is an httpsession instance from the client to a webapplication on the server until the client is disconnected from the server.

The common method is as follows:

Long getcreationtime () returns the session Creation Time
Public String GETID () returns the unique ID number set for the JSP Engine when the session is created.
Long getlastaccessedtime () returns the last client request time in this session
Int getmaxinactiveinterval () returns the interval between two requests this session is canceled (MS)
String [] getvaluenames () returns an array containing all available attributes of this session.
Void invalidate () cancels the session to make the session unavailable
Boolean isnew () returns a session created by the server, whether the client has been added
Void removevalue (string name) deletes the attribute specified in the session.
Void setmaxinactiveinterval () sets the interval between two requests this session is canceled (MS)

 

1.4 out

An out object is an instance of the jspwriter class and is a common object for outputting content to the client.

The common method is as follows:

Void clear () clears the buffer content
Void clearbuffer () clears the current content of the buffer
Void flush () clears the stream
Int getbuffersize () returns the size of the buffer in 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

 

1.5 page

The page object points to the current JSP page itself, a bit like the this pointer in the class. It is an instance of the Java. Lang. Object Class.

The common method is as follows:

Class getclass returns the class of this object

Int hashcode () returns the hash code of this object

Boolean equals (Object OBJ) determines whether the object is equal to the specified object

Void copy (Object OBJ) copies the object to the specified object.

Object clone () clone this object

String tostring () converts the object to a string object.

Void y () Wake up a waiting thread

Void policyall () Wake up all waiting threads

Void wait (INT timeout) enables a thread to wait until the timeout ends or is awakened.

Void wait () enables a thread to wait until it is awakened.

Void entermonitor () locks the object

Void exitmonitor () locks the object

 

1.6 Application

The Application Object shares data between users and stores global variables. It starts from the start of the server until the server is closed. During this period, this object will always exist. In this way, the object can be connected between users or between users, you can perform operations on the same attribute of this object. Operations on this object attribute anywhere will affect access by other users. The startup and shutdown of the server determine the life of the Application object. It is an instance of the servletcontext class.

Common Methods:

Object getattribute (string name) returns the attribute value to the name
Enumeration getattributenames () returns the enumeration of all available attribute names
Void setattribute (string name, object OBJ)
Void removeattribute (string name) deletes a property and its attribute values.
String getserverinfo () returns the JSP (servlet) engine name and version number.
String getrealpath (string path) returns the actual path of a virtual path.
Servletcontext getcontext (string uripath) returns the Application Object of the specified webapplication.
Int getmajorversion () returns the maximum servlet API version supported by the server.
Int getminorversion () returns the maximum servlet API version supported by the server.
String getmimetype (string file) returns the MIME type of the specified file
URL getresource (string path) returns the URL path of the specified resource (file and directory ).
Inputstream getresourceasstream (string path) returns the input stream of the specified resource
Requestdispatcher getrequestdispatcher (string uripath) returns the requestdispatcher object of the specified resource.
Servlet getservlet (string name) returns the servlet with the specified name
Enumeration getservlets () returns the enumeration of all servlets
Enumeration getservletnames () returns the enumeration of all servlet names
Void log (string MSG) writes the specified message to the servlet Log File
Void log (exception, string MSG) writes the stack track and error message of the specified exception to the servlet Log File
Void log (string MSG, throwable) writes stack tracing and throwable exception descriptions to the servlet log file.

1.7 exception

An exception object is an exception object. When an exception occurs during a page running, this object is generated. To apply this object to a JSP page, you must set iserrorpage to true; otherwise, compilation fails. It is actually a java. Lang. throwable object.

Common Methods:

String getmessage () returns a message describing an exception.
String tostring () returns a brief description message about the exception.
Void printstacktrace () displays exceptions and stack traces
Throwable fillinstacktrace () overwrites the execution stack track of an exception

 

1.8 pagecontext

The pagecontext object provides access to all the objects and namespaces on the JSP page, that is, it can access the session on this page, or take a property value of the application on this page, it is equivalent to the aggregator of all functions on the page. Its Class Name is also called pagecontext.

Common Methods:

Jspwriter getout () returns the jspwriter stream (out) in use in the current client response)
Httpsession getsession () returns the httpsession object (session) on the current page)
Object getpage () returns the object (PAGE) of the current page)
Servletrequest getrequest () returns the servletrequest object (request) of the current page)
Servletresponse getresponse () returns the servletresponse object (response) of the current page)
Exception getexception () returns the exception object (exception) of the current page)
Servletconfig getservletconfig () returns the servletconfig object (config) of the current page)
Servletcontext getservletcontext () returns the servletcontext object (Application) of the current page)
Void setattribute (string name, object attribute) set attributes and attribute values
Void setattribute (string name, object OBJ, int scope) set attributes and attribute values within the specified range
Public object getattribute (string name) gets the attribute value
Object getattribute (string name, int scope) obtains the attribute value within the specified range
Public object findattribute (string name) searches for an attribute and returns the starting attribute value or null.
Void removeattribute (string name) deletes an attribute.
Void removeattribute (string name, int scope) deletes an attribute within a specified range.
Int getattributescope (string name) returns the scope of an attribute.
Enumeration getattributenamesinscope (INT scope) returns an enumeration of available attribute names within a specified range.
Void release () releases resources occupied by pagecontext
Void forward (string relativeurlpath) causes the current page to be re-exported to another page
Void include (string relativeurlpath) contains another file at the current position

 

1.9 config

The Config object is used by the JSP Engine to transmit information to a servlet during initialization. This information includes the parameters used during servlet initialization (consisting of attribute names and attribute values) and server-related information (by passing a servletcontext object ).

Common Methods:

Servletcontext getservletcontext () returns the servletcontext object containing server-related information.
String getinitparameter (string name) returns the value of the initialization parameter.
Enumeration getinitparameternames () returns the enumeration of all parameters required for servlet initialization.

2. jsp standard action

Common standard actions in JSP include: <JSP: usebean>, <JSP: setproperty>, <JSP: getproperty>, <JSP: Include>, and <JSP: forward>, <JSP: param>
These standard actions are directly used in JSP code without introducing files. The following describes them one by one:

2.1 <JSP: usebean>

<JSP: usebean> You can reference A JavaBean class that will be used on the JSP page. The usage is as follows:

<jsp:useBean id="name" class="..." scope="..." type="..."> </jsp:useBean>

Explanation:

  • ID: Set an ID for a JavaBean class and use it as the unique tag of the entire JSP page;
  • Class: the complete Java class name of the JavaBean class to be referenced (the package name must be included ).
  • Type: specifies that the JavaBean class to be referenced needs to implement a Java interface or a super class to be extended by the JavaBean. The type attribute is not required, but not required.
  • Scope: Specifies the scope of use of this JavaBean. This attribute can take one of the four values below: Page, request, session, application; page indicates that the JavaBean is only available on the current page (saved in the pagecontext of the current page); request indicates that the JavaBean is valid in the current customer request (saved in the servletrequest object ); session indicates that the JavaBean is valid for all pages in the current httpsession; application indicates that the JavaBean is valid for the entire application, and the four ranges become larger in turn.
2.2 <JSP: setproperty>

You can use the <JSP: setproperty> action to modify the attribute variable value in the JavaBean instance. You can set the attribute value for the JavaBean instance by using the following method:

<JSP: usebean id = "name" class = "... "Scope = "... "type = "... "> <JSP: setproperty name = "... "Property = "... "value = "... "Or Param = "... "/> </jsp: usebean>

Explanation:

  • Name: required. assign values to the attribute of the JavaBean with the name specified;
  • Property: It must be set to specify which attribute of the JavaBean instance needs to be assigned a value. Here, a special usage is to set the property "*", this setting indicates that all request parameters whose names match the JavaBean attribute are passed to the Set Method of the corresponding attribute for value assignment;
  • Value: This attribute is optional and specifies the value of the JavaBean attribute;
  • Param: This attribute and Value Attribute cannot be used at the same time. Only one attribute can be used between the two. When neither attribute is in <JSP: setproperty
    > When specified in the action, the specified property variable uses the default value in JavaBean (for example, the default value in the constructor). If the param attribute is used, the container will set the property variable specified by property to the value of the request parameter specified by Param.
     
2.3 <JSP: getproperty>

Use <JSP: getproperty> to obtain the specified JavaBean attribute value. The usage is as follows:

<jsp:getProperty name=”...” property=”...”/>

Explanation:

  • Name: Specifies the name of the JavaBean;
  • Property: Specifies the attribute value in the JavaBean.
2.4 <JSP: Include>

You can include the content generated on another page in the current page. The usage is as follows:

<jsp:include page="..." flush="true|false"></jsp:include>

Explanation:

  • Page: the document to insert into the current page
  • Flush: whether to refresh the buffer on the current page
2.5 <JSP: Forward>

To redirect a request to another page, use the following method:

 <jsp:forward page=”url”/>

Page specifies the URL to jump.

2.6 <JSP: param>

<JSP: param> is generally used together with <JSP: Include> and <JSP: Forward> to transmit parameters. The usage is as follows:

<jsp:include page=”...”>        <jsp:param name=”...” value=”...”/>  </jsp:include> 

Explanation:

  • Name: Parameter Name
  • Value: the value of the parameter.
     
3. Concepts and usage of JavaBean

Javabean is a reusable component written in Java and a common Java class.

When writing a Java class, you only need to follow the following rules in the class method naming:

  • If the name of the member variable of the class is XXX, you can use two methods in the class to change or obtain the value of the member variable, that is, to change or obtain the attribute:
    Getxxx () is used to obtain the property XXX.
    Setxxx () is used to modify the property XXX ..
  • For a Boolean member variable, that is, a Boolean logical attribute, "is" can be used to replace the preceding "get" and "set ".
  • The access attribute of methods in the class must be public.
  • If there is a constructor in the class, the constructor is public and has no parameters.

Use the <JSP: usebean/> standard action to Use Javabean on the JSP page.

4 El expression 4.1 el

To simplify JSP writing and simplify JSP pages.

4.2 El usage
  • Syntax structure
    $ {Expression}
  • [] And. Operators
    El provides "." and "[]" operators to access data. When the attribute name to be accessed contains some special characters, such as. Or? If it is not a letter or number, you must use "[]". For example:
    $ {User. My-name} should be changed to $ {user ["My-name"]}
    If you want dynamic values, you can use "[]", but "." cannot achieve dynamic values. For example:
    Data in $ {sessionscope. User [data]} is a variable.
  • Variable
    El is easy to access variable data, for example, $ {username }. It means to retrieve the variable named username in a range. Because we do not specify the username of the range, it will search for the range from page, request, session, and application in sequence. If the username is found on the way, it will be returned directly and will not be found any more, but if all the ranges are not found, null will be returned. Of course, the El expression will be optimized, the page is blank, instead of printing the output null ..
  • Name of attribute range in El
    Page pagination
    Request requestscope
    Session sessionscope
    Application applicationscopee
4.3 commonly used built-in objects in El
  • Pagecontext: Context of the JSP page. It can be used to access JSP implicit objects, such as requests, responses, sessions, outputs, and servletcontext. For example, $ {pagecontext. Response} assigns a value to the response object of the page.
  • Param: map the request parameter name to a single string parameter value (obtained by calling servletrequest. getparameter (string name ). The getparameter (string) method returns a parameter with a specific name. The expression $ {Param. name} is equivalent to request. getparameter (name ).
  • Paramvalues: maps the request parameter name to a numeric array (obtained by calling servletrequest. getparameter (string name ). It is very similar to a Param implicit object, but it retrieves a string array instead of a single value. Expression $ {paramvalues. Name) is equivalent to request. getparamtervalues (name ).
  • Header: map the request header name to a single string header value (obtained by calling servletrequest. getheader (string name ). The expression $ {header. name} is equivalent to request. getheader (name ).
  • Headervalues: maps the request header name to a numeric array (obtained by calling servletrequest. getheaders (string ). It is very similar to the header implicit object. The expression $ {headervalues. name} is equivalent to request. getheadervalues (name ).
  • COOKIE: maps the cookie name to a single cookie object. Client requests sent to the server can obtain one or more cookies. Expression $ {cookie. Name. Value} returns the first cookie value with a specific name. If the request contains multiple cookies with the same name, use the $ {headervalues. name} expression.
  • Initparam: returns a value of the string type, just like servletcontext. getinitparameter (string name.
  • Pagination: obtains the value corresponding to the attribute name in the page range.

  • Requestscope: Get the value corresponding to the attribute name of the Request range.
  • Sessionscope: gets the value corresponding to the attribute name of the session range.
  • Applicationscope: obtains the value corresponding to the attribute name of the application range.

 

Exchange to explore my Sina Weibo: http://weibo.com/tianrui1990

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.