JSP basic Knowledge _1

Source: Internet
Author: User

I. Introduction to JSP

JSP is a technique for simplifying servlet writing that mixes Java code and HTML statements in the same file, only to
The dynamically generated content is written in Java code, while static content is written in a normal static HTML page.
The JSP page must have a. jsp file name extension
Java code written in JSP pages needs to be nested in <% and%>, and Java code nested between <% and%> is called a script fragment (Scriptlets).
Content that is not nested between <% and%> is called a template element of JSP
The Java code in the JSP can use the OUT.PRINTLN statement to output the resulting string from other Java program code to the client, or you can use the
The SYSTEM.OUT.PRINTLN statement prints them to the command-line window.
You can also use an element called a JSP expression in a JSP page, simply encapsulating the variables or expressions you want to output directly in <%= and%>.
You can output the result of the operation of this variable or expression to the client. A variable or expression nested within a JSP expression cannot have a semicolon after it.

two. JSP operating principle
When the • Web container (servlet engine) receives a request for access to a URL with an extension of. jsp, it will hand the access request to the JSP engine for processing.
• Each JSP page is accessed the first time, the JSP engine translates it into a servlet source program, and then compiles the servlet source program into a servlet
class file, which is then loaded and interpreted by the Web container (servlet engine) in the same way as a normal servlet program to execute this JSP
Page translated into the servlet program. The JSP specification also does not explicitly require that the scripting code in the JSP be in the Java language, and that the scripting code in the JSP can be used in other languages than the Java language.
Scripting language, however, the JSP page must eventually be converted into a Java servlet program. • Pre-compile all of its JSP pages into a servlet program before the Web application is officially published.
From the top: JSP is essentially a servlet, see the source code to understand more
If the project is published in Tomcat, you can go to the Tomcat work file-->catalina-->localhost--> project name-->jsp-->org
Find Jspname_jsp.java and Jspname_jsp.class under-->apache-->jsp.

The relationship between JSP and servlet

1. jsp files must be run within the JSP server.

2. The jsp file must be generated by a servlet to execute.

3. The first visitor of each JSP page is slow because it must wait for the JSP to compile into a servlet.

4, the JSP page visitor does not need to install any client, even needs not to be able to run the Java running environment, because the JSP page transports to the client is the standard HTML page.

5. The static content of the JSP page and the JSP script are converted into the servlet's Xxxservice () method, similar to the service () method when creating the servlet itself.

6. The JSP declaration part is converted into a member part of the servlet. All JSP declarations can use modifiers such as private,protected,public,static, but not elsewhere.

7, the output expression of the JSP (<%=. %> section), the output expression is converted to the output statement in the servlet's Xxxservice () method.

8, nine built-in objects are either formal parameters of the Xxxservice () method, or local variables of the method, so nine built-in objects can only be used in JSP scripts and output expressions.

three. JSP nine built-in objects

The nine built-in objects are declared in the _jspservice method in each of the following Jspname_jsp.java, so the JSP page can use them in <%%> without declaring the object

1  Public void_jspservice (httpservletrequest request, httpservletresponse response)2         throwsjava.io.IOException, servletexception {3 4PageContext PageContext =NULL;//page context, it is important to get all the information about the page, you can get the other eight hidden objects5HttpSession session =NULL;//A session that represents a browser and a server6ServletContext application =NULL;//represents the current web app and can get initialization parameters for Web apps7ServletConfig config =NULL;//object that represents the servlet for the current JSP, which is hardly used, because initialization parameters need to be configured for the JSP8JspWriter out =NULL;//JspWriter object, often called the Out.println () method, to print a string to a browser, eg out.println ("<br/>") line wrapping9Object page = This;//An application that points to the Servlet object that the current JSP corresponds to, but is an object type and can only invoke the method of object, almost withoutTenJspWriter _jspx_out =NULL; OnePageContext _jspx_page_context =NULL; A  -    ... ... -Throwable exception = the org.apache.jasper.runtime.JspRuntimeLibrary.getThrowable (request); -    if(Exception! =NULL) { - Response.setstatus (httpservletresponse.sc_internal_server_error); -      //exception can only be used when the iserrorpage=true of the page directive is declared, as in the following declaration: +      //in the JSP file header Declaration <% page iserrorpage= "true"%> -}
Of the nine built-in objects above, the following 4 are called domain objects
Pagecontext,request,session,application, they all have the following property-related methods:
1.Object getattribute (String name): Gets the specified property
2.Enumeration getattributenames (): Gets the names of all the properties that comprise the enumeration
3.removeAttribute (String name): Removes the specified property
4.void SetAttribute (String name,object o): Sets properties They are scoped to the following:
PageContext: The scope of the property is limited to the current JSP page
Request: The scope of the property is limited to the same request
Session: The scope of the property is limited to the same session, that is, the browser from open to close is called a session (during which session is not invalidated)
Application: The scope of the property is limited to the current Web application, the maximum property scope, as long as the access is set in one place, in each JSP or servlet can be obtained
four. JSP and servlet application comparison
JSP is a Web development technology centered on generating Web page display content that can directly use template elements to produce the content of a Web page document.
The source file of the JSP page is simpler than the servlet source file, and the JSP page development process is much simpler than the servlet development process.
The JSP engine can detect changes to JSP pages and automatically re-translate and compile modified JSP files.
JSP technology is based on the servlet technology, and all JSP pages are eventually converted into Servlets to run.
• In the development of large Web applications, Servlets and JSPs are often used in a mix of roles, servlets are often used as control components, and processing
Some complex background business, JSP as the display component. • The one-time response process can be divided into several functional modules to work together, first by the servlet to complete process control and business processing, and to store the resulting data
In the request or session domain, and then forward requests (forward) to the JSP page, and then the JSP page from the request or session domain to remove the knot
Data and complete the output of the response content. In this way, the separation of business logic from the display content allows application developers and Web page authors to
Separation of work.

Five. Forwarding and redirection of requests 1) Request forwarding the servlet requests the forwarding implementation process:
1  Public voiddoget (httpservletrequest request, httpservletresponse response)2             throwsservletexception, IOException {3 4System.out.println ("ForwardServlet doget");5         //1. Call HttpServletRequest's Getrequestdispatcher (String path) in Doxxx () and pass in the forwarding address6RequestDispatcher requestdispatcher=request.getrequestdispatcher ("Testservlet");7         //2. Call HttpServletRequest's forward (request,response) for request forwarding8 Requestdispatcher.forward (request, response);9}

JSP Request Forwarding implementation:

Similarly

1 <body>2     <%3       // Request Forwarding 4      Request.getrequestdispatcher ("/jsp_result.jsp"). Forward (request, response); 5     %>6 </body>

2) redirectservlet Redirection implementation process:
1  Public void doget (httpservletrequest request, httpservletresponse response) 2             throws servletexception, IOException {34         System.out.println ("Redirectservlet doget"); 5         // perform the requested redirect in Doxxx (), call Response.sendredirect (String path) directly, and pass in the address you want to redirect 6         Response.sendredirect ("Testservlet"); 7     }
JSP Redirection implementation process:
1 <body>2     <%3        // redirect 4        Response.sendredirect ("jsp_result.jsp"); 5     %>6  </body>
3) The difference between the two

The essential difference is that request forwarding sends only one request, while redirection sends two requests
Specific as follows
(1) Forwarding: The Address bar's request address is the first time the requested address
Redirect: The address bar's request address is displayed as the last redirected address
(2) Forwarding: In the final servlet, the request object and the request that is in transit are the same object
Redirect: In the final servlet, the request object and the transfer request are not the same object
(3) Forwarding: A resource that can only be forwarded to the current Web application
Redirect: Can redirect to any resource
(4) Forwarding: path/represents the root directory of the current web App
Redirect: Path/Represents the root directory of the current Web site

JSP basic Knowledge _1

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.