JSP written test. __js

Source: Internet
Author: User
Tags html form smtpclient
1. How to mix JSP and SSI #include?
You can include pure HTML in a JSP by using the following methods:
<!--#i nclude 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 ("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 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
%>
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.