JSP built-in object programming skills

Source: Internet
Author: User
Tags error handling file system set cookie client
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);


%>








Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.