JSP basic syntax and JSP basic syntax

Source: Internet
Author: User

JSP basic syntax and JSP basic syntax
Basic JSP Syntax 1. JAVA code 2 on the JSP page, instructions 3 on the JSP page, and implicit objects on the JSP page (nine built-in objects)Directory 1. JAVA code on the JSP page

  • JSP expressions (convenient output)
  • JSP script (complete relatively long logic operations)
  • JSP Declaration (add attributes or methods)
1. JSP expression (Format: <% = %>)
1     <%=2+2%>2     <%=xx.getName()%>3     <%=xx.getName()+“str”%>

Note: you do not need to write a semicolon to end the expression.

When Java code in this form is translated into Servlet, the code in <% = %> will become the output of the out. print statement used in the service () method.

1     out.print(2+2);2     out.print(xx.getName());3     out.print(xx.getName()+“str”));
2. JSP script (Format: <%>)
 1     <table> 2     <%  3        List<Student> students = (List<Student>)request.getAttribute(“students“); 4        for(Student st : students){ 5     %> 6               <tr> 7                   <td> <%=st.getId()%>    </td> 8                   <td> <%=st.getName()%>  </td> 9               </tr>10     <%11         }12     %>13     </table>

The code above uses a small script to dynamically generate a table. The code in <%> will eventually become part of the service method. The conversion result is as follows:

 1     public void service(…){ 2       out.write(“<table>”); 3       List<Student>  students = (List<Student>)request.getAttribute(“students“); 4       for(Student st : students){ 5          out.write(“<tr> <td>”); 6          out.print(st.getId()); 7          out.write(“</td><td>”); 8          out.print(st.getName()); 9          out.write(“</td></tr>”);10       }11       out.write(“</table>”);12     }
3. JSP Declaration (Format: <%! %>)
1 <%! 2 private String name; // attribute Declaration 3 4 public void fun () {// method Declaration 5 //... Method body 6} 7%>

JSP declaration <%! The code in %> can add attributes and methods for the Servlet. the result after converting to Servlet is as follows:

1 public class XXX_JSP extends JSPBase {2 private String name; // attribute Declaration 3 4 public void fun () {// method Declaration 5 //... Method body 6} 7 8 public void service (... ...) {9 10} 11}
Ii. Instructions on the JSP page
  • Page command
  • Include command
  • Taglib command
1. page command

The page command can be used to import java packages or to set some page attributes.

1 <% -- import package -- %> 2 <% @ page import = "java. util. * "%> 3 <% @ page import =" java. util. *, java. SQL. * "%> 4 5 <% -- set response. the parameter value of the setConentType Method -- %> 6 <% @ page contentType = "text/html; charset = UTF-8 '%> 7 8 <% -- set the decoding method when the container reads the file -- %> 9 <% @ page pageEncoding = "UTF-8" %>

 

(1) when using the page command to export packages, the import attribute is required. If you want to import multiple packages, you can divide them into multiple page commands, or use commas (,) to separate them.

(2) set the encoding method of the output content in the contentType attribute, so that the browser can use the correct decoding method to display the page.

(3) The pageEncoding attribute sets the decoding method when the container reads the file. This Code ensures that the Chinese characters are correctly decoded when the page is loaded to the memory.

Note: The page command should be written at the top of the page.

2. include command

The include command is mainly used to include other pages in another page to reuse the page content. The include command syntax is as follows:

1 <%@ include file=“thetitle.html” %>
3. taglib commands

The JSTL label library can be introduced to copy the jar package corresponding to the label Library to the WEB-INF/lib directory, so that the system can load the required class. Use the taglib command to introduce the tag namespace and prefix on the page to help the system locate the corresponding class.

1     <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

 

3. Implicit objects in JSP pages (nine built-in objects)

As the JSP page is eventually converted into a Servlet, and as a previous version of the Servlet to be converted, many systems will provide objects that can be directly used by the page, you can directly use the functions on the page. The following table lists the specific information about Implicit objects:

 

Implicit object Type Description
Request HttpServletRequest Request object
Response HttpServletResponse Response object
Out JSPWriter Output stream
Session HttpSession Session
Application ServletContext Global Servlet context object
PageContext PageContext JSP page context
Page Object Indicates the JSP page itself, that is, this
Config ServletConfig Servlet configuration object
Exception Throwable Captured page exception object

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.