JSP script elements and annotations review sample

Source: Internet
Author: User
Tags comments config tomcat apache tomcat

Review the JSP script element and annotation section today, the case writes out, everybody oneself debugging, the overall summary is as follows,
1, JSP declaration statement:
<%! Declaration Statement%>
A variable with a declaration statement is a global variable, and multiple users execute the JSP page, and the variable is shared.
Such as:

Copy Code code as follows:


&lt;html&gt;


&lt;head&gt;


&lt;title&gt;jsp demo&lt;/title&gt;


&lt;/head&gt;


&lt;body&gt;


&lt;%! int a = 1;%&gt;


&lt;%


out.println ("a=" +a);


a++;


%&gt;


&lt;/body&gt;


&lt;/html&gt;


The results are as follows:

Refresh again, you will find the following a value plus one, because variable a is a global variable, a++ operation multiple cumulative, multiple customer service end sharing:

2, JSP Scriptlets:
<% Java Code%>
In the JSP scriptlets can contain several statements, such as: methods, variables, expressions, etc.;
Such as:

Copy Code code as follows:


&lt;html&gt;


&lt;head&gt;


&lt;title&gt;test demo&lt;/title&gt;


&lt;/head&gt;


&lt;body&gt;


&lt;%


String str = "hello,xiaomo!";


out.println (str);


%&gt;


&lt;/body&gt;


&lt;/html&gt;


The results are as follows:

3, JSP expression:
<%= Java Code%>
A simple output form that must have a value that can be output, the code in the expression executes first, and then converted to a string to display to the page.
Such as:

Copy Code code as follows:


&lt;html&gt;


&lt;head&gt;


&lt;title&gt;test demo&lt;/title&gt;


&lt;/head&gt;


&lt;body&gt;


&lt;% String str = "hello,xiaomo!"; %&gt;


&lt;%= str%&gt;


&lt;/body&gt;


&lt;/html&gt;


The results are as follows:

4, JSP comments:
<!----> HTML comments, which are displayed on the client side;
<%----%>jsp annotation, will not be displayed on the client;
Such as:

Copy Code code as follows:


&lt;html&gt;


&lt;head&gt;


&lt;title&gt;test demo&lt;/title&gt;


&lt;/head&gt;


&lt;body&gt;


&lt;!--This is a Web page annotation--&gt;


&lt;%--This is a JSP annotation--%&gt;


&lt;/body&gt;


&lt;/html&gt;


The results are as follows:

We found that there is nothing to show, click the browser right button to see the source code can be seen in the comments are in the JSP, this is only in the JSP source code can see:

Copy Code code as follows:


&lt;html&gt;


&lt;head&gt;


&lt;title&gt;test demo&lt;/title&gt;


&lt;/head&gt;


&lt;body&gt;


&lt;!--This is a Web page annotation--&gt;


&lt;/body&gt;


&lt;/html&gt;


It should be known to many people, JSP page is translated into a servlet after the conversion of the server into the HTML code sent to the client display, the following for you to see the case of JSP page converted into a servlet after the source code (which is marked as converted to the foreground of the HTML codes):

Copy Code code as follows:




/*


* Generated by the Jasper component of Apache Tomcat


* Version:apache tomcat/7.0.34


* Generated at:2013-07-03 01:27:37 UTC


* Note:the Last modified time of this file is set to


* The last modified time of the source file after


* generation to assist with modification tracking.


*/


package org.apache.jsp;


import javax.servlet.*;


import javax.servlet.http.*;


import javax.servlet.jsp.*;


public final class helloxiaomo_jsp extends Org.apache.jasper.runtime.HttpJspBase


Implements Org.apache.jasper.runtime.JspSourceDependent {


private static final javax.servlet.jsp.JspFactory _jspxfactory =


javax.servlet.jsp.JspFactory.getDefaultFactory ();


private static java.util.map&lt;java.lang.string,java.lang.long&gt; _jspx_dependants;


private Javax.el.ExpressionFactory _el_expressionfactory;


private Org.apache.tomcat.InstanceManager _jsp_instancemanager;


public java.util.map&lt;java.lang.string,java.lang.long&gt; getdependants () {


return _jspx_dependants;


}


public void _jspinit () {


_el_expressionfactory = _jspxfactory.getjspapplicationcontext (Getservletconfig (). GetServletContext ()). Getexpressionfactory ();


_jsp_instancemanager = Org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager (Getservletconfig ( ));


}


public void _jspdestroy () {


}


public void _jspservice (final javax.servlet.http.HttpServletRequest request, final Javax.servlet.http.HttpServletResponse response)


throws Java.io.IOException, javax.servlet.ServletException {


final Javax.servlet.jsp.PageContext PageContext;


javax.servlet.http.HttpSession session = NULL;


final Javax.servlet.ServletContext Application;


Final Javax.servlet.ServletConfig config;


javax.servlet.jsp.JspWriter out = null;


Final Java.lang.Object page = this;


Javax.servlet.jsp.JspWriter _jspx_out = null;


Javax.servlet.jsp.PageContext _jspx_page_context = null;


try {


Response.setcontenttype ("Text/html;charset=utf-8");


PageContext = _jspxfactory.getpagecontext (this, request, response,


null, True, 8192, true);


_jspx_page_context = PageContext;


application = Pagecontext.getservletcontext ();


config = Pagecontext.getservletconfig ();


session = Pagecontext.getsession ();


out = Pagecontext.getout ();


_jspx_out = out;


out.write ("RN");


out.write ("&lt;html&gt;rn");


out.write ("T&lt;head&gt;rn");


out.write ("Tt&lt;title&gt;test demo&lt;/title&gt;rn");


out.write ("T&lt;/head&gt;rn");


out.write ("T&lt;body&gt;rn");


out.write ("tt&lt;!--This is a page annotation--&gt;rn");


out.write ("tt");


out.write ("RN");


out.write ("T&lt;/body&gt;rn");


out.write ("&lt;/html&gt;");


} catch (Java.lang.Throwable t) {


if (!) ( T instanceof Javax.servlet.jsp.SkipPageException)) {


out = _jspx_out;


if (out!= null &amp;&amp; out.getbuffersize ()!= 0)


try {out.clearbuffer ();} catch (Java.io.IOException e) {}


if (_jspx_page_context!= null) _jspx_page_context.handlepageexception (t);


Else throw new servletexception (t);


}


} finally {


_jspxfactory.releasepagecontext (_jspx_page_context);


}


}


}

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.