JSP insider

zhaozj2021-02-08  481

Author: ybwen

The current enterprise application has been transferred from 2-Tier towards 3-Tier structures, and developers have many different ways to distribute applications and data to users. JSP API is part of J2EE, which provides a simple but powerful mechanism to create a variety of applications for Web and Java developers.

JSP provides two important capabilities to developers: 1. It provides mechanisms to access remote data such as EJB, RMI, JDBC. Second, it allows developers to achieve maximum code reuse and flexibility from the logic of the performance from the performance. Separation of logic and performance is a major advantage that transcends other web application structures.

In this article, I will have an overall description of JSP technology, of course, there will be some examples to demonstrate the advantages of JSP.

First, how JSP is executed

To support JSP, the web server must have a corresponding configuration. The popular web server supported by JSP is: WebSphere, JWEBServer, JSWDK, JRUN, TOMCAT, and more.

When the web server receives a request for the ".jsp" page, it triggers the JSP engine. The JSP engine first checks the JSP file is new or modified, and the file will be translated and compiled with the files. Translation is the core of how JSP works: The JSP engine takes a JSP file to convert it to Java servlet. Then use the standard JVA compiler to compile the servlet, and perform the Java Servlet using the standard API. This translation step is also a slow reason why JSP is slow.

Write a JSP

An example of a JSP is as follows:

<% @ page language = "java" info = "form processing page"%>

HTML

Body

<%! String cities [], month;%>

Pthe Cities SELECTED ARE:

/ P

<%

CITIES = Request.getParameterValues ​​("City");

Month = Request.getParameter ("MONTH");

IF (citive! = null) {

%>

UL

<%

For (int i = 0; i

%>

Li <% = CITIES%>

<%

}

%>

/ UL

<%

}

Else {

%>

Pnone selected.

/ P

<%

}

%>

Br

Pand the successmed month is:

B <% = MONTH%>

/ B

/ P

/ body

/ html

The JSP tag allows the Java code to be embedded directly in HTML, as long as this page is requested.

The JSP label has the following types:

Label Description JSP 1.0 syntax

Comment allows inline document comments HTML Comment: <- Comment ->

JSP Comment:

% - Comment -%

Directive provides an instruction to the JSP engine when the page is translated

Expression contains variables or constants, which will be calculated when the page is requested

The Scriptlet contains a code snippet that will be executed when the page is requested.

JSP executes JSP specific functions when the page is requested

Comments. There are two types of comments in JSP. Standard HTML Comment

--comment - it can be sent to the client browser. JSP comment

% - Comment -% it does not appear in HTML.

Directive: When the JSP page is translated into servlet, it indicates JSP engine processing.

Declarations: Define variables in an expression or script segment.

Expressions: Using an expression, you can dynamically calculate the value inserted into the JSP page.

ScriptleTs: embed Java code blocks in the page.

JSP: Based on normal label library syntax.

Equal form of simple FORM processing

Form.jsp is a simple JSP page that is used to process the request for the HTML table.

Form.html is a typical HTML Form

The source file of form.html is as follows:

HTML

Body

Shenzhen

Br

beijing

Br

Shanghai

Br

GuangzhouGdong

Br

Br

january

Br

june

Br

july

Br

September

Br

Br

/ form

/ body

/ html

The value of the control in the table can be accessed via the Request object, which is an instance of javax.servlet.ServletRequest (but usually using a more detailed javax.servlet.http.httpservletRequest). The JSP generated code is performed only a standard servlet's service () method, which has two parameters Request to the response object.

Use JavaBeans in JSP pages

Feedback.jsp is another example of processing for Form, except that JavaBean: Mailbean.java is used in JSP. If you are not familiar with JavaBeans, JavaBean is a Java component with properties, methods, events. Feedback.html is HTML for requesting feedback.jsp. The feedback.jsp source code is as follows:

HTML

Title

Results of sending the message

/ Title

Body

<%

Boolean Success = mail.sendMessage ();

IF (Success) {

%>

PYOUR Message Has Been Sent!

/ P

PTHANKS.

/ P

<%

}

Else {

%>

Pyour message was not Sent for Some REAASON.

/ P

Pplease Go Back and Try Again

/ P

<%

}

%>

/ body

/ html

The feedback.html source code is as follows:

HTML

HEAD

Titleemail Feedback Form

/ Title

/ HEAD

Body

Br

PDO you have feedback to provide to us?

/ P

PYOUR Email Address:

/ P

->

psending to:

/ P

PYOUR Comments:

/ P

P