1.1 Application
* Get Application objects in JSP
Such as: GetServletContext (). SetAttribute ("counter", new mycount.counter ());
Such as:
* Processing the ON Application Start and the ON Session Start event in JSP
Use the HTTPSessionBindingListener class.
Add session:
Session.putValue ("Bingdings.Listener", New MyListener (GetServletContext ());
Define myListener class:
Import javax.servlet.http. *;
Import javax.servlet. *;
Public class mylistener imports httpsessionBindingListener {
ServletContext context;
Public mylistener (servletContext context) {
THIS.CONTEXT = Context;
}
Public void valuebound (httpsessionBindingEvent evenet) {
System.out.println ("ValueBound: Someone Just Bound My Listener to A Session!");
}
Public void valueunbound (httpsessionBindingEvent evenet) {
System.out.println ("Valueunbound: Someone Just UNBound My Listener!");
}
}
1.2 Request
* Get an absolute URL address of a JSP / servlet file in runtime
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 ());
* Get which URL access to the client access this page
String CallPage = Request.getHeader ("Referer");
* Get the real path in the current script in the file system
Request.getRealPath (Request.getServletPath ());
* Judgment one of multiple Submit
Use Request.GetParameter in JSP;
1.3 Response
* Web page redirection three methods
(1) Response.sendredirect (URL);
(2) <% response.setstatus (httpservletResponse.sc_moved_premanently); string nowloc = "/ newpath / index.htm";
Response.setHeader ("location", newloc);%>
(3)
Note that this method can only be used before any output has not been sent to the client.
* Disable cache
<% response.setheader ("Cache-Control", "No-Store");
Response.SetDateHeader ("Expires", 0);%>
1.4 session
* Savory time
<% session.setMaxinactiveInterval (300);%>
* Logout
session.INVALIDATE ();
1.5 Exception
* Handling a servlet error in the JSP page
Protected Void SenderRedirect (httpservletRequest Request,
HTTPSERVLETRESPONSE RESPONSE, STRING ERRORPAGEURL, THROWABLE E)
Throws servletexception, ioException {
Request.setattibute ("javax.servlet.jsp.jspexception", e);
GetServletConfig (). getServletContext ();
GetRequestDispatcher (ErrorPageurl) .forward (Request, Response);
}
Public void dopost (httpservletRequest request, httpservletResponse response) {
Try {
//
}
Catch (Exception E) {TRY {
SenderRredirect (Request, Response, "/ JSP / Errpage.jsp", E);
} catch (exception e) {E.PrintStackTrace ();
}
}
* Output errors in the JSP page STACKTRACE
(1)
<% @ Page IserrorPage = "True%>
<%
OUT.PRINTLN ("
");PrintWriter PW = response.getwriter ();
Exception.PrintStackTrace (PW);
OUT.PRINTLN (" pre>");
%>
(2)
<% @ Page IserrorPage = "True%>
<%
Exception.PrintStackTrace (New PrintWriter (out);
%>
pre>
1.6 cookie
* Setupie
<%
Cookie mycookie = new cookie ("aname", "avalue");
Response.addcookie (MyCookie);
//mycookie.setmaxage (Time);
%>