JSP question and Answer set

Source: Internet
Author: User
Tags define connection pooling flush header html form html input text include integer
JS 1, how to mix JSP and SSI #include?
You can include pure HTML in a JSP by using the following methods:
<!--#include file= "Data.inc"-->
But if the data.inc contains JSP CODE, we can use:
<% @include file= "Data.inc"%>


2. How to execute a thread-safe JSP?
Just add the following instructions
<%@ page isthreadsafe= "false"%>


3, JSP How to handle the data in HTML form?
You can use the built-in request object, as follows:
<%
String item = request.getparameter ("item");
int howmany = new Integer (Request.getparameter ("units")). Intvalue ();
%>


4, in the JSP how to include a static file?
The static includes the following: <%@ include file= "copyright.html"%>
The dynamic includes the following: <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 browsing redirection in JSP?
The following methods can be used: Response.sendredirect ("http://ybwen.home.chinaren.com/index.html");
You can also physically change the HTTP header properties as follows:
<%
Response.setstatus (httpservletresponse.sc_moved_permanently);
String newlocn= "/newpath/index.html";
Response.setheader ("Location", NEWLOCN);
%>


7, how to prevent the JSP or servlet in the output is not browser saved in the cache?
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 to set up cookies in JSP?
Cookies are sent as part of the HTTP header, and you can set the following methods:
<%
Cookie MyCookie = new Cookie ("Aname", "avalue");
Response.addcookie (MyCookie);
%>


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


10. How to stop the execution of JSP in the request processing of a JSP
The following example:
<%
if (Request.getparameter ("Wen")!= null) {
Do something
} else {
Return
}
%>


11. How to define methods in JSP
You can define a method, but you cannot directly access the built-in object of the JSP, but rather pass it through the method of the parameter. As follows:
<%!
Public String Howbadfrom (HttpServletRequest req) {
HttpSession ses = req.getsession ();
...
return Req.getremotehost ();
}
%>
<%
Out.print ("In General,lao Lee are not baddie");
%>
<%= Howbadfrom (Request)%>


12, if the browser has been closed cookies, in the JSP how can I open the session to track
You can use URL rewriting as follows:
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 the session is" +i.intvalue ());
%>


13, in the JSP can send email?
You can use the Sun's private package: SUN.NET.SMTP package. The following script uses the SmtpClient class to send 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 (to);
PrintStream message = Client.startmessage ();
Message.println ("to:" + to);
MESSAGE.PRINTLN ("subject:sending email from jsp!");
Message.println ("This is sent from a JSP page!");
Message.println ();
Message.println ("cool!:-)");
Message.println ();
Message.println ("Good Boy");
Message.println ("I ' m 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 the servlet?
Of course, the following shows how to invoke a JSP error page within a servlet control logical 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, JSP and applet how to communicate
How JSP communicates with EJB Sessionbean
The following code snippet is a good example of
<%@ page import= "javax.naming.*, Javax.rmi.PortableRemoteObject,
Foo. Accounthome, Foo. Account "%>
<%!
Defines a global reference to an instance of the Sessionbeanhome interface
Accounthome Acchome=null;

public void Jspinit () {
Get Home Interface Instance
InitialContext cntxt = new InitialContext ();
Object ref= cntxt.lookup ("JAVA:COMP/ENV/EJB/ACCOUNTEJB");
Acchome = (accounthome) portableremoteobject.narrow (Ref,accounthome.class);
}
%>
<%
Instantiation of Sessionbean
Account acct = Acchome.create ();
Calling remote methods
Acct.dowhatever (...);
Mailroom
%>


16. When I use a result set, how do I prevent a field that is "null" from being displayed in my HTML input text field?
You can define a simple function to achieve your goal, as follows:
<%!
String Blanknull (string s) {
return (s = = null)? "": s;
}
%>

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


17. How to download a file (such as: binary,text,executable) in a servlet or JSP?
There are two solutions available:
A: Using HTTP,
B: In the servlet, you can do this by setting up contenttype and using the stream such as Java.io package. For example:
Response.setcontenttype ("Application/x-msword");
Then I want to write some things in the output buffer.

18. How to accept initialization parameters when initializing a bean using the Usebean flag
Use the following two labels:
<jsp:getproperty name= "Wenbean" property= "Someproperty"/>
<jsp:setproperty name= "Wenbean" property= "Someproperty" value= "somevalue"/>

19, using JSP How to obtain the Customer browser information?
Use Request.getheader (String) to


20. Can call a JSP like calling a subroutine?
Of course, with <jsp:include page= "Relativeurl" flush= "true"/>


21. After I recompile a class used by my JSP, why does the JVM continue to use my old class?


<% @include The difference between file= "abc.jsp"%> and <jsp:include page= "abc.jsp"/>?
The previous one is statically contained and the latter is dynamically contained


22, the shortcomings of JSP?
1. Debugging a Java program There's no good stuff.
2. Because most servlet engines do not support connection pooling
3. Servlet engine has no standard
4. JSP's interaction with other scripting languages


23, JSP can be recursive call it?
Of course, if you submit the form to this page


34, how to achieve the internationalization of JSP?
Provide resource bundles property files for various versions

25. How to write text files in JSP?
Use the PrintWriter object, such as:
<%@ 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 the absolute path file in the JSP?
You can use URLConnection.


27. Can I share the session object between Servlets and JSP?
Of course
HttpSession session = Request.getsession (true);
Session.putvalue ("Variable", "value");


28, JavaScript variables can be copied into the JSP session?


29. How do I set a cookie to expire after a certain time?
with cookie.setmaxage (int)


30, how to get the current number of sessions?
You can use Httpsessionbindinglisteners to track


31. Can you set up some code to run on all my JSP files? If you can, can you share it?
Of course, you can define an alias for your JSP file:/jsp/=ybwen.genius.mypreprocessingservlet, and/jsp/-prefixed files can be used


32, for a JSP page, if multiple clients simultaneously request it, synchronization possible?
What are the benefits of using Beanname in Jsp:usebean syntax?
Beanname use Beans.instantiate () to initialize the bean


33, when I use <jsp:forward>, in the browser's address bar has not changed?
Using Response.sendredirect ("Newurl")


34, how to convert the JSP 0.9 version of the file to JSP1.1?
You can use Sed/awk


35. Use JSP to set the focus of input fields in HTML form without JavaScript?
No way


36. What is the best way to connect a buffer pool with a JSP connection to the database?
1. Use JDBC2. 0 driver with this service
2. Use the application server that provides this service
3. Write your own


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.