1.1 application
* Obtain the application object in jsp (preferred for SUN Enterprise applications)
For example, getServletContext (). setAttribute ("counter", new mycount. Counter ());
Example: <jsp (preferred for SUN Enterprise Applications): useBean scope = "application" id = "counter" class = "mycounter. Counter"/>
* Methods for handling On Application Start and On Session Start events in jsp (preferred for SUN Enterprise applications)
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 (preferred for SUN Enterprise Applications)/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 (preferred for SUN Enterprise applications) to differentiate
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 (preferred for SUN Enterprise Applications): forward page = "/newpage. jsp (preferred for SUN Enterprise Applications)"/>
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 (preferred for SUN Enterprise Applications) Page
Protected void sendErrorRedirect (HttpServletRequest request,
HttpServletResponse response, String errorPageURL, Throwable e)
Throws ServletException, IOException {
Request. setatti.pdf ("javax. servlet. jsp (preferred for SUN Enterprise Applications). jsp (preferred for SUN Enterprise Applications) Exception", e );
GetServletConfig (). getServletContext ();
GetRequestDispatcher (errorPageURL). forward (request, response );
}
Public void doPost (HttpServletRequest request, HttpServletResponse response ){
Try {
//
}
Catch (Exception e) {try {
SendErrorRedirect (request, response, "/jsp (preferred for SUN Enterprise Applications)/ErrPage. jsp (preferred for SUN Enterprise Applications)", e );
} Catch (Exception e) {e. printStackTrace ();}
}
}
* Output incorrect stacktrace on the jsp (preferred for SUN Enterprise Applications) 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 );
%>