Basic JSP Syntax: Comment, scriptlet, and compile command

Source: Internet
Author: User

JSP (Java Server Page)

JSP is simply HTML + Java code, and the file suffix is. jsp.

JSP and Servlet are the important foundations of javaee. It seems that JSP and Servlet are two completely different things, but JSP is actually a form of servlet, and JSP needs to be compiled into servlet eventually;

JSP pages must be deployed in Web containers for access, because JSP pages alone are useless. JSP pages must be converted to servlets for use;

JSP is object-oriented, because it will eventually become servlet, and servlet is a class, which may not be easy to understand. The following will explain.

The result of the servlet output stream when the user accesses the output displayed on the JSP page;

JSP can not handle exceptions, because there are two ways to handle exceptions:

1. display on the browser page;

2. Jump to errorpage;

JSP to servlet conversion when a JSP file is accessed for the first time, the web server converts the JSP file into a servlet, and then the servlet provides the output stream to output HTML to the customer. therefore, the customer only contacts HTML, and the JSP processing process is completed on the server. Note: Each servlet class has only one object in the container, therefore, the same JSP page accessed by each customer is a servlet object;

Suppose there is a null. jsp file. The file content is blank, as shown below:

<%@ page contentType="text/html;charset=utf-8" pageEncoding="utf-8" info="This is a JSP " %>

The null_jsp.java and null_jsp.class are generated in the Tomcat work directory. The code for null_jsp.java is as follows:

Package Org. apache. JSP; import javax. servlet. *; import javax. servlet. HTTP. *; import javax. servlet. JSP. *; // httpjspbase is a subclass of Servlet/* this class mainly consists of three methods: 1. init (); initialize 2. destroy (): Destroy 3. service (); receives client requests and processes */public final class null_jsp extends Org. apache. jasper. runtime. httpjspbase implements Org. apache. jasper. runtime. jspsourcedependent {Public String getservletinfo () {return "this is a JSP";} Private Static fi NAL jspfactory _ jspxfactory = jspfactory. getdefafactory Factory (); Private Static Java. util. list <string> _ jspx_dependants; private javax. el. expressionfactory _ el_expressionfactory; private Org. apache. tomcat. instancemanager _ jsp_instancemanager; Public Java. util. list <string> getdependants () {return _ jspx_dependants;} public void _ jspinit () {_ el_expressionfactory = _ jspxfactory. getjspapplicationcontex T (getservletconfig (). getservletcontext ()). getexpressionfactory (); _ jsp_instancemanager = org. apache. jasper. runtime. instancemanagerfactory. getinstancemanager (getservletconfig ();} public void _ jspdestroy () {} public void _ jspservice (final httpservletrequest request, final httpservletresponse response) throws Java. io. ioexception, servletexception {// generate the JSP built-in object final pagecontext; httpses Sion session = NULL; Final servletcontext application; Final servletconfig config; jspwriter out = NULL; final object page = This; jspwriter _ jspx_out = NULL; pagecontext _ jspx_page_context = NULL; try {response. setcontenttype ("text/html; charset = UTF-8"); pagecontext = _ jspxfactory. getpagecontext (this, request, response, null, true, 8192, true); _ jspx_page_context = pagecontext; application = page Context. getservletcontext (); Config = pagecontext. getservletconfig (); Session = pagecontext. getsession (); out = pagecontext. getout (); _ jspx_out = out; // output the display out on the JSP page. write ("\ r \ n"); out. write ("<HTML> \ r \ n"); out. write ("\ t <body> \ r \ n"); out. write ("\ r \ n"); out. write ("\ t </body> \ r \ n"); out. write ("

From the above code, we can clearly see the relationship between JSP and Servlet. Both the <%> <% = %> and HTML code written by JSP are implemented in the service function;

And <%! The code in %> serves as the member variables and member methods of the servlet class;

JSP built-in objects are created in this file.

I. Notes

There are comments in one language, and JSP has two types of comments. You can view the source code of the web page and see the explicit comments instead of the implicit comments.

(1) Explicit comment: <! -- Explicit comment -->
(2) Implicit comments:

// Implicit Annotation

/* Implicit comment */

<% -- Implicit comment -- %>

2. Insert Java code in HTML: 1. scriptlet: script Applet

(1)<%... %>: Some statements can be inserted.

Example: <% Out. println ("<H2> Hello World </H2>"); %> indicates that a hello World statement is output to the webpage.

(2)<%! ... %>: Only global variables, global constants, classes, and functions can be placed.

Example:

<%!

Public static final string info = "Hello World ";

Class person {

.....

}

%>

Note: <%! The built-in object cannot be used in %>!

(3) <% =... %>: only one variable and constant can be placed.

Example: <% = "Hello World" %>

2. <JSP: scriptlet> tag

Insert a statement in <JSP: scriptlet> </jsp: scriptlet>.

Iii. compile instruction Compilation instruction template: <% @ instruction %> 1. Page instruction

The page command is placed at the top of the JSP file and describes some information, in the form of <% @ page property = "value" %>;

Attributes can include:

(1) contenttype: The content type of the webpage, that is, the MIME type, usually contenttype = "text/html"; the character set can also be set, often contenttype = "text/html; charset = GBK ";

(2) language: indicates the language used by the script. Currently, only language = "Java" is supported ";

(3) pageencoding: indicates the page encoding method, usually pageencoding = "GBK ";

(4) import: indicates the package to be imported when writing Java code. For example, to use JDBC, You need to import Java. SQL. *, It is import = "Java. SQL. *";

(5) errorpage: the page to jump to when an error occurs, but the iserrorpage attribute must be set for the error page at the same time.

Note: you must first set response. setstatus (200) on the error page.


JSP does not need to handle exceptions because of the errorpage attribute. The errorpage attribute is similar to catch {}. If an exception exists, the error page is redirected.


(6) iserrorpage: If a page is an error page, it must be set to iserrorpage = "true ";

Add error pages and Exception Handling pages to the entire virtual directory:

Add the following content to the Web. xml file in the virtual directory:

<Error-page>

<Error-code> 404 </error-code>

<Location>/error/clienterror. jsp </location>

</Error-page>

<Error-page>

<Exception-type> JAVA. Lang. nullpointerexception </exception-type>

<Location>/error/exception. jsp </location>

</Error-page>

Note: After configuring web. XML, restart the Tomcat server;

Comprehensive example:

<% @ Page Language = "Java" contenttype = "text/html; charset = gb2312" pageencoding = "gb2312" Import = "Java. util. * "errorpage =" error.html "%>

Note:

(1) If you want the page to recognize Chinese characters, you must set the pageencoding attribute.

(2) MIME type: Key ---> value. Key indicates the file suffix, and value indicates the application type that opens the suffix. In CONF/Web. xml of Tomcat, you can view the MIME type and its open mode.

For example, if it is set to application/MSWord, it is opened with word. After you type a webpage, It is downloaded first, and then it can be opened with word.

Change the name by using response. setheader ("content-disposition", "attachment?filename=xiazdong.doc;

(3) html and htm are the same.

(4) pageencoding specifies the page encoding of JSP, while contenttype is the client encoding.

2. include command

<% @ Include file = "FILENAME" %>

This is a static file to be included.Integrate into a fileSo we can see from the Java file generated by Tomcat.

Dynamic inclusion calls the include function to include a file, rather than integrating it into one;

The above differences cannot be seen externally. You need to observe the automatically generated servlet class to see them;

The disadvantage of static inclusion is that parameters cannot be passed;

4. Client jump and server jump:

(1) Client jump: This type of jump will change the address in the address bar to the jump address, such as. JSP ---> B. JSP. The original address bar is. JSP, jump to the address bar to B. JSP, the jump belongs to the client jump.

(2) server jump: the address bar does not change after the jump, andRequest Parameters will not be lost. For example, if a. jsp --> B. jsp is maintained in the address bar, server jump is called.

 

The errorpage jump we just encountered is a server jump.

 

V. database connection:

Take MYSQL as an example. The configuration step is to put the JDBC driver of MySQL into tomcat/lib. We usually put all third-party jar packages here.

Note that you need to add the try statement. The other statements are similar to those of JDBC.

 

6. include commands)

 

Simply put, it contains an HTML/JSP/htm file, which can be divided into two types:

(1) Static inclusion: includes and then compiles. Format: <% @ include file = "demo.html" %>

Example:. JSP, B. JSP, C. JSP, in. JSP static contains B. JSP, C. JSP means that B. JSP, C. JSP code is written into. JSP, and then compile and generate the class file.

(2) Dynamic inclusion: for a dynamic page, first compile and then include. Format: <JSP: Include page = "demo. jsp"/>

For example, A. jsp, B. jsp, and C. jsp dynamically include B. jsp and C. jsp in A. jsp, which means that the class file is compiled and then included together.

Dynamic inclusion can also pass Parameters, Note:Is the include page ---> included page,Shape:

<JSP: Include page = "demo. jsp"/>

<JSP: Param name = "name" value = "xiazdong"/>

<JSP: Param name = "Age" value = "20"/>

</Jsp: Include>

The contained page is received by request. getparameter.

7. Jump command (<JSP: Forward>) server jump

(1) Jump commands without passing parameters:


<JSP: Forward page = "demo. jsp"/>

(2) Redirect command for passing parameters:


<JSP: Forward page = "demo. jsp">

<JSP: Param name = "name" value = "xiazdong"/>

<JSP: Param name = "Age" value = "20">

</Jsp: Forward>

Request. getparameter (""); receive, in fact, because this jump is a server jump, you can also set the request built-in object settings attribute for transmission, JSP built-in object will be described later.

 

 

Supplement: JSP Lifecycle

 

1. The Web Container reads the configuration information in Web. xml;

2. When the customer sends a jsp request, the JSP is translated into a Servlet and compiled into a class file;

3. container loading servlet;

4. The Container instantiates the servlet and calls the jspinit () method;

5. The Container creates a servlet thread and calls the service method;

6. Call destroy ();

 

 

 

 

 

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.