Detailed description of JSP internal objects

Source: Internet
Author: User
Tags set cookie
1.1 Application
* Obtain the application object in JSP.
For example, getservletcontext (). setattribute ("counter", new mycount. Counter ());
Example: <JSP: usebean scope = "application" id = "counter" class = "mycounter. Counter"/>
* Methods for processing on Application Start and on session start events in JSP
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/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 to distinguish

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: Forward page = "/Newpage. jsp"/>
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 page
Protected void senderrorredirect (httpservletrequest request,
Httpservletresponse response, string errorpageurl, throwable E)
Throws servletexception, ioexception {
Request. setattivity ("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 on 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>

Cookie 1.6
* Set cookie
<%
Cookie mycookie = new cookie ("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.