JSP nine kinds of built-in objects: request, reponse, out, session, Application, config, PageContext, page, exception.
A. Request object: The object encapsulates the information submitted by the user, and it is possible to obtain the encapsulated information by invoking the object's corresponding method, that is, the object can be used to obtain user submissions.
A 1.Request object can use the GetParameter (string s) method to obtain information submitted by the form through text. Such as:
Request.getparameter ("Boy")
2, use the Request object to get information to be extra careful, to avoid the use of empty objects, otherwise there will be nullpointerexception (null pointer) exception, so to catch exceptions, do exception handling.
String textcontent=request.getparameter ("Girl");
Double number=0,r=0;
if (textcontent==null)
{textcontent= "";
}
try{number=double.parsedouble (textcontent);
if (number>=0)
{r=math.sqrt (number);
Out.print ("<BR>" +string.valueof (number) + "square root:");
Out.print ("<BR>" +string.valueof (r));
}
Else
{Out.print ("<BR>" + "Please enter a positive number");
}
}
catch (NumberFormatException e)
{Out.print ("<BR>" + "Please enter numeric characters");
}
3, processing Chinese characters information: When the Request object to obtain customer submitted characters, will appear disorderly horse problem, must carry out special treatment, will get the string with Iso-8859-1 for hard transcoding.
String name1=request.getparameter ("name");
String Name=new string (name1.getbytes ("iso-8859-1"), "UTF-8");
4. Examples of common methods:
Getprotocol (), Getservletpath (), Getcontentlength (), GetMethod (), getremoteaddr (), Getremotehost (), GetServerName (), Getparametername ()
The client uses a protocol that:
String Protocol=request.getprotocol ();
Get the page that accepts customer submissions:
String Path=request.getservletpath ();
Accept the length of customer submission information:
int Length=request.getcontentlength ();
How the customer submits the information:
String Method=request.getmethod ();
To get the value of User-agent in the HTTP header file::
String Header1=request.getheader ("user-agent");
Gets the value of the accept in the HTTP header file:
String Header2=request.getheader ("accept");
Gets the value of the host in the HTTP header file:
String Header3=request.getheader ("Host");
Gets the value of the accept-encoding in the HTTP header file:
String Header4=request.getheader ("accept-encoding");
To obtain the customer's IP address:
String ip=request.getremoteaddr ();
Get the name of the client:
String Clientname=request.getremotehost ();
Get the name of the server:
String Servername=request.getservername ();
To get the port number of the server:
int Serverport=request.getserverport ();
Gets the name of all parameters submitted by the client:
Enumeration Enum=request.getparameternames ();
while (Enum.hasmoreelements ())
{string s= (String) enum.nextelement ();
Out.println (s);
}
Gets an enumeration of header names:
Enumeration Enum_headed=request.getheadernames ();
while (Enum_headed.hasmoreelements ())
{string s= (String) enum_headed.nextelement ();
Out.println (s);
}
Gets an enumeration of the full value of the specified header name in the header file:
Enumeration Enum_headedvalues=request.getheaders ("Cookie");
while (Enum_headedvalues.hasmoreelements ())
{string s= (String) enum_headedvalues.nextelement ();
Out.println (s);
}
5. Forward Request page
String Strname=request. GetParameter ("name");
if ("Tom". Equals (StrName))
{
Request.getrequestdispatcher ("index.jsp"). Forward (Request,response);
}
6. Save Context Request Object
String Strname=request. GetParameter ("name");/Get request parameter
Studentservice service=new Studentservice ();//Initialize Object
Students Stu=service.islogin (strName);//Calling method
Request.setattribute ("MyName", Stu);//Save context Request object to request
Two. Response objects: Dynamic response to customer requests, sending data to the client.
When a user accesses a JSP page, if the page sets the page's ContentType property to be text/html, the JSP engine responds to this property value. If you want to dynamically change this property value to respond to the customer, you need to use the setContentType (String s) method of the response object to change the ContentType property value.
Format: Response.setcontenttype (String s)
Parameter s are preferable to text/html, Application/x-msexcel, Application/msword, etc.
<form action= "" method= "get" name=form>
<input type= "Submit" value= "yes" name= "submit" >
</FORM>
<% String Str=request.getparameter ("submit");
if (str==null)
{str= "";
}
if (str.equals ("yes"))
{Response.setcontenttype ("application/msword;charset=gb2312");
}
%>
2. Response Redirect: In some cases, when responding to a customer, you need to reboot the customer to another page, and you can use the response Sendredirect (URL) method to achieve customer redirection.
<%
String address = Request.getparameter ("where");
if (address!=null) {
if (Address.equals ("Chinawebber"))
Response.sendredirect ("http://www.ChinaWebber.com");
else if (address.equals ("Yahoo"))
Response.sendredirect ("http://www.yahoo.com");
else if (address.equals ("Sun"))
Response.sendredirect ("http://www.sun.com");
}
%>
Three Session Object
1. What is a Session:session object is a JSP built-in object that is automatically created when the first JSP page is loaded and completes session management.
From a customer to open the browser and connect to the server to start, to the customer close the browser to leave the server end, is called a session. When a client accesses a server, it may be repeatedly connected between several pages of the server, repeatedly refreshing a page, the server should somehow know that this is the same customer, this requires the session object.
2. ID of the Session object: When a customer first accesses a JSP page on a server, the JSP engine produces a sessions object and assigns a string ID number, which the JSP engine sends to the client at the same time, stored in a cookie, so A one by one correspondence between the session object and the customer is established. When a customer accesses another page that connects to the server, it is no longer assigned to the customer's new session object until the client closes the browser, and the client's sessions object is canceled on the server side, and the client's conversation relationship disappears. When the client reopened the browser and then connected to the server, the server creates a new session object for the client.
3. Common methods for session objects:
(1). Public String getId (): Gets the Session object number.
(2). public void setattribute (String key,object obj): Adds the object obj specified by the parameter object to the session object and assigns an index keyword to the added object.
(3). public object GetAttribute (String key): Gets the object that contains the keyword in the session object.
(4). Public Boolean isnew (): Determine if it is a new customer.
Four Aplication objects
1. What is application:
This Application object is generated when the server is started, and the Application object is the same until the server is shut down when the customer browses through the pages of the site visited. But unlike the session, all customers have the same application object, that is, all customers share this built-in application object.
2. Common methods for application objects:
(1) public void setattribute (String key,object obj): Adds the object obj specified by the parameter object to the Application object and assigns an index keyword to the added object.
(2) Public object GetAttribute (String key): Gets the object that contains the keyword in the Application object.
Five Out objects
An Out object is an output stream that is used to output data to the client. The Out object is used for the output of various data.
Common methods:
(1) out.print (): Output various types of data.
(2) Out.newline (): Output a line feed character.
(3) Out.close (): Close the stream.
<%
Date now = new Date ();
String hours=string.valueof (Now.gethours ());
String mins=string.valueof (Now.getminutes ());
String secs=string.valueof (Now.getseconds ());
%>
is now
<%out.print (String.valueof (Now.gethours ());%>
Hours
<%out.print (String.valueof (Now.getminutes ());%>
Part
<%out.print (String.valueof (Now.getseconds ());%>
Seconds
Six Cookies
1. What is a cookie:
A cookie is a piece of text that the Web server saves on a user's hard disk. Cookies allow a Web site to save information on a user's computer and then retrieve it.
For example, a Web site might produce a unique ID for each visitor, and then save it in the form of a cookie file on each user's machine.
If you use IE to access the Web, you will see all the cookies saved on your hard disk. The most common places they store are: C:\Windows\Cookies (C:\Documents and Settings\ your username in Win 2000 \cookies)
Cookies are saved in the format "keyword key= value".
2. Creates a cookie object that invokes the constructor of the cookie object to create a cookie. The constructor for the cookie object has two string parameters: the cookie name and the cookie value.
Cookie C=new Cookie ("username", "John");
3. In the JSP, if you want to transfer the encapsulated cookie object to the client, use the response Addcookie () method.
Format: Response.addcookie (c)
4. Read the cookies saved to the client, use the GetCookies () method of the Request object, and arrange the cookie objects from all clients as an array when executed, and if you want to remove the cookie object that matches your needs, you need to iterate through the keywords for each object in the array.
Cases:
Cookie[] C=request.getcookies ();
if (c!=null)