jsp-Tutorials (Generate Java class, static import and dynamic Import)

Source: Internet
Author: User

I. INTRODUCTION of JSP Technology

JSP is the abbreviation for the Java Server page, which is the extension of the servlet, and its role is to simplify the creation and maintenance of the site.
JSP is a mixture of HTML code and Java code.
JSP files are usually in the name of a JSP or jspx extension.
The JSP has its own syntax.
JSP forms like HTML, but is essentially a servlet.
The advent of JSP makes it possible to effectively separate HTML and business logic code from web development. Often JSP is only responsible for generating dynamic HTML documents, while business logic is implemented by other Java components such as JavaBean. JSPs can access these components through Scriptlet.
<%%>

The process diagram for Tomcat's first access to the JSP:

First of all, we use Tomcat to publish this one web:index.jsp code is:
<%@ page language="java" import="java.util.*" pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" ><html>  <head>  </head>  <body>    <H1>Demonstrating JSP Technology</H1>    <% String name = "Jack";        HttpSession s = request. getsession ();        ServletConfig cfig = Getservletconfig ();        ServletContext AA = Getservletcontext ();        response. setcharacterencoding ("Utf-8");        response. setContentType ("Text/html;charset=utf-8");                 Session.setattribute ("A","AABBCC");      request. SetAttribute ("req", "request"); %>    <H1>Here, use the text to separate.</H1>    <% out.println ("name="+name);//Note 1: The name variable is defined in the JSP above, so it can be accessed here, as for why access, to see the underlying code to know out          . println (Session.getattribute ("a")); %>    <br/>    <!--We call the SUM function here, and output age--sum=<%=sum (%>)<br/>Age=<%=age%>    <%! Here's the addition of "!"        The contents of the number int age =;             int sum (int n) { int s=0;  for (int i=0; i<=n;i++)            {s+=i;        } return s; }%>  </body></html>
The result of the operation is this:

Why is that so? Let's look at the Java source code that Tomcat helped us generate (no matter how the JSP was written, when someone else visited our site, Tomcat would temporarily read our JSP and then generate the corresponding. java file):
My tomcat is installed under D, Myjspdemo is my project name,
D:\apache-tomcat-7.0.30\work\Catalina\localhost\myJspDemo\org\apache\jsp This directory will generate: Index_jsp.java file, we open it,

I just posted a code picture about the JSP I just wrote, or it's too long.

After reading the first picture, did you feel anything ~

Look at the second one ~ ~ is not suddenly understand, the original jsp<%%> in the characters just as it is written into Java code AH ~

The characters in the <%%> are written into the servlet () Method!!!

and <%! The characters in the%> are written into the class, and the method is a level, and the variable is the member variable! --Use this to have a fun thing oh, write one more servlet in, overwrite Tomcat's public void _jspservice (final javax.servlet.http.HttpServletRequest Request, final Javax.servlet.http.HttpServletResponse response) Class! That is, after running, your own JSP will not run at all, will only run their own servlet content (the method name is the same as Tomcat _jspservice, the inside of the variable with the HttpServletRequest parent class, you can implement the overlay). Interesting, hey, but just the lily, there is no need to do so, I just want to explain one, is to use the <%! %>, the variable in this, the scope of the method is the whole class! (Write internal classes are OK)

<%=age%> The bottom of the name translated into: Out.print (age);

Static import:

In the <%! of index.jsp Insert after%>:

<h2>--从这里开始导入包含页--静态导入,合成一个类,可以共享:局部变量、request和response对象等等</h2>     <!-- 静态导入:      -->     <%@include file="jsps/a.jsp" %>
/JSPS/A.JSP:

This write, MyEclipse will error, but it doesn't matter, this is MyEclipse intelligent identification problem, it does not recognize the variable name.

<%@ page language="Java"import="Java.util.*"pageencoding="UTF-8"%> . JSP... Start  out. println("Name="+name); Local variables, which can be accessed         out. println("Age="+age); Global variables that can be accessed         out. println("sum="+sum ( -));//method, you can access         out. println(Session. getattribute("a"));//can access%> . JSP... End 
Demo Result:


Look at how Tomcat helped us to generate this a.jsp code:

Obviously, the generation of index.jsp is in a class, and it's a servlet method!

So, with this, it's easy to understand why we have access to the name,age variable and the sum () method.

Explain:

Static import is actually the additional import of the JSP code (equivalent to the JSP header, the rest of the original copy) (in the form of JSP translation) directly into the current _jspservlet () in the corresponding position!
JSP's way of translating: HTML code (including HTML, body tags, doctype constraints) is in the Out.write () package. The Java code written in the JSP is copied into it!

Dynamic Import:

In index.jsp, after the static import demo, add:

"jsps/b.jsp"></jsp:include> <% out. println("<br/>");         out. println("request= in Index"+request);          out. println("<br/>");         out. println(Request. getattribute("Req")); %> . JSPThe end of the 
B.JSP:
<%@ page language="java" import="java.util.*" pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" ><html>  <head>    <title>This is the page that was imported dynamically</title>  </head>  <body>    <H1>This is the page that was Jsp:include (dynamically imported)</H1>    <% out.println ("Session.getattribute (a) in b.jsp ="+session.getattribute ("a"));        Out.println ("<br/>");        Out.println ("request= in b.jsp"+request);        Out.println ("<br/>");      OUT.PRINTLN (requestgetattribute ("req")); %>  </body></html>
Demo Result:

Index_jsp.java:

It can be seen clearly that the content in b.jsp is not generated here.

Instead, a class was regenerated:

Naturally, the code in B.JSP is generated in this class, and as to why it is not possible to access those local variables and member variables, it is easy for people who understand Java to understand it!

So, static import and dynamic import,
The difference is that static imports do not generate Java classes in addition, but instead generate Java code in the currently imported class, and dynamic import will generate additional Java classes, which is why accessing a static import site runs faster than a dynamically imported web site.
Import all when static import, the compilation instructions will work.
In the case of dynamic import, the compiled instruction of the imported page loses its function, but inserts the body content of the imported page.

Practical application:

In development, the dynamic import/static import feature is typically used to include some infrequently modified headers and footers.

Comment Statement for JSP:
<%- - 在这儿输入注释 - - %>

jsp-Tutorials (Generate Java class, static import and dynamic Import)

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.