One of the introduction of JSP technology

Source: Internet
Author: User
Tags expression html page require

After Sun officially released JSP (JavaServer Pages), this new Web application development technology quickly caught people's attention. JSP provides a unique development environment for creating highly dynamic Web applications. According to Sun, JSP can adapt to the market, including Apache WebServer, IIS4.0 and other 85% of the server products. Even if you are "madly" to the ASP, we believe that it is still necessary to focus on the development of JSP.

A simple comparison between ㈠jsp and ASP

JSP is very similar to Microsoft's ASP technology. Both provide the ability to mix some kind of program code in HTML code, and the language engine to interpret the execution code. In an ASP or JSP environment, HTML code is primarily responsible for describing the display style of information, while program code is used to describe the processing logic. Plain HTML pages rely on WEB servers only, while ASP and JSP pages require additional language engine analysis and code execution. The execution results of the program code are then embedded in the HTML code and sent to the browser together. Both ASP and JSP are Web server-oriented technologies, and client browsers do not require any additional software support.

ASP's programming language is the scripting language such as VBScript, JSP uses Java, this is the most obvious difference between the two. In addition, there is a more fundamental difference between ASP and JSP: The two language engines handle embedded program code in a page in a completely different way. In ASP, VBScript code is interpreted by the ASP engine, and in JSP the code is compiled into a servlet and executed by the Java virtual machine, which occurs only on the first request to the JSP page.

㈡ Operating Environment

Sun's JSP home page in http://www.javasoft.com/products/jsp/index.html, where you can also download JSP specifications that define some of the rules that vendors must follow to create a JSP engine.

Executing JSP code requires the JSP engine to be installed on the server. Here we are using Sun's JavaServer Web Development Kit (JSWDK). For ease of learning, this package provides a large number of examples to modify. After you install JSWDK, you can start the server by simply executing the startserver command. The server listens on port 8080 under the default configuration and opens the default page using http://localhost:8080.

Before running the JSP sample page, be aware of the directory where you installed JSWDK, especially the contents of the "work" subdirectory. When you execute the sample page, you can see here how the JSP page is converted into a Java source file and then compiled into a class file (that is, a servlet). The sample pages in the JSWDK package are grouped into two categories, either JSP files or HTML files that contain a form that is processed by JSP code. Like the ASP, Java code in the JSP executes on the server side. Therefore, using the View Source menu in the browser is not possible to see the JSP source code, only to see the resulting HTML code. The source code for all the samples is provided through a separate "examples" page.

㈢jsp Page Sample

Below we analyze a simple JSP page. You can create another directory in the JSWDK examples directory where the file name can be arbitrary, but the extension must be. jsp. As you can see from the code listing below, the JSP page has essentially the same structure as a few more Java code than a normal HTML page. Java code is added to the HTML code through the% and%> notation, and its main function is to generate and display a string from 0 to 9. At the front and back of this string are some text that is output through the HTML code.

< HTML>
< HEAD>< TITLE>JSP 页面 < /TITLE>< /HEAD>
< BODY>
< %@ page language="java" %>
< %! String str="0"; %>
< % for (int i=1; i < 10; i++) {
str = str + i;
} %>
JSP 输出之前。
< P>
< %= str %>
< P>
JSP 输出之后。
< /BODY>
< /HTML>

This JSP page can be divided into several sections to analyze.

The first is the JSP directive. It describes the basic information about the page, such as the language used, whether the session state is maintained, whether buffering is used, and so on. JSP instruction starts with%@,%> ends. In this case, the instruction "%@ page language=" java "%>" simply defines the Java language (currently the only supported language in the JSP specification).

The next is the JSP declaration. A JSP declaration can be viewed as a place to define variables and methods at this level of class. JSP declaration by%! Start,%> end. As in this case, "%!" String str= "0"; %> "defines a string variable. After each declaration, you must have a semicolon, just as you would declare a member variable in a normal Java class.

Code blocks between% and%> are Java code that describes the processing logic of JSP pages, as shown in the For loop in this example.

Finally, the code between%= and%> is called a JSP expression, as shown in the "%= str%>" in this example. A JSP expression provides a simple way to embed a JSP-generated value into an HTML page.

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.