JSP built-in object programming skills
Last Update:2017-02-28
Source: Internet
Author: User
js| Programming | objects | techniques | Built-in Objects 1.1 application
* Get Application object in JSP
such as: Getservletcontext (). setattribute ("Counter", New mycount. Counter ());
such as: <jsp:usebean scope= "Application id=" counter "class=" mycounter. Counter "/>
* methods to handle on application start and on session start events in a JSP
uses the Httpsessionbindinglistener class.
Add session:
session.putvalue ("Bingdings.listener", New MyListener (Getservletcontext ());
definition 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 I listener to a session!");
}
public void Valueunbound (Httpsessionbindingevent event) {
System.out.println ("Valueunbound:someone just unbound my listener!");
}
}
1.2 Request
* Gets an absolute URL address for a jsp/servlet file that is running
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 client through which URL to access this page
String callpage=request.getheader ("Referer");
* Get the real path of the current script in the Scabies file system
Request.getrealpath (Request.getservletpath ());
* To judge a
in multiple submit
<input type=submit name= "sub" value= "Up" >
<input type=submit name= "sub" value= "Down" >
Use Request.getparameter ("sub") in the JSP, and you can distinguish the
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 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 (;%>)
* Cancellation
session.invalidate ();
1.5 Exception
* error handling Servlet in JSP page
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 the wrong stacktrace in 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>
1.6 Cookie
* Set Cookie
<%
Cookies Mycookie=new Cookies ("Aname", "avalue");
Response.addcookie (MyCookie);
//mycookie.setmaxage (time);
%>
。