Introduction to ASP and JSP pages

Source: Internet
Author: User

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

1. simple comparison between ASP and JSP

JSP is very similar to Microsoft's ASP technology. Both provide the ability to mix some program code in HTML code and explain and execute the program code by the Language engine. In ASP and JSP environments, HTML code describes the display style of information, while program code describes the processing logic. Common HTML pages only depend on Web servers, while ASP and JSP pages require additional language engines to analyze and execute program code. The execution result of the program code is re-embedded into the HTML code and sent to the browser together. ASP and JSP are both Web server-oriented technologies, and client browsers do not require any additional software support.

ASP programming language is a script language such as VBScript, and JSP uses Java, which is the most obvious difference between the two. In addition, there is a more essential difference between ASP and JSP: the two language engines process the program code embedded in the page in a completely different way. In ASP, the VBScript Code is interpreted and executed by the ASP engine. In JSP, the code is compiled into a Servlet and executed by the Java Virtual Machine. This compilation operation only occurs during the first request to the JSP page.

2. Runtime Environment

Sun's JSP home page is in http://www.javasoft.com/products/jsp/index.html, where JSP specifications can also be downloaded, which define some of the rules that suppliers must follow when creating JSP engines.

To execute JSP code, you must install the JSP engine on the server. Here we use Sun's assumerver Web Development Kit JSWDK ). For ease of learning, this software package provides a large number of examples that can be modified. After JSWDK is installed, run the startserver command to start the server. In the default configuration, the server listens on port 8080 and uses http: // localhost: 8080 to open the default page.

Before running the JSP Sample Page, pay attention to the JSWDK installation directory, especially the content in the "work" subdirectory. When executing the sample page, you can see how the JSP page is converted to a Java source file, and then compiled into a class file, that is, Servlet ). The sample pages in the JSWDK package are divided into two types: JSP files, HTML files containing a form, which are processed by JSP code. Like ASP, Java code in JSP is executed on the server. Therefore, you cannot view the JSP Source Code by using the "View Source File" menu in the browser. You can only see the result HTML code. The source code of all examples is provided through a separate "examples" page.

3. JSP page example

Next we will analyze a simple JSP page. You can create another directory in the examples directory of JSWDK to store this file. The file name can be arbitrary, but the extension must be. jsp. From the code list below, we can see that JSP pages have the same structure except Java code more than ordinary HTML pages. Java code is added to the HTML code through the <% and %> symbols. Its main function is to generate and display a string from 0 to 9. The front and back of this string are some text output through HTML code.

 
 
  1. <HTML>
  2. <HEAD> <TITLE> JSP page </TITLE> </HEAD>
  3. <BODY>
  4. <% @ PageLanguage="Java"%>
  5. <%! StringStr="0"; %>
  6. <% For (intI=1; I <10; I ++ ){
  7. StrStr= Str + I;
  8. } %>
  9. Before JSP output.
  10. <P>
  11. <% = Str %>
  12. <P>
  13. After JSP output.
  14. </BODY>
  15. </HTML>

This ASP and JSP page can be analyzed in several parts.

The first is the JSP command. It describes the basic information of the page, such as the language used, whether to maintain the session status, and whether to use buffering. JSP commands start with <% @, and end with %>. In this example, the command "<% @ page language =" java "%>" only briefly defines that the current Java language is used in this example, java is the only supported language in JSP specifications ).

The following is the JSP declaration. The JSP declaration can be seen as a place where variables and methods at the class level are defined. JSP declaration by <%! Start, %> end. In this example, "<%! String str = "0"; %> "defines a String variable. Each declaration must be followed by a semicolon, just like declaring a member variable in a common Java class.

The code block between <% and %> is the Java code that describes the JSP page processing logic, as shown in the for loop in this example.

Finally, the Code between <% = and %> is called a JSP expression, as shown in "<% = str %>" in this example. JSP expressions provide a simple method to embed JSP-generated values into HTML pages.

  1. Adding special characters causes exposure of JSP source code files
  2. Use JMeter to test JSP Application Performance
  3. Multi-thread synchronization in Servlet and JSP
  4. Analysis of the best solution example of JSP IIS
  5. Can JSP meet the Web boom?

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.