Java Study Notes-JSP 1 (32), java Study Notes

Source: Internet
Author: User
Tags define local

Java Study Notes-JSP 1 (32), java Study Notes

Servlet as a dynamic web page development technology is too unreliable. In particular, when writing a page, you need to specify the template tag of the page and process a lot of business logic. Therefore, it is complicated.

Think: Why is it so hard for Servlet to write a page?

The main reason is that Servlet actually writes labels in java.

For the above reasons, SUN provides the dynamic web page development technology JSP.

JSP is the Java Server Page. It can be seen that JSP is a page, so it is very easy to compile HTML tags on the page.

I. JSP experience

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

Summary:

JSP running principle

JSP is essentially a Servlet, but this Servlet is better at writing pages.

<% = Expression %> the expression cannot be followed by a semicolon.

Example 1: output an expression.

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

Running result

2>1 = true org.apache.jsp.index_jsp@1135cd9

Summary: The output expressions on all JSP pages are translated into the _ jspService () in the java class and encapsulated using the out. write () method.

This output expression can also output variables, but a variable needs to be defined first.

3. JSP script

JSP scripts are mainly used to define local variables and write JAVA business code.

Syntax: <% JAVA code %>
Example 1: use JSP scripts to define variables. <% Int sum = 0; %> sum = <% = sum %> <br/>

Summary: JSP scripts are translated into _ jspService (). All variables defined using this syntax are local variables. Therefore, no variable modifiers can be added.

Example 2: use JSP scripts to implement inverted triangle output of pages.

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

Example 3: use JSP scripts to output H1 ~ H6 titles.

<%      for(int i = 1; i < 7; i++){%>      

Summary:

Can JSP scripts be used to define methods and classes? The write method cannot be continued, but local internal classes can be defined.

Example 4: define a local internal class.

<%     class Inner{       public int age = 30;       public int getSum(){          return 90;       }    }%>    <%= new Inner().age %><br/>    <%= new Inner().getSum() %><br/>

4. JSP Declaration

The JSP declaration mainly declares the member variables and methods of the class.

Syntax:

<%! Declare member %>

Example 1: declare a member attribute.
· <% Int x = 20; // _ jspService () %> <%! Private int x = 10; // index_jsp.java %> <% = x %> // 20
If you must access 10, you can use the following statement
<% = This. x %>
Example 2: Define a member method.
<%! Public String sayHello (String name) {return "hello:" + name ;}%> <% = this. sayHello ("jnb") %> <br/>
Example 3: rewrite the JSP lifecycle method.
<%! Static {// execute System. out. println ("loading Servlet! ");} Private int globalVar = 0; public void jspInit () {// initialization method System. out. println (" initializing jsp! ") ;}%> <%! Public void jspDestroy () {// destruction method System. out. println ("destroying jsp! ") ;}%>

 

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.