Working principle of JSP Engine

Source: Internet
Author: User

JSP running environment:

To execute JSP code, you must install the JSP engine on the server. Common engines include WebLogic and Tomcat. After configuring these web servers that support JSP. Then the client can access the JSP page through a browser. The default port is 7001.

JSP lifecycle:

The JSP processing method is to regard all these requests as Servlet. For this reason, many functions and lifecycles of JSP are defined by Java Servlet technical standards. This restriction is more obvious in terms of dynamic content. Therefore, when learning JSP, You need to review Servlet content and standards.

JSP Network Application Server framework:

After the J2EE-compliant server is built, you can compile the JSP page. The JSP script is embedded in the HTML page, and the page becomes a JSP page. JSP scripts are enclosed by specific tags, such as <%... %> or <JSP>... </JSP>. The workflow of JSP is as follows: when it reads HTML code, it is directly output to the client. If it reads the embedded JSP script, you need to translate and process these scripts separately, and then output them to the client.

The following describes the framework of JSP network applications through a simple page:

1. JSP client interaction

Most of the work of JSP is to interact with the client. Clients generally refer to browsers that interact with JSP pages placed on servers. All Java statements in JSP scripts are executed on the server. The server accepts the requests submitted by the client. After some processing, the server returns the results to the client. The client only obtains HTML code. The following is a JSP HelloWorld program code:

 
 
  1. <html> 
  2. <head> 
  3. <title>HelloWorld for JSP</title> 
  4. </head> 
  5. <body> 
  6. <h1>HelloWord for JSP</h1> 
  7. <%out.println("<h3>Hello World!</h3>");%> 
  8. </body> 
  9. </html> 

2. Interaction Process

In the interaction between the client and the server, HTTP is used. The client interacts with the server through the following four steps:

1) establish a connection between the client and the server

2) Send client requests

3) The server returns a response to the customer.

4) Close the connection on the client

All requests are sent by the client, and the server keeps Listening passively. After you enter the address you want to access in the browser and press enter to confirm the connection, the browser starts to establish a connection with the server. From then on, an interaction process starts. After waiting for a while, the browser receives a response from the server and presents the corresponding information to the user in HTML format. When users browse the website, there are actually many such interactive response processes. During the interaction between the client and the JSP page, only the actions of the server receiving the request and returning the response may be different. The other actions are basically the same as those in the preceding process. The basic information is as follows:

1) The server first needs to analyze the request after receiving a request. If the requested page is only a common HTML page, the server will directly read the HTML page and return it to the client.

2) If the client requests a JSP page, the server calls the JSP page requested by the JSP engine Translation and returns the translated and processed HTML to the client.

3) In case of a JavaBeans component, the JSP Engine calls the corresponding JavaBeans component to obtain the return value of the JavaBeans and finally returns the result to the JSP page.

3. Working Principle of JSP Engine

When a JSP page is accessed for the first time, the JSP engine performs the following steps:

1) Translate JSP pages into a Servlet. this Servlet is a java file and a complete java program.

2) the JSP Engine calls the java compiler to compile the Servlet to obtain the executable file class.

3) the JSP Engine calls the Java Virtual Machine to explain the execution class file, generate a response sent to the client, and then send it to the client.

The preceding three steps are executed only when the JSP page is accessed for the first time. The later access speed will be greatly improved because the class file has been generated. When an access request is sent from a client in the JSP Engine Street, the system first checks whether the requested JSP page is newer than the corresponding Servlet. If yes, the corresponding JSP page needs to be re-compiled.

4. Processing request information

In most interactions, a JSP page first analyzes users' requests and obtains useful data from the requests, such as receiving user-submitted data or request methods, and then proceed with the corresponding processing. In this case, you need to know how to get the input parameters and how to get other user information.

1) obtain the input parameter method

During the interaction process, the server must obtain some data from the client to help with the process. The data includes user authentication data, such as the user name and password, and other required information. The carrier of the data is the request submitted by the user. The request consists of two parts: header information. Header information includes the following information: The request method GET and POST), the request URL, and browser information. Other information includes some data information.

The first thing to understand is that when the GET method sends a request, this method is used by the client to request information to the server segment, so the request does not contain the information body. You can only append the request content to the URL as a parameter, and separate the parameters with the "&" symbol, for example, code: get.html, a unique getrequest table.

 
 
  1. <html> 
  2. <head> 
  3. <title>Get</title> 
  4. </head> 
  5. <body> 
  6. <h1>Get Method</h1> 
  7. <form action = "submit.jsp" method = "GET"> 
  8.     username<input name = "uername" value = "aaa"><br> 
  9.     password<inpput type = password name = "password" value = "111111"><br> 
  10.     <input type = submit value = "submit"> 
  11. </form> 
  12. </body> 
  13. </html> 

There is a form in the Code. When a form is defined, the property method = "GET" is defined, which indicates that the form submission method is GET. This form submits two parameters to the server, one of which is username, the default value is aaa, and the other is password, and the default value is 111111 .)

This page only obtains the input data. only submit. JSP can submit the data on this page. JSP is required. The Code is as follows: submit. JSP

 
 
  1. <html> 
  2. <head> 
  3. <title>submit</title> 
  4. </head> 
  5. <body> 
  6. <h1>submit</h1> 
  7. request method:<%=request.getMethod() %><br> 
  8. user name:<%=request.getParameter("user") %><br> 
  9. password:<%=request.getParameter("password") %><br> 
  10. </body> 
  11. </html> 

On this JSP page, two methods of the JSP built-in object request are used. One is getMethod, which is used to obtain the type of the current request and the other is getParameter, which is used to obtain the value of the specified parameter. Click the submit button in get.html to display the form request method, user name, password, and other information.

2) how to obtain other information

To obtain other parameter information that is not entered by the user, other methods are required. The following is a simple program for obtaining client information. Based on this program, we can get a rough idea of how to obtain client information. The Code is as follows:

 
 
  1. <Html> 
  2. <Head> 
  3. <Title>Get message</Title> 
  4. </Head> 
  5. <Body> 
  6. <H1>URL contained in the request</H1><Br> 
  7. <% = Request. getQueryString () %><Br> 
  8. </Body> 
  9. </Html> 
  1. Call JavaBean in JSP
  2. Establishment of JSP development environment
  3. Three methods for displaying Chinese Characters in JSP Web programs
  4. Develop jsp http Server
  5. JSP, ASP, and PHP security programming

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.