Hello jsp! --Basic Grammar Chapter

Source: Internet
Author: User
Tags html comment

I. INTRODUCTION of JSP

JSP is the abbreviation for Java Server pages, a technology introduced by Sun Company in June 1999, which forms a JSP page by inserting JSP scripting code into traditional HTML. JSP is a Web development technology based on Java servlet and the whole Java system, which can be used to build a secure and cross-platform Dynamic Web site. JSP is constantly being updated and optimized.

JSP has the following main features:

1. Program Logic and display separation

The development of interface and the development of program logic can be separated by using JSP technology. Web developers use HTML or XML tags to design the interface, use JSP tags or scripts to generate dynamic content on the page, the logic of dynamic content is encapsulated in markup and JavaBeans components, and bundled in scripts, so the script runs on the server side.

JSP Technology makes the division of the developer more explicit, the page designer can modify the content without affecting the logic, application development This can modify the logic without affecting the content.

2. Reusable components

The vast majority of JSP pages rely on reusable, cross-platform components (JavaBeans or Enterprise JavaBean) to perform complex processing of application requirements. Developers can share and swap components, or make them available to more users. The component-based development method effectively improves the development efficiency of the application and accelerates the overall development process of the project.

3. Use tags to simplify page development

JSP encapsulates a number of features that are needed to generate dynamic content for JSP-related HTML or XML, and standard JSP tags can access and instantiate JavaBeans components, set or retrieve properties of components, download applets, and perform features that are difficult to implement using other methods.

JSP tags are extensible, allowing developers to extend JSP tags, and developers to customize common function tag libraries. Third-party or other developers can also create their own tag libraries, because page creators can use the features in the tag library, thus greatly reducing the reliance on scripting languages and reducing the complexity of page authoring.

4. Features of Java

Because the JSP page's built-in script is based on the Java language, and all JSP pages are compiled into Servlets, all JSPs feature Java features such as robust storage management, security, cross-platform, "write once, run everywhere".


Second, the JSP operating mechanism

hellojsp.jsp

Operation Result:

After you open the JSP page using your browser, view the page source code as follows:

The implementation of JSP pages can be divided into two stages, one is the translation phase, and the other is the request phase.

translation Phase : The JSP page is converted into a servlet class.

Request Phase : The servlet class executes, sending the response result to the client.

The execution process is as follows:

(1) The User (client) accesses the corresponding JSP page.

(2) The server finds the appropriate JSP page.

(3) The server translates the JSP into the source code of the servlet.

(4) The server compiles the servlet source code into a class file.

(5) The server loads the class file into memory and executes it.

(6) The server sends the HTML file code generated by the class file to the client and the client browser is displayed according to the corresponding HTML code.

If the JSP page is executed for the first time, it passes through both phases, and if it is not the first time, then only the stage is requested. This is why the second time the JSP page executes is significantly faster than the first time.

If a JSP page is modified, the server discovers the modification and re-executes the translation and request phases. That's why access slows down after the page is modified.


Third, JSP scripting elements

JSP scripting elements contain JSP declaration statements, JSP Scriptlets, and JSP expressions.

1.JSP declaration Statements

JSP declaration statements to <%! Start with %> , whose syntax format is:<%! declaration Statement%>

A variable declared with a declaration statement is a global variable, that is, when more than one user executes the JSP page, the variable is shared.

Declarationsdemo . JSP

 

The example code line No. 06 passes the "<%! int a=1; %> "declares a global variable A, uses" Out.println ("a=" +a) "output a variable in line No. 08, and automatically increments by" a++ "on line No. 09, because variable A is a full -board variable, so if more than one user executes the JSP, The variable will be shared.

2.JSP Scriptlets

JSP scriptlets begins with <% , ends with %> , and its syntax format is %> for <% Java code. In JSP scriptlets CAS contains multiple statements, such as: methods, variables, expressions, and so on.

jspscriptletsdemo.jsp


Operation Result:


Code parsing:

The sample code declares a string, str, whose value is "Java Scriptlets Demo", which outputs the value of the string on the Web page through the Out.println () method.

3.JSP-expression

The JSP expression begins with <%= , ends with %> , and has a syntax format of <%= Java code%>. The code in the JSP expression is executed first, then converted to a string and displayed on the Web page.

declarationsdemo.jsp

Operation Result:


Code parsing:

The sample code declares a string str with the value: "Hello" and the value of the string is printed on the Web page by using an expression <%=str%>.


4. Notes

The comments of the JSP are divided into two categories, one for annotations that can be displayed on the client, that is, an HTML comment, and a comment that cannot be displayed by the client, that is, a JSP comment.

HTML Comments:

Start with <!-- , and end with the code in the middle that is the comment section.

commentsdemo1.jsp

<%@ page pageencoding= "UTF-8"%>
<%@ page pageencoding= "UTF-8"%>

5. Comprehensive Exercises:

practice.jsp

Operation Result:



Modify the above example to use a JSP expression to output the value of the variable C

Operation Result:


6. Summary:

To start learning JSP, the most important thing is to understand its operating mechanism, must understand when to translate, when to respond to requests, JSP scripting elements are the most basic part of the whole JSP system.

Personal Learning Experience:

Contact a new language, and the intention to learn, the harvest is really big, the only bad is that the base of the Web page is too poor, the HTML comments can be displayed on the client, I think it is able to display on the Web page, the results and otherwise, HTML comments can only be displayed in the source code of the Web page, and JSP comments can not I sent the question to the elder sister to learn to communicate with the group, although not ridiculed, but found the cause of the problem oneself still feel a hot face, the computer foundation of a freshman is really white learning. But the new knowledge get still very happy, shame is not terrible.

The content of this blog is basically I hit the book, through the Cheng Bowen of every knowledge point in the form of the first to deepen their memories.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hello jsp! --Basic Grammar Chapter

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.