Common jsp q &

Source: Internet
Author: User
Tags html input text smtpclient

Common jsp q &

1. How to mix JSP and SSI # include?
In JSP, you can use the following method to include pure HTML:
<! -- # Include 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
%>

16. When I use a result set, how do I prevent the field "null" from being displayed in my HTML input text field?
A simple function can be defined as follows:
<%!
String blanknull (string s ){
Return (S = NULL )? "": S;
}
%>

Then in the form of JSP, you can use
<Input type = "text" name = "shoesize" value = "<% = blanknull (shoesize) %>">

17. How can I download an object (such as binary, text, and executable) from a servlet or JSP )?
Two solutions are available:
A: Use http,
B: In servlet, you can set contenttype and use the stream class of Java. Io package. For example:
Response. setcontenttype ("application/X-MSWord ");
Then you can write something in the output buffer.

18. How to accept initialization parameters when initializing beans using usebean flag
Use the following two tags:
<JSP: getproperty name = "wenbean" property = "someproperty"/>
<JSP: setproperty name = "wenbean" property = "someproperty" value = "somevalue"/>

19. How can I obtain the information of the client browser through JSP?
Use request. getheader (string ).

20. Can I call JSP like a subroutine?
Of course, you can use <JSP: Include page = "relativeurl" Flush = "true"/>

21. Why does the JVM continue to use my old class after I recompile a class used by my JSP?

What is the difference between <% @ include file = "ABC. jsp" %> and <JSP: Include page = "ABC. jsp"/>?
The first is static, and the other is dynamic.

22. What are the disadvantages of JSP?
1. Debugging Java programs is not good.
2. Because most servlet engines do not support Connection Pooling
3. No Servlet Engine Standard
4. Interaction between JSP and other scripting languages

23. Can JSP be recursively called?
Yes, of course. For example, if you submit a form to this page

34. How to internationalize JSP?
Provide resource bundles attribute files for various versions.

25. How to Write text files in JSP?
Use a printwriter object, such:
<% @ Page import = "Java. Io. *" %>
<%
String STR = "Print me ";
String nameoftextfile = "/usr/Anil/imp.txt ";
Try {
Printwriter PW = new printwriter (New fileoutputstream (nameoftextfile ));
PW. println (STR );
PW. Close ();
} Catch (ioexception e ){
Out. println (E. getmessage ());
}
%>

26. How to include absolute path files in JSP?
Use urlconnection.

27. Can session objects be shared between servlets and JSP?
Of course,
Httpsession session = request. getsession (true );
Session. putvalue ("variable", "value ");

28. Can JavaScript variables be copied to JSP sessions?

29. How do I set the cookie to expire after a certain time?
Use cookie. setmaxage (INT)

30. How do I obtain the current number of sessions?
You can use httpsessionbindinglisteners for tracking.

31. Can I set some code to run on all my JSP files? If yes, can it be shared?
Of course, you can define an alias for your JSP file:/JSP/= ybwen. Genius. mypreprocessingservlet. Files prefixed with/JSP/can use

32. Is synchronization possible if multiple clients simultaneously request a JSP page?
What are the advantages of using beanname in JSP: usebean syntax?
Beanname uses beans. instantiate () to initialize Bean

33. When I use <JSP: Forward>, the address bar of the browser is not changed?
Use response. sendredirect ("newurl ")

34. How to convert JSP 0.9 files to jsp1.1?
You can use SED/awk.

35. Can I use JSP to set the focus of the input field in HTML form, without JavaScript?
No way

36. What is the best way to connect to the database to connect to the buffer pool Using JSP?
1. Use jdbc2. Driver with this service in 0
2. Use the application server that provides this service
3. Write by yourself

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.