Java basics-answers to problems that may be encountered in JSP

Source: Internet
Author: User
Tags html input text smtpclient

1. How to mix Jsp and SSI # include?
In JSP, you can use the following method to include pure HTML:
& Lt! -- # Include file = "data. inc" -- & gt
However, if data. inc contains jsp code, we can use:
& Lt % @ include file = "data. inc" % & gt


2. How to execute a thread-safe JSP?
Add the following command:
& Lt % @ page isThreadSafe = "false" % & gt


3. How does JSP process data in html form?
You can use the built-in request object as follows:
& Lt %
String item = request. getParameter ("item ");
Int howeter = new Integer (request. getParameter ("units"). intValue ();
% & Gt


4. How to include a static file in JSP?
Static include: & lt % @ include file = "copyright.html" % & gt
Dynamic inclusion: & lt jsp: include page = "copyright.html" flush = "true"/& gt


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


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:
& Lt %
Response. setStatus (HttpServletResponse. SC _MOVED_PERMANENTLY );
String newLocn = "/newpath/index.html ";
Response. setHeader ("Location", newLocn );
% & Gt


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:
& Lt %
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
% & Gt


8. How do I set cookies in JSP?
COOKIE is sent as part of the http header. You can set it as follows:
& Lt %
Cookie mycookie = new Cookie ("aName", "aValue ");
Response. addCookie (mycookie );
% & Gt


9. How to delete a COOKIE in JSP?
& Lt %
Cookie killMyCookie = new Cookie ("mycookie", null );
KillMyCookie. setMaxAge (0 );
KillMyCookie. setPath ("/");
Response. addCookie (killMyCookie );
% & Gt


10. How to stop JSP execution during JSP request processing
For example:
& Lt %
If (request. getParameter ("wen ")! = Null ){
// Do something
} Else {
Return;
}
% & Gt


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:
& Lt %!
Public String howBadFrom (HttpServletRequest req ){
HttpSession ses = req. getSession ();
...
Return req. getRemoteHost ();
}
% & Gt
& Lt %
Out. print ("in general, lao lee is not baddie ");
% & Gt
& Lt % = howBadFrom (request) % & gt


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
& Lt % @ page session = "true" % & gt
& Lt %
Integer num = new Integer (100 );
Session. putValue ("num", num );
String url = response. encodeURL ("hello2.jsp ");
% & Gt
& Lt a href = & lt % = url % & gt & gthello2.jsp & lt/a & gt

Hello2.jsp
& Lt % @ page session = "true" % & gt
& Lt %
Integer I = (Integer) session. getValue ("num ");
Out. println ("Num value in session is" + I. intValue ());
% & Gt


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.
& Lt % @ page import = "sun.net. smtp. SmtpClient, java. io. *" % & gt
& Lt %
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 );
}
% & Gt


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.
& Lt % @ page import = "javax. naming. *, javax. rmi. PortableRemoteObject,
Foo. AccountHome, foo. Account "% & gt
& Lt %!
// 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 );
}
% & Gt
& Lt %
// Instantiate SessionBean
Account acct = accHome. create ();
// Call the Remote Method
Acct. doWhatever (...);
// So on
% & Gt


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:
& Lt %!
String blanknull (String s ){
Return (s = null )? "": S;
}
% & Gt

Then in the FORM of JSP, you can use
& Lt input type = "text" name = "shoesize" value = "& lt % = blanknull (shoesize) % & gt" & gt


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:
& Lt jsp: getProperty name = "wenBean" property = "someProperty"/& gt
& Lt jsp: setProperty name = "wenBean" property = "someProperty" value = "someValue"/& gt

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 & lt jsp: include page = "relativeURL" flush = "true"/& gt


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


& Lt % @ include file = "abc. jsp" % & gt and & lt jsp: include page = "abc. jsp"/& gt?
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:
& Lt % @ page import = "java. io. *" % & gt
& Lt %
String str = "print me ";

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.