JSP test.

Source: Internet
Author: User
Tags smtpclient
1. How to mix JSP and SSI # include?
In JSP, you can use the following method to include pure HTML:
<! -- # I nclude file = "data. Inc" -->
However, if data. inc contains JSP code, we can use:
<% @ Include file = "data. Inc" %>

2. How to execute a thread-safe JSP?
Add the following command:
<% @ Page isthreadsafe = "false" %>

3. How does JSP process data in HTML form?
You can use the built-in request object as follows:
<%
String item = request. getparameter ("item ");
Int howeter = new INTEGER (request. getparameter ("units"). intvalue ();
%>

4. How to include a static file in JSP?
Static include: <% @ include file = "copyright.html" %>
Dynamic inclusion: <JSP: Include page = "copyright.html" Flush = "true"/>

5. How to Use annotations in JSP?
There are four main methods:
1. <% -- And -- %>
2. //
3. /** And **/
4. <! -- And -->

6. How to perform browser redirection in JSP?
Use the following method: Response. sendredirect ("http://ybwen.home.chinaren.com/index.html ");
The HTTP header attribute can also be physically changed as follows:
<%
Response. setstatus (httpservletresponse. SC _moved_permanently );
String newlocn = "/newpath/index.html ";
Response. setheader ("location", newlocn );
%>

7. How to Prevent the output in JSP or servlet from being stored in the cache by browser?
Add the following script to the beginning of the JSP file:
<%
Response. setheader ("cache-control", "No-store"); /// HTTP 1.1
Response. setheader ("Pragma", "No-Cache"); // HTTP 1.0
Response. setdateheader ("expires", 0); // prevents caching at the Proxy Server
%>

8. How do I set cookies in JSP?
Cookie is sent as part of the HTTP header. You can set it as follows:
<%
Cookie mycookie = new cookie ("aname", "avalue ");
Response. addcookie (mycookie );
%>

9. How to delete a cookie in JSP?
<%
Cookie killmycookie = new cookie ("mycookie", null );
Killmycookie. setmaxage (0 );
Killmycookie. setpath ("/");
Response. addcookie (killmycookie );
%>

10. How to stop JSP execution during jsp request processing
For example:
<%
If (request. getparameter ("Wen ")! = NULL ){
// Do something
} Else {
Return;
}
%>

11. How to define methods in JSP
You can define methods, but you cannot directly access the built-in JSP objects, but pass through the parameter method. As follows:
<%!
Public String howbadfrom (httpservletrequest req ){
Httpsession ses = Req. getsession ();
...
Return Req. getremotehost ();
}
%>
<%
Out. Print ("in general, Lao Lee is not baddie ");
%>
<% = Howbadfrom (request) %>

12. If the browser has disabled cookies, how can I enable session in JSP to track them?
Use the URL to rewrite it, as shown below:
Hello1.jsp
<% @ Page session = "true" %>
<%
Integer num = new INTEGER (100 );
Session. putvalue ("num", num );
String url = response. encodeurl ("hello2.jsp ");
%>
<A href = "/<"; % = URL %> hello2.jsp </a>

Hello2.jsp
<% @ Page session = "true" %>
<%
Integer I = (integer) Session. getvalue ("num ");
Out. println ("Num value in session is" + I. intvalue ());
%>

13. Can I send an email in JSP?
You can use sun's dedicated package: sun.net. SMTP package. The following script uses the smtpclient class to send an email.
<% @ Page import = "sun.net. SMTP. smtpclient, java. Io. *" %>
<%
String from = "ybwen@sina.com ";
String to = "hewenjun@yeah.net, lei@who.com.cn ";
Try {
Smtpclient client = new smtpclient ("mail. XXXXX. XXX ");
Client. From (from );
Client. To ();
Printstream message = client. startmessage ();
Message. println ("to:" + );
Message. println ("Subject: sending email from JSP! ");
Message. println ("This was sent from a JSP page! ");
Message. println ();
Message. println ("cool! :-)");
Message. println ();
Message. println ("Good boy ");
Message. println ("Im in genius.com ");
Message. println ();
Client. closeserver ();
}
Catch (ioexception e ){
System. Out. println ("error sending email:" + E );
}
%>

14. Can I call a JSP error page in Servlet?
Of course there is no problem. The following shows how to call a JSP error page in a servlet control logic unit.
Protected void senderrorredirect (httpservletrequest request,
Httpservletresponse response, string errorpageurl,
Throwable E)
Throws servletexception, ioexception {
Request. setattribute ("javax. servlet. jsp. jspexception", e );
Getservletconfig (). getservletcontext ().
Getrequestdispatcher (errorpageurl). Forward (request,
Response );
}

Public void dopost (httpservletrequest request, httpservletresponse response ){
Try {
// Do something
} Catch (exception ex ){
Try {
Senderrorredirect (request, response, "/JSP/myerrorpage. jsp", ex );
} Catch (exception e ){
E. printstacktrace ();
}
}
}

15. How to communicate with JSP and Applet
How does JSP communicate with EJB sessionbean?
The following code snippet is a good example.
<% @ Page import = "javax. Naming. *, javax. RMI. portableremoteobject,
Foo. accounthome, foo. Account "%>
<%!
// Define a global reference to the sessionbeanhome interface instance
Accounthome acchome = NULL;

Public void jspinit (){
// Obtain the home interface instance
Initialcontext cntxt = new initialcontext ();
Object ref = cntxt. Lookup ("Java: COMP/ENV/EJB/accountejb ");
Acchome = (accounthome) portableremoteobject. Narrow (ref, accounthome. Class );
}
%>
<%
// Instantiate sessionbean
Account Acct = acchome. Create ();
// Call the Remote Method
Acct. dowhatever (...);
// So on
%>

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.