< about JSP technology > Operation mechanism and Grammar (with The Spit groove in the final of ACM competition in Tongji University)

Source: Internet
Author: User
Tags send cookies

A The mechanism of JSP operation

JSP is a Dynamic Web page technology built on the servlet specification, which embeds script code in a common Web page file for generating dynamic content, but unlike ASP, the JSP file is embedded with Java code and JSP tags. In fact, the JSP file in the user's first request will be compiled into a servlet, and then by the servlet processing the user's request, so the JSP can also be seen as a runtime servlet, the JSP and the servlet is vaguely the same thing. But what are the differences between them?

1.Servlet is Java's response to CGI (Common Gateway Interface), which performs and interprets the browser's content requests on the server, assuming a middle-tier role between the client and other applications. A servlet is primarily a dynamic mix of content into static content to produce HTML for response.

The 2.JSP page embeds java script code and JSP tags in the HTML page, which makes the file length shorter and the format clearer. On the other hand, JSP separates static and dynamic content, and realizes the separation representation of content.

3.JSP use process, do not need to configure each file separately, as long as the extension is. The jsp,jsp container is automatically recognized and converted into a servlet for client service.

The JSP container manages two stages of the JSP page life cycle: The transition phase and the execution phase, when a client request to the JSP page arrives, the JSP container verifies that the JSP page syntax is correct, and the JSP page is converted to a servlet source file. Then call the Javac tool class to compile the servlet source file to generate the bytecode file, this is in the transformation phase, next, the servlet container loads the transformed Servlet class, instantiates an object to handle the client's request, the object that responds after the request processing is completed will be accepted by the JSP container. The container sends the HTML-formatted response information to the client, which is the stage of execution.

As shown, the first time the JSP page is loaded, because the JSP file to be converted to the Servlet class, so the response is slow, when the request again, the JSP will directly execute the first request to produce the servlet, and not to re-convert the JSP file, So its execution speed will be almost the same as running the servlet directly, during the execution of the JSP, the system will check the JSP file to see if there are any updates or modifications. If so, the JSP container will compile the JSP or servlet again, and if it is not updated or modified, it will run directly before the servlet that was generated.

As defined in the JSP2.0 specification, the servlet class after the JSP page conversion must implement the Javax.servlet.jsp.JspPage interface (similar to the servlet, the servlet class must implement the Javax.servlet.Servlet interface, which inherits from JAV Ax.servlet.Servlet interface) In addition to inherited methods, the Jsppage interface defines the following two methods:

public void Jspinit ()

This method is called when the JSP page is initialized, similar to the init () method in the servlet. Programmers can override this method in the JSP declaration, but it is best to call Super.init () in the subclass.

public void Jspdestroy ()

This method is called when the JSP page is destroyed, similar to the Destroy method in the servlet, where the page writer can override this method in the JSP's declaration element to perform its own clear finishing work.

Because in the vast majority of cases, JSP pages use the HTTP protocol, So the JSP page converted Servlet class must actually implement the Javax.servlet.jsp.HttpJspPage interface, which inherits from the Jsppage interface, except for the inherited method, the Httpjsppage interface only defines a method:

public void _jspservice (httpservletrequest request,httpservletresponse response)

Do you feel very gracious to see this method? The service method in this servlet class is simply the biological brother, and of course our programming often covers the doget,dopost of methods such as the essence of a switchbox in the service function to implement the separation of the service method wrapper.

Two An overview of the syntax of JSP

# # on the JSP syntax has been understood by skilled workers can ignore this paragraph # #

The 1.page directive acts on the entire JSP page, defining many page-related properties that will be used to communicate with the JSP container.

<%@ page attribute1= "xxx" atrribute2= "xxx" ...%> | | <jsp:directive.page attribute1= "xxx" attribute2= "xxx" .../>

There are 15 main properties of the page directive:

Language,extends,import,session,buffer,autoflush,isthreadsafe,info,errorpage,iserrorpage,contenttype, Pageencoding,iselignore,defferredsyntaxallowedasliteral,trimdirectivewhitespaces

The 2.include directive is used to statically include a file in a JSP page, which can be a JSP page, an HTML page, a text file, or a piece of Java code. JSP pages that use the include directive are automatically inserted into the text or code of the contained file when the JSP container is converted. (The value of the file's property is interpreted as the URL of the current JSP file)

<%@ include file= "xxx"%> | | <jsp:directive.include file= "xxxx"/>

The 3.TAGLIB directive allows the page to use a new user-defined tag:

<%@ taglib (uri= "Taglibraryuri" | tagdir= "Tagdir") prefix= "TagPreFix"%> | | <jsp:directive.taglib (uri= "Taglibraryuri | ") >

4. Script Elements  <%! .. %><%. %><%= ...%> The three elder tags, I'm not going to go over it.

Three Hidden objects in JSPs

In the _jspservice () method of the Servlet class generated in the JSP container, several hidden objects are defined, and these are hidden objects that we can use when writing JSP pages. It is important to note that because these implicit objects are defined in the methods in _jspservice, we can only use them in script segments and expressions.

In the JSP page, there are 9 hidden objects: Request,response,pagecontext,session,application,out,config,page and exception, in the HTTP protocol-based implementation, The correspondence between these 9 hidden objects and their respective classes is obvious.

Here are some of the more important hidden objects request,response,session,config and cookies (this is counted as a turn!). Talk together! )

1.request,response

public void GetAttribute (String name)

Public enumeration Getattributenames ()

public void RemoveAttribute (String name)

public void SetAttribute (String name,object obj)

Public String getcharacterencoding ()

public int getcontentlength ()

Public String getcontentlength ()

Public Servletinputsream getInputStream ()

Public String getparameter (name)

Public enumeration Getparameternames ()

Public string[] Getparametervalues (String name)

Public PrintWriter getwriter () throws IOException

public void Reset ()

Public BufferedReader Getreader () throws IOException

2.session,config

Config is usually passed in as a parameter to the Init method to assign a value to the config inside the servlet class, so this config contains the values of the parameters we set in Web. Xml.

public string Getinitparameter (string name)

Public enumeration Getinitparameternames ()

Public ServletContext Getservletcontext ()

Public String Getservletname ()

Session is used to track the status of sessions and management sessions, using the session, the server can be a customer all the requests are linked together, and remember the customer's operational status, when the first time the customer connected to the server, the server will give him a session, And give the customer a unique SessionID identity, after each submission of the customer request, will be submitted together with the identity, the server based on the identification of the specific session, the session to record the customer status, the process according to the customer's HTTP request to respond. From the above process, we can see that by including SessionID in each request and response, the server can separate one user from another user.

Three mechanisms for user session tracking: 1. SSL (Secure Socket Layer) session 2.Cookies 3.URL rewrite

To put it simply, SSL is the encryption technology that runs on TCP/IP and HTTP as an application layer, SSL is the encryption technology used in the HTTPS protocol, SSL allows SSL-based servers to authenticate clients with SSL. and to maintain an encrypted connection between the client and the server, in the process of establishing an encrypted connection, both the client and the server can produce something called a "session key", which is a symmetric key for encrypting and decrypting the message, and the HTTPS protocol-based server can use the client's symmetric key to establish the session.

The cookie is a key-value-pair method of recording the content of the session tracking, the server uses the response packet header Set-cookie to send cookies information:

The format of the response header is: Name=value comment=value domain=value max-age=value path=value Secure version=1*digit

1. The main interface method for the session is as follows:

Public Object getattribute (String name)

Public enumeration Getattributenames ()

public void SetAttribute (String name,object value)

Public HttpSession getsession ()

2. The main interface methods for cookies are as follows:

Public String getcomment ()

public void Setcomment (String c)

Public String GetName ()

Public String GetValue ()

public string SetValue (string newvalue)

Public String GetPath ()

Spit Groove stage:!!

The ACM qualifying final is a pit of a father to see this ranking has wood, pro have not found the second question who did not have to do a wood there!?

Yes, there is a problem with the validation answer data for this question!! And we just wasted two hours on this, wrote a huge test data run a run, is not find out the bug I Go

And the difficulty of the game is completely reversed---B when I shook my hands to hold the last question, I laughed and sprayed, a number of key words of a large sort

Fortunately I learned C + +:-P directly with the <algorithm> Stable_sort wrote the five lines ac off wow ha haha still did not be shortlisted despite the AC three row 41. Have a chance to see you next year. (the person who wrote the verification answer--)

< about JSP technology > Operation mechanism and Grammar (with The Spit groove in the final of ACM competition in Tongji University)

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.