Java Learning Note-jsp 1 (32)

Source: Internet
Author: User
Tags define local

Servlet as a dynamic web development technology is too unreliable. Especially when you need to write a page, you need to specify the template label of the page, but also to handle a lot of business logic. So it is more cumbersome.

Thinking: Why does a servlet have to write a page so laboriously?

The main reason is that the servlet is actually writing tags in java.

For these reasons, Sun provides a JSP for dynamic web development techniques.

The JSP is the Java Server Page. The JSP itself is a page, so it should be very easy to write HTML tags on the page.

A JSP Experience

<body>   <%=new java.util.Date ()%></body>

Summarize:

    1. The servlet is annoying to write HTML in Java, and the servlet is to get parameters and request forwarding.
    2. JSP is the HTML to write Java comparison annoying, JSP is mainly to display data.
JSP operating principle

The JSP nature is a servlet, but this servlet is better at writing pages.

JSP syntax

Learning JSP should be learned from the following aspects:

1 JSP template elements

JSP is to write Java code in HTML, then the most basic of JSP is the HTML element. We call these elements the template elements of the JSP.

2 JSP output expression

JSP expressions are primarily used for output variables and output expressions.

Grammar:

<%= expression%> expression cannot have a semicolon after it. 

Example 1: Output an expression.

2>12>1 %>  <br/><%=page%>  <br/>

Run results

2>1true  [email protected]

Summary: The output expressions in all JSP pages are translated into _jspservice () in the Java class and encapsulated using the Out.write () method.

The output expression can also output variables, but the current need to define a variable first.

3 JSP Script

JSP scripts are primarily used to define local variables and write Java business code.

syntax:<%  java code%>
Example 1: Use a JSP script to define a variable. <% int0; %>= <%= sum%><br/>

Summary: The JSP script is translated in _jspservice (), and the variables defined with the syntax are all local variables. Therefore, you cannot add any variable modifiers.

Example 2: Use a JSP script to implement the inverted triangle output of the page.

<% for     (int.0; i<6; i++) {       for (int j = i; j<6; J + +) {%>       *    <%    }%>    <br/>    <%       }     %>

Example 3: Use a JSP script to output H1~H6 headings at all levels.

<% for      (int17; i++) {%>            }    %>

Summarize:

Can I define methods and classes using JSP scripts? Method cannot continue to write methods, but local inner classes can be defined.

Example 4: Define a local inner class.

<%     class  inner{       publicint;         Public int getsum () {          return;       }    } %>    New Inner (). Age%><br/>    new Inner (). Getsum ()%><br/>

4 JSP Declaration

A JSP declaration is primarily a member variable and a method that declares a class.

Grammar:

<%! Declaring a member%>

  Example 1: Declare a member property. 
... <% int x = 20 ; // _jspservice () %> <%! private int x = 10 ; // Index_jsp.java %> <%= x%> // If you have to access access to 10, you can use the following statement
<%= this . x%>
Example 2: Define a member method.
<%! Public string SayHello (string name) { return""+name; } %> this. SayHello ("jnb")%><br/>
Example 3: Implement the rewrite of the JSP life cycle method.
<%!Static{//class is loaded at the time of executionSystem. out. println ("Loading servlet!"); } Private intGlobalvar =0; Public voidJspinit () {//Initialize MethodSystem. out. println ("Initializing jsp!"); } %> <%! Public voidJspdestroy () {//Method of DestructionSystem. out. println ("destroying jsp!"); } %>

Java Learning Note-jsp 1 (32)

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.