1.1 application
*Get the application object in jsp such as: getServletContext().setAttribute("counter",new mycount.Counter());
For example: <jsp:useBean scope="application" id="counter" class="mycounter.Counter"/>
*The method of handling On Application Start and On Session Start events in jsp uses 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 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
*Get the absolute URL address 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());
*Get the url through which the client accesses this page
String callPage=request.getHeader("Referer");
*Get the real path of the current script in the local file system
request.getRealPath(request.getServletPath());
*Determine one of multiple submissions
<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
*Three methods of web page 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 the above method can only be used before any output has been sent to the client.
*Disable caching
<%response.setHeader("Cache-Control","no-store");
response.setDateHeader("Expires",0);%>
1.4 session
*survival time
<%session.setMaxInactiveInterval(300);%>
*Logout
session.invalidate();
1.5 exception
*Handling Servlet errors in jsp pages
protected void sendErrorRedirect(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{
sendErrorRedirect(request,response,"/jsp/ErrPage.jsp",e);
}catch(Exception e){e.printStackTrace();}
}
}
* Output error stacktrace in 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>
1.6 Cookies
*Set cookies
<%
Cookie mycookie=new Cookie("aName","aValue");
response.addCookie(mycookie);
//mycookie.setMaxAge(time);
%>