JSP nine large built-in objects include request response session application Out page Config exception pagecontent
Where the valid range of the request response Out page Config exception PageContent object is the current page application session valid range is the current session (all pages of the current client) application valid range Is the entire application, as long as the server does not shut down the object is valid
====================================================================
Request
====================================================================
Request.getparameter (); Get user-submitted form information
Request.setcharacterencoding ("UTF-8"); Set request encoding to prevent garbled characters
Request.setattribute ("Unmae", New Object ()); Save the data to a variable in the request scope
Request.forward (String Url); forwarding
Request.getrequesturl (); Gets the IE address of the current page
Request.getheader ("Resref"); Obtain the requested IE address
Request.getremoteaddr (); Get the IP address of the user
====================================================================
Cookies
====================================================================
Cookie.getcookies () Get a collection of all cookie objects
Cookie.getname () Obtain the specified name of the cookie
Cookie.getvalue () Gets the value of the cookie object
Urlencoder.encode (); Encode data that needs to be saved in a cookie
Urlencoder.decode (); Decode information when reading cookie information
====================================================================
Response
====================================================================
Response.addcookie () sends a cookie object to the client
Response.sendredirect (String path); redirect
====================================================================
Application
====================================================================
Application.setattribute (key,value); Add attribute value to Application
Application.getattribute (Key,value); Gets the specified value
====================================================================
Session
====================================================================
session.setmaxinactiveinterval (int num); Set active time for session object
Session.isnew (); Determine if a Boolean is returned for a new user
Session.setattribute ();
Session.getattribute ();
Session.invalidate (); Destroy the current session
====================================================================
Case
====================================================================
1: Prevent forms from being submitted outside the site using request
String Address=request.getrequesturl (). toString ();//Get the current IE address
String Addresstwo=request.getheader ("Referer");//Get request Address
String pathadd=null;//Current Server host
String pathaddtwo=null;//Access Server host
Get access to the host name
try {
if (address!=null&&address!= "") {
URL url=new url (address);
Pathadd=url.gethost ();
}
if (addresstwo!=null&&addresstwo!= "") {
URL url1=new url (addresstwo);
Pathaddtwo=url1.gethost ();
}
if (Pathadd.equals (Pathaddtwo)) {
SYSTEM.ERR.PRINTLN ("accessible");
}else{
System.err.println ("not accessible in the same station");
}
} catch (Malformedurlexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
2: Website Counter application
Synchronized Java keyword uses a thread that is not completed to lock the next thread
int i=0;
Synchronized (application) {
if (Application.getattribute ("Times") ==null) {//The first visitor after server startup
I=1;
}else{
I=integer.parseint (Application.getattribute ("Times"));
i++;//number of visits plus one
}
Application.setattribute ("Times", integer.tostring (i)); To deposit the number of visits to application
}
====================================================================
Nine large built-in objects in Java