Servlet & JSP (10)

Source: Internet
Author: User

The main content I discussed earlier is servlet. In fact, my intention is to only discuss servlet. However, since JSP is an indispensable part of Java Web development, there is no harm in learning more. In the previous content, we found that when using Servlet to generate a dynamic web page, we need to output HTML tags in the code. In addition, static code and dynamic code have to be mixed together. If you use servlet to develop dynamic web pages, programmers and web page editors cannot work together. Because Web editors may not understand Java, Servlet Code cannot be modified. That's why Sun was acquired by Oracle .) JSP (Java
Server pages ).

Introduction

You may understand ASP. Like ASP, JSP is embedded with script code in HTML files to generate dynamic content, but JSP is embedded with Java statements. The JSP file will be compiled into a servlet when the user requests for the first time and then processed by the servlet. Therefore, jsp can also be seen as a runtime servlet. Speaking of this, you may be wondering, in this case, why is the difference between JSP, JSP, and Servlet used?

1) servlet executes and interprets browser requests on the server as the intermediate layer of the client and other applications. Servlet is mainly used to mix dynamic content into static content to generate HTML.
2) the JSP page is embedded with Java Script code and JSP tag in HTML to shorten the file length and clear the format. In addition, JSP separates static and dynamic content to separate content and representation.
3) when using JSP, you do not need to configure each file separately, as long as the extension is. jsp, JSP Container (that is, servlet container) will automatically identify and convert it to servlet.

JSP Running Mechanism

Let's compile a simple JSP file. First create a folder Hello under the D: \ apache-Tomcat-7.0.33 \ webapps directory, and then create a folder WEB-INF and file hello. jsp in the Hello folder. The Hello. JSP code is as follows:

Let's start Tomcat, and enter http: // localhost: 8080/Hello. jsp in the URL bar of the browser. Hello and JSP are displayed on the page !. What is the process in the middle? Open the D: \ apache-Tomcat-7.0.33 \ work \ Catalina \ localhost directory and find the hello folder. Open it in the hello \ org \ apache \ JSP folder, we can see two files: hello_jsp.java and hello_jsp.class. This is generated by the JSP Container when you access hello. jsp.
The JSP Container manages two stages of the JSP page lifecycle: the conversion phase and the execution phase. When a request arrives, the JSP Container first checks whether the JSP syntax is correct. If yes, convert the JSP file to a Servlet File and call javac to compile and generate the. Class file. This is the conversion phase. Next, the servlet container loads the converted servlet class and instantiate an object to process client requests. After the request is complete, the response object is received by the JSP Container, the container sends the Response Message in HTML format to the client, which is the execution phase.

Now let's learn about JSP syntax.

JSP pages are divided into two parts: Elements and template data. Elements must be processed by JSP containers, while template data is not processed by JSP containers. For example, the HTML Statement on the JSP page. JSP has three types of elements: command elements, script elements, and Action elements.

Element type in JSP

1) Directive Element)

The command element is used to provide information related to the entire JSP page for the conversion phase. The command does not generate any output to the current output stream. Syntax format:

<%@ directive {attr="value"}* %>

Command elements include page, include, and taglib.

The page command syntax is:

<%@ page attr1="value1" attr2="value2" ...%>

Syntax of XML format:

<jsp:directive.page attr1="value1" attr2="value2" .../>

The page command has 15 attributes. Language, extends, import, session, buffer, autoflush, isthreadsafe, info, errorpage, iserror, contenttype, pageencoding, iselignored, deferredsyntaxallowedasliteral, trimdirectivewhitespaces. Yes, right? Dazzled, right? In fact, you only need to know about it. You can check the information when using it. Here are a few introductions.

The language is used to specify the script language used in the script element. It is Java. Extends is used to instruct the class from which the servlet class is inherited after JSP page conversion. Import is used to specify the Java classes that can be used in the script environment. If multiple classes are referenced, the classes are separated by commas. Session is used to specify whether a JSP page participates in a session. The value is true or false. Errrorpage is used to specify the error handling page to be redirected when an exception occurs on the JSP page. Note: if an error processing page is specified, the error processing page defined in the web. xml file will not be used. Contenttype specifies the MIME type and character encoding of the JSP page used for response. Pageencoding is used to specify the character encoding used by JSP pages.

The include command is used to statically include a file on a JSP page. The file can be a JSP page, HTML page, text file, or a Java code. The include command syntax is:

<%@ include file="relativeRULspec" %>

Syntax of XML format:

<jsp:directive.include file="relativeRULspec" .../>

The value of the file attribute is the URL relative to the current JSP file. (Note: it is best not to use tags such as <HTML> to prevent conflicts with the original JSP file .)

The taglib command allows the page to use custom labels. Taglib command syntax:

<%@ taglib (uri="tagLibraryURI" | tagdir="tagDir") prefix="tagPrefix" %>

Syntax of XML format:

<jsp:directive.taglib (uri="tagLibraryURI" | tagdir="tagDir") prefix="tagPrefix" .../>

The taglib command has three attributes: URI, tagdir, and prefix. The unique identifier of the URI and the tag library descriptor related to the prefix, which can make the URI relative to the absolute fire. Tagdir indicates that the prefix will be used to identify the Tag file installed in the/WEB-INF/tags/directory or its letters. Prefix defines a string prefix in the format of Prefix: tagname, which is used to distinguish multiple custom tags.

2) scripting Element)

The script element consists of three parts: Declaration, script segment, and expression.

The Declaration script element is used to declare the variables and methods used in the JSP page scripting language. It must be a complete declaration statement and follow the Java syntax. It does not generate any output in the current output stream. Syntax:

<%! declaration(s)%>

The declaration in XML format is as follows:

<jsp:declaration> declaration(s) </jsp:declaration>

Note: Do not forget the; sign next to the variable when declaring the variable. The statement is valid only on the current JSP page.

The script segment is the Java code segment to be executed during request processing. The syntax format is as follows:

<% scriplet %>

The script segment in XML format is as follows:

<jsp:scriplet> scriplet </jsp:scriplet>

The expression script element is a complete expression in Java. These expressions are calculated during request processing. The calculation result is converted to a string and inserted into the current output stream. The syntax is as follows:

<%= expression %>

The syntax of XML format is as follows:

<jsp:expression> expression </jsp:expression>

Note: When writing an expression, do not add any punctuation marks after the expression.

3) Action Element)

Action elements provide information for the request processing phase. The jsp2.0 Specification defines 20 standard action elements.

<JSP: usebean>, <JSP: setproperty>, and <JSP: getproperty> are used to access JavaBean.
<JSP: param> Provide additional information for other tags in the form of name-value pairs, which are used together with <JSP: Include>, <JSP: Forward>, and <JSP: plugin>. The format is as follows:

<jsp:param name="name" value="value" />

Name indicates the parameter name. value indicates the parameter value. It can be an expression.

<JSP: Include> it is used to include static and dynamic resources on the current page. Once the contained page is executed, request processing continues on the call page.
<JSP: Forward> allows you to forward the current request to a static resource, JSP, page, or servlet at runtime, the resource to which the request is redirected must be in the same context as the jsp request.

Other action elements are not described here. If you are interested, you can view the information by yourself.

4) annotations

The HTML annotation syntax is:

<!-- comments -->

JSP annotation Syntax:

<%-- comments --%>

In addition, you can also use the Java language annotation mechanism as follows:

<% // comments %><% /* comments*/ %>

Implicit JSP object

For example, the out object we use in hello. jsp is one of them. In addition to out, there are eight other hidden objects. The nine implicit objects and their types are shown in table 1:

Implicit object Type
Request Javax. servlet. http. httpservletrequest
Response Javax. servlet. http. httpservletresponse
Pagecontext Javax. servlet. jsp. pagecontext
Session Javax. servlet. http. httpsession
Application Javax. servlet. servletcontext
Out Javax. servlet. jsp. jspwriter
Config Javax. servlet. servletconfig
Page Java. Lang. Object
Exception Java. Lang. throwable

Table 1 Types of JSP implicit objects

Reprinted please indicate the source: http://blog.csdn.net/iAm333

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.