A detailed description of nine built-in objects in JSP

Source: Internet
Author: User
Tags session id stack trace throwable unique id

1. Request Object

The client's request information is encapsulated in the requests object to understand the customer's needs and respond. It is an instance of the HttpServletRequest class.

Ordinal method Description
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 method
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 name specified parameter
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 parameter name
Ten String Getprotocol () returns the protocol type and version number of the request
One String Getscheme () returns the plan name of the request, such as: Http.https and FTP, etc.
String getServerName () returns the host name of the server that accepts 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 that sent this request
String Getremotehost () returns the host name of the client that sent this request
$ void SetAttribute (String key,object obj) to set property values for a property
String Getrealpath (string path) returns the true path of a virtual path
19
20

<%@ page contenttype= "text/html;charset=gb2312"%>
<%request.setcharacterencoding ("gb2312");%>
<title>request Object _ Example 1</title>
<body bgcolor= "#FFFFF0" >
<form action= "" method= "POST" >
<input type= "text" name= "Qwe" >
<input type= "Submit" value= "Submission" >
</form>
Request method: <%=request.getmethod ()%><br>
Requested resource: <%=request.getrequesturi ()%><br>
Requested protocol: <%=request.getprotocol ()%><br>
Requested file name: <%=request.getservletpath ()%><br>
Ip:<%=request.getservername ()%><br> of the requested server
Port of the requesting server: <%=request.getserverport ()%><br>
Client IP Address: <%=request.getremoteaddr ()%><br>
Client host Name: <%=request.getremotehost ()%><br>
Value submitted by Form: <%=request.getparameter ("Qwe")%><br>
</body>
<%@ page contenttype= "text/html;charset=gb2312"%>
<%request.setcharacterencoding ("gb2312");%>
<%@ page import= "Java.util.Enumeration"%>
<title>request Object _ Example 2</title>
<body bgcolor= "#FFFFF0" >
<form action= "" method= "POST" >
User name: <input type= "text" name= "username" >&nbsp;&nbsp;
Password: <input type= "text" name= "Userpass" >&nbsp;&nbsp;
<input type= "Submit" value= "enter" >
</form>
<%
String str= "";
if (Request.getparameter ("username")!=null && request.getparameter ("Userpass")!=null) {
Enumeration enumt = Request.getparameternames ();
while (Enumt.hasmoreelements ()) {
Str=enumt.nextelement (). toString ();
Out.println (str+ ":" +request.getparameter (str) + "<br>");
}
}
%>
</body>
<%@ page contenttype= "text/html;charset=gb2312"%>
<%request.setcharacterencoding ("gb2312");%>
<title>request Object _ Example 3</title>
<body bgcolor= "#FFFFF0" >
<form action= "" method= "POST" >
Good: <input type= "checkbox" Name= "CB" value= "ON1" >VC++&nbsp;
<input type= "checkbox" Name= "CB" value= "ON2" >JAVA&nbsp;
<input type= "checkbox" Name= "CB" value= "ON3" >DELPHI&nbsp;
<input type= "checkbox" Name= "CB" value= "ON4" >VB&nbsp;
<br>
<input type= "Submit" value= "enter" Name= "Qwe" >
</form>
<%
if (Request.getparameter ("Qwe")!=null) {
for (int i=0;i<request.getparametervalues ("CB"). length;i++) {
OUT.PRINTLN ("CB" +i+ ":" +request.getparametervalues ("CB") [i]+ "<br>");
}
Out.println (Request.getparameter ("qwe"));
}
%>
</body>

2. Response Object

The Response object contains information about the response to a customer request, but it is seldom used directly in the JSP. It is an instance of the HttpServletResponse class.

Ordinal method Description
1 String getcharacterencoding () returns what character encoding is used for the response
2 Servletoutputstream Getoutputstream () returns a binary output stream of the response
3 PrintWriter getwriter () returns an object that can output characters to the client
4 void setcontentlength (int len) Set response header length
5 void setContentType (String type) sets the MIME type of the response
6 Sendredirect (java.lang.String location) REDIRECT client request
7
8

3. Session Object

The session object refers to a client-to-server conversation, starting with a webapplication from the client to the server until the client disconnects from the server. It is an instance of the HttpSession class.

Ordinal method Description
1 long GetCreationTime () returns session creation time
2 public String GetId () returns the unique ID number that the JSP engine sets for the session when it is created
3 Long Getlastaccessedtime () returns the client's last request time in this session
4 int Getmaxinactiveinterval () returns two request interval how long this session was canceled (MS)
5 string[] GetValueNames () returns an array containing all the properties available in this session
6 void Invalidate () Cancel session to make session unavailable
7 Boolean isnew () returns a session created by the server, whether the client has joined
8 void RemoveValue (String name) removes the attribute specified in session
9 void Setmaxinactiveinterval () set two request interval how long this session is canceled (MS)
10
11
12
13
14
15

<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.util.*"%>
<body><br>
Session creation Time: <%=session.getcreationtime ()%>&nbsp;&nbsp;& lt;%=new Date (session.getcreationtime ())%><br><br>
Session ID Number: <%=session.getid ()%><br><br>
Client last Request time: <%=session.getlastaccessedtime ()%>&nbsp;& nbsp;<%=new java.sql. Time (Session.getlastaccessedtime ())%><br><br>
Two requests interval how long this session is canceled (MS): <%=session.getmaxinactiveinterval ()%><br><br>
is a newly created session:<%=session.isnew ()? " Yes ":" No "%><br><br>
<%
Session.putvalue ("name", "Lin Yuan Programming");
Session.putvalue ("Nmber", "147369");
%>
<%
for (int i=0;i<session.getvaluenames (). length;i++)
Out.println (Session.getvaluenames () [i]+ "=" +session.getvalue (session.getvaluenames () [i]));
%>
<!--returned from 0:00:00 GMT (GMT) January 01, 1970 to calculate the millisecond of the time--
</body>

4. Out Object

An Out object is an instance of the JspWriter class and is an object commonly used to output content to the client

Ordinal method Description
1 void Clear () clears the contents of the buffer
2 void Clearbuffer () clears the current contents of the buffer
3 void Flush () empty stream
4 int GetBufferSize () returns the buffer as the size of the number of bytes, or 0 if no buffer is set
5 int getremaining () returns how much of the buffer is remaining available
6 Boolean Isautoflush () returns when the buffer is full, automatically empties or throws an exception
7 void Close () close the output stream
8
9
10
11
12
13
14
15

<% @page contenttype= "text/html;charset=gb2312"%>
<% @page buffer= "1KB"%>
<body>
<%
for (int i=0;i<2000;i++)
Out.println (i+ "{" +out.getremaining () + "}");
%><br>
Cache size: <%=out.getbuffersize ()%><br>
Remaining Cache Size: <%=out.getremaining ()%><br>
Auto Refresh: <%=out.isautoflush ()%><br>
<%--out.clearbuffer ();--%>
<%--out.clear ();--%>
<!--By default: The server output to the client's content, not directly to the client, but first write to an output buffer. The contents of the buffer are output to the client only in the following three scenarios:
1. Output of completed information for this JSP page
2. The output buffer is full
Out.flush () or Response.flushbuffer () is called in 3.JSP
-
</body>

5. Page Object

The Page object is pointing to the current JSP page itself, a bit like the this pointer in the class, which is an instance of the Java.lang.Object class

Ordinal method Description
1 class GetClass Returns the classes of this object
2 int hashcode () returns the hash code of this 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 () to clone this object
6 string toString () converts this object to the object of the String class
7 void Notify () wakes up a waiting thread
8 void Notifyall () wakes up all waiting threads
9 void Wait (int timeout) causes a thread to wait until timeout ends or is awakened
Ten void Wait () causes a thread to wait until it wakes up
one void Entermonitor () lock on object
void Exitmonitor () to unlock object
13
14
15

6. Application Object

The Application object implements the sharing of data among users and can store global variables. It starts at the start of the server until the server shuts down, during which time this object will persist, so that the same properties of the object can be manipulated on the user's back-and-forth connection or in a connection between different users, and the operation of this object property anywhere will affect access to it by other users. The startup and shutdown of the server determines the life of the Application object. It is an instance of the ServletContext class.

Ordinal method Description
1 Object getattribute (String name) returns the property value for the given name
2 enumeration Getattributenames () returns an enumeration of all available property names
3 void SetAttribute (String name,object obj) sets the property value of the property
4 void RemoveAttribute (String name) deletes an attribute and its property value
5 String Getserverinfo () returns the JSP (SERVLET) engine name and version number
6 string Getrealpath (string path) returns the true path of a virtual path
7 ServletContext getcontext (String uripath) returns the Application object for the specified WebApplication
8 int Getmajorversion () returns the maximum version number of the Servlet API supported by the server
9 int getminorversion () returns the maximum version number of the Servlet API supported by the server
Ten string GetMimeType (string file) returns the MIME type of the specified file
One-by-one URL getresource (String path) returns the URL path for the specified resource (file and directory)
InputStream getResourceAsStream (String path) returns the input stream for the specified resource
RequestDispatcher Getrequestdispatcher (String uripath) returns the RequestDispatcher object for the specified resource
Getservlet servlet (String name) returns the servlet of the specified name
Enumeration Getservlets () returns an enumeration of all Servlets
Enumeration Getservletnames () returns an enumeration of all servlet names
$ void log (String msg) writes the specified message to the servlet's log file
void log (Exception exception,string msg) writes the stack trace and error message of the specified exception to the servlet's log file
void log (String msg,throwable throwable) writes the description information of the stack trajectory and the given Throwable exception to the servlet's log file
20

<%@ page contenttype= "text/html;charset=gb2312"%>
<body><br>
JSP (SERVLET) engine name and version number: <%=application.getserverinfo ()%><br><br>
Returns the true path of the/application1.jsp virtual path: <%=application.getrealpath ("/application1.jsp")%><br><br>
Large version number of the Servlet API supported by the server: <%=application.getmajorversion ()%><br><br>
Minor version number of the Servlet API supported by the server: <%=application.getminorversion ()%><br><br>
Specify the URL path for the resource (file and directory): <%=application.getresource ("/application1.jsp")%& gt;<br><br><!-- You can change the application1.jsp to a directory--
<br><br>
<%
Application.setattribute ("name", "Lin yuan computer programming technology Training School");
Out.println (Application.getattribute ("name"));
Application.removeattribute ("name");
Out.println (Application.getattribute ("name"));
%>
</body>
<%@ page contenttype= "text/html;charset=gb2312"%>
<body><br>
<!--because the application is always present on the server side, you can use this feature to count pages--
<%
if (Application.getattribute ("Count") ==null)
Application.setattribute ("Count", "1");
Else
Application.setattribute ("Count", Integer.tostring (integer.valueof (Application.getattribute ("Count"). ToString () ). Intvalue () +1));
%>
You are <%=application.getattribute ("Count")%> bit visitor
</body>
<!--because the GetAttribute () method gets an object of type objects, the GetString () method is used to convert to type string--
<!--use the valueof () method of the integer class to convert the resulting string into an integer object, using the Intvalue () method to get the int type, plus 1, and finally the result of the calculation with integer.tostring () Method into the string type required by the setattribute () method--
<%@ page contenttype= "text/html;charset=gb2312"%>
<body><br>
<!--because the application is always present on the server side, you can use this feature to count pages--
<%
String Str=application.getattribute ("Count"). ToString ();//getattribute ("Count") returns the type of object
int i=0;
if (str==null)
Application.setattribute ("Count", "1");
Else
I=integer.parseint (str); Out.println (i);
Application.setattribute ("Count", ++i+ "");
%>
You are <%=application.getattribute ("Count")%> bit visitor
</body>

7. Exception Object

The exception object is an exception object that is created when a page has an exception during the run. If a JSP page is to be applied to this object, the Iserrorpage must be set to true, otherwise it cannot be compiled. He's actually a java.lang.Throwable object.

Ordinal method Description
1 String getMessage () returns a message describing the exception
2 String toString () returns a short description message about the exception
3 void Printstacktrace () shows anomalies and their stack traces
4 throwable fillinstacktrace () Rewrite the exception's execution stack trajectory
5

8. PageContext Object

The PageContext object provides access to all objects and namespaces within the JSP page, which means that he can access the session where the page is located, or a property value of the application that is located on the pages, which is the equivalent of all the functions in the page. The class name is also called PageContext.

Ordinal method Description
1 JspWriter getout () returns the current client response used by the JspWriter stream (out)
2 HttpSession getsession () returns the HttpSession object (session) in the current page
3 Object GetPage () returns the current page of Object Objects (page)
4 ServletRequest Getrequest () returns the ServletRequest object of the current page (request)
5 Servletresponse GetResponse () returns the Servletresponse object of the current page (response)
6 Exception GetException () returns the Exception object of the current page (Exception)
7 ServletConfig Getservletconfig () returns the ServletConfig object (config) of the current page
8 ServletContext Getservletcontext () returns the ServletContext object of the current page (application)
9 void SetAttribute (String name,object attribute) set properties and property values
void SetAttribute (String name,object obj,int scope) sets properties and property values within a specified range
One public Object getattribute (String name) takes the value of the property
Object getattribute (String name,int scope) takes the value of the property within the specified range
Public Object Findattribute (String name) looks for a property that returns the property value or null
+ void RemoveAttribute (String name) to delete a property
void RemoveAttribute (String name,int scope) Deletes a property at the specified range
Getattributescope Int (String name) returns the scope of a property
Enumeration Getattributenamesinscope (int scope) Returns the property name enumeration available within the specified range
void release () releases the resources occupied by PageContext
Forward (String Relativeurlpath) to redirect the current page to another page
void include (String relativeurlpath) contains another file at the current location
21st

<% @page contenttype= "text/html;charset=gb2312"%>
<body><br>
<%
Request.setattribute ("name", "Lin Yuan Programming");
Session.setattribute ("name", "Lin yuan computer Programming technical training");
Session.putvalue ("name", "Computer Programming");
Application.setattribute ("name", "training");
%>
Request Set Value: <%=pagecontext.getrequest (). getattribute ("name")%><br>
Session Set Value: <%=pagecontext.getsession (). getattribute ("name")%><br>
Application Set Value: <%=pagecontext.getservletcontext (). getattribute ("name")%><br>
Value within range 1: <%=pagecontext.getattribute ("name", 1)%><br>
Value within range 2: <%=pagecontext.getattribute ("name", 2)%><br>
Value within range 3: <%=pagecontext.getattribute ("name", 3)%><br>
Value within range 4: <%=pagecontext.getattribute ("name", 4)%><br>
<!--start with the smallest range page, then the Reques, session, and Application-->
<%pagecontext.removeattribute ("name", 3);%>
PageContext the modified session setting Value: <%=session.getvalue ("name")%><br>
<%pagecontext.setattribute ("name", "Applied Technology Training", 4);%>
PageContext modified Application Set Value: <%=pagecontext.getservletcontext (). getattribute ("name")%><br>
Lookup of the value: <%=pagecontext.findattribute ("name")%><br>
Property name Range: <%=pagecontext.getattributesscope ("name")%><br>
</body>

9. config Object

The Config object is used by the JSP engine to pass information to it when a servlet is initialized, including the parameters to be used when the servlet initializes (through property names and property values) and information about the server (by passing a ServletContext object)

Ordinal method Description
1 ServletContext getservletcontext () returns ServletContext object containing information about the server
2 string Getinitparameter (string name) returns the value of the initialization parameter
3 enumeration Getinitparameternames () returns an enumeration of all parameters required by the servlet initialization
4
5

A detailed description of nine built-in objects in JSP

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.