1.1 Application
* Obtain the application object in JSP.
For example, getservletcontext (). setattribute ("counter", new mycount. Counter ());
Example: <JSP: usebean scope = "application" id = "counter" class = "mycounter. Counter"/>
* Methods for processing on Application Start and on session start events in JSP
Use the httpsessionbindinglistener class.
Add session:
Session. putvalue ("bingdings. listener", new mylistener (getservletcontext ());
Define the mylistener class:
Import javax. servlet. http .*;
Import javax. servlet .*;
Public class mylistener implements httpsessionbindinglistener {
Servletcontext context;
Public mylistener (servletcontext context ){
This. Context = context;
}
Public void valuebound (httpsessionbindingevent event ){
System. Out. println ("valuebound: Someone just bound my listener to a session! ");
}
Public void valueunbound (httpsessionbindingevent event ){
System. Out. println ("valueunbound: Someone just unbound my listener! ");
}
}
1.2 request
* Obtain the absolute URL of a running JSP/Servlet File
Stringf file = request. getrequesturl ();
If (requet. getquerystring ()! = Null {
File + = '? '+ Request. getquerystring ();
}
URL reconstructedurl = new URL (request. getscheme (), request. getservername (), request. getserverport (), file );
Out. println (reconstructedurl. tostring ());
* Obtain the URL through which the client accesses the page.
String callpage = request. getheader ("Referer ");
* Obtain the real path of the current script in the zookeeper file system.
Request. getrealpath (request. getservletpath ());
* Judge one of multiple submit
<Input type = submit name = "sub" value = "up">
<Input type = submit name = "sub" value = "down">
Use request. getparameter ("sub"); in JSP to distinguish
1.3 response
* Method 3 of webpage redirection
(1) response. sendredirect (URL );
(2) <% response. setstatus (httpservletresponse. SC _moved_premanently );
String nowloc = "/newpath/index.htm ";
Response. setheader ("location", newloc); %>
(3) <JSP: Forward page = "/Newpage. jsp"/>
Note that this method can only be used before any output is sent to the client.
* Disable Cache
<% Response. setheader ("cache-control", "No-store ");
Response. setdateheader ("expires", 0); %>
1.4 session
* Survival time
<% Session. setmaxinactiveinterval (300); %>
* Logout
Session. invalidate ();
1.5 exception
* Handle servlet errors on the JSP page
Protected void senderrorredirect (httpservletrequest request,
Httpservletresponse response, string errorpageurl, throwable E)
Throws servletexception, ioexception {
Request. setattivity ("javax. servlet. jsp. jspexception", e );
Getservletconfig (). getservletcontext ();
Getrequestdispatcher (errorpageurl). Forward (request, response );
}
Public void dopost (httpservletrequest request, httpservletresponse response ){
Try {
//
}
Catch (exception e) {try {
Senderrorredirect (request, response, "/JSP/errpage. jsp", e );
} Catch (exception e) {e. printstacktrace ();}
}
}
* Output the wrong stacktrace on the JSP page.
(1)
<% @ Page iserrorpage = "true %>
<%
Out. println ("<PRE> ");
Printwriter PW = response. getwriter ();
Exception. printstacktrace (PW );
Out. println ("</PRE> ");
%>
(2)
<% @ Page iserrorpage = "true %>
<PRE>
<%
Exception. printstacktrace (New printwriter (out ));
%>
</PRE>
Cookie 1.6
* Set cookie
<%
Cookie mycookie = new cookie ("aname", "avalue ");
Response. addcookie (mycookie );
// Mycookie. setmaxage (time );
%>