JSP question and answer set (to the first contact JSP)

Source: Internet
Author: User
Tags define header html form include integer interface client smtpclient
How is JS mixed with JSP and SSI #include?
You can include pure HTML in a JSP in the following ways:
<!--#include file= "Data.inc"-->
But if the data.inc contains JSP CODE, we can use:
<% @include file= "Data.inc"%>
How do I perform a thread-safe JSP?
Just add the following instructions <br>
<%@ page isthreadsafe= "false"%>
How does a JSP handle the data in an HTML form?
Through the inside of the request object can be as follows:
<%<br>
String item = request.getparameter ("item");
int howmany = new Integer (Request.getparameter ("units")). Intvalue ();
%>
How does a JSP contain a meditation file?
The meditation includes the following: <%@ include file= "copyright.html"%>
The motion includes the following: <jsp:include page= "copyright.html" flush= "true"/>
How to use annotations in JSP?
There are four main methods:
1. <%--and--%>
2. //
3. /** and **/
4. <!--and-->
How do I perform a browse redirect in a JSP?
Use the following way: Response.sendredirect ("http://ybwen.home.chinaren.com/index.html";);
You can also change the HTTP header physically, as follows:
<%
Response.setstatus (httpservletresponse.sc_moved_permanently);
String newlocn= "/newpath/index.html";
Response.setheader ("Location", NEWLOCN);
%>
How do I prevent the output in the JSP or servlet from being saved in the cache by browser?
Add the following toe to the beginning of the JSP file:
<%
Response.setheader ("Cache-control", "No-store"); File://HTTP 1.1
Response.setheader ("Pragma", "No-cache"); File://HTTP 1.0
Response.setdateheader ("Expires", 0); File://prevents caching at the proxy server
%>
How do I set cookies in a JSP?
Cookies are made? A portion of the HTTP header is sent, and the following methods can be set:
<%
Cookie MyCookie = new Cookie ("Aname", "avalue");
Response.addcookie (MyCookie);
%>
How do I delete a cookie in the JSP?
<%
Cookie Killmycookie = new Cookie ("MyCookie", null);
Killmycookie.setmaxage (0);
Killmycookie.setpath ("/");
Response.addcookie (Killmycookie);
%>
How do I stop the JSP in the request of a JSP?
The following example:
<%
if (Request.getparameter ("Wen")!= null) {
Do something
} else {
Return
}
%>
How do I define a method in a JSP?
You can define the method, but you can't access the JSP's internal object directly, but pass the parameters through the method. 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)%>

If browser has closed cookies, how do I open the session in JSP?
You can use the URL rewrite as follows:
hello1.jsp
<%@ page session= "true"%>
<%
Integer num = new integer (100);
Session.putvalue ("num", num);
String URL =response.encodeurl ("hello2.jsp");
%>
Hello2.jsp</a>
hello2.jsp
<%@ page session= "true"%>
<%
Integer i= (integer) session.getvalue ("num");
Out.println ("Num value in the session is" +i.intvalue ());
%>

Can I send email in JSP?
You can use Sun's dedicated package: SUN.NET.SMTP package. The following script uses SmtpClient 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);
}
%>

Can I adjust a JSP error page in the servlet?
Of course, the following shows how to adjust 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 ();
}
}
}
How does JSP interact with EJB Sessionbean?
The following code section is a good example.
<%@ page import= "javax.naming.*, Javax.rmi.PortableRemoteObject,
Foo. Accounthome, Foo. Account "%>
<%!
FILE://defines a global reference to the Sessionbeanhome interface example
Accounthome Acchome=null;
public void Jspinit () {
file://Get Home Interface case
InitialContext cntxt = new InitialContext ();
Object ref= cntxt.lookup ("JAVA:COMP/ENV/EJB/ACCOUNTEJB");
Acchome = (accounthome) portableremoteobject.narrow (Ref,accounthome.class);
}
%>
<%
file://in the actual case of Sessionbean
Account acct = Acchome.create ();
file://method for remote-end tuning
Acct.dowhatever (...);
Mailroom
%>





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.