1 JSP-Overview
1.1 What is JavaServer Pages?
The JSP (Java server Pages) is a dynamic resource on the Javaweb server side. It works the same as an HTML page, displaying data and getting data .
The composition of 1.2 jsp
JSP = html + java Script (code snippet) + JSP dynamic tag
2 JSP syntax
2.1 JSP Script
The JSP script is the Java code fragment, it is divided into three kinds:
L <%...%>:java statement;
L-<%=...%>:java expression;
L <%!...%>:java define class members;
1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> 2 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" > 3
2.2 Built-in object outAn out object can be used in a JSP page without creating it, and it is used to output to the client.
1 <body>2 7 </body>
where <%=...%> and Out.print () function is the same! They are all output to the client, for example:
<%=s1%> equivalent to <% out.print (S1); %>
<%= "Hello"%> equates to <% out.print ("Hello"); %>, also equivalent to writing hello directly on the page.
More than 2.3 <%...%> can be universal1 <body>2
To cycle through a table:
1 <body> 2
3 The principle of JSP3.1 JSP is a special servletJSP is a special servlet that, when the JSP page is first accessed, the container (TOMCAT) compiles the JSP into a servlet before executing the servlet. So JSP is actually a servlet!
3.2 JSP Host DirectoryJSP-generated servlet stored in the ${catalana}/work directory, I often joke that it is the "home" JSP. Let's take a look at what's in it and find out about the "flesh" of the JSP.
You will find that static information (such as
The entire content of the JSP will be put into a method named _jspservice! You may say < @page > is not in the "<%", the @page > will be recorded later in the notes.
A_jsp.java's _jspservice () method:
1 public void _jspservice (final javax.servlet.http.HttpServletRequest request, 2 final Javax.servlet.http.HttpServletResponse response) 3 throws Java.io.IOException, Javax.servlet.ServletException {4 5 final Javax.servlet.jsp.PageContext PageContext; 6 javax.servlet.http.HttpSession session = NULL; 7 Final Javax.servlet.ServletContext Application; 8 Final javax.servlet.ServletConfig config; 9 Javax.servlet.jsp.JspWriter out = null;10 final Java.lang.Object page = this;11 javax.servlet.jsp.JspWriter _jspx_out = null;12 Javax.servlet.jsp.PageContext _jspx_page_context = null;13 try {Response.setcon Tenttype ("Text/html;charset=utf-8"); PageContext = _jspxfactory.getpagecontext (this, request, response,18 NULL, True, 8192, true); _jspx_page_context = pagecontext;20 application = pagecontext.getservletc Ontext (); config = Pagecontext.getservletconfig (); session = PAGECONTEXT.GEtsession (); out = Pagecontext.getout (); _jspx_out = out;25 26 ... 27}4 re-discussion of JSPThere are three forms of JSP scripts:
l <%...%>: Content will be placed directly in the "flesh";
l <%=...%>: content is placed in Out.print () as a parameter of Out.print ();
L <%!...%>: Content will be placed outside the _jspservice () method, directly contained by the class;
The <%...%> and <%=...%> have been explained before, but the role of%> is not explained <%!...!
Now we know that JSP is actually a class, a servlet class. <%!...%> 's role is to add methods or members to the class, so the contents of <%!...%> do not appear in _jspservice ().
1 <%!2 private string name;3 public string Hello () {4 return "Hello jsp!"; 5 }6%>
5 JSP annotationsWe now know that JSP needs to be compiled into a. Java and then compiled into a. class. The content in <%--...--%> is ignored when JSP is compiled into. Java, which is the JSP comment.
You can also use HTML annotations in JSP pages to:<!--...--, but this comment is present in the JSP compiled. Java, it is not ignored and is sent to the client browser.
However, when the browser displays the HTML sent by the server, the browser will not display it because <!--...--> is an HTML comment.
Getting Started with JSPs