JSP basic syntax

Source: Internet
Author: User
Tags html comment

(1) JSP comments

JSP annotation is used to mark the development demonstration during the program development process.

JSP comment: <% -- Comment -- %> it is not output to the client

HTML comment: <! -- Comment --> it will output to the client

(2) JSP Declaration

JSP is essentially servlet, and servlet is a special Java class, so JSP declaration conforms to Java syntax.

Syntax format: <%! Declaration %>

JSP declarations are used to declare variables and methods. They can be placed anywhere in JSP. The JSP declaration is converted to the member variables or member methods in the servlet.

<Body>
<%!
Public int count;
Public String Info (){
Return "hello ";
}
%>
<% Out. Print (count ++ "" + Info (); %>
</Body>

The corresponding servlet code is

package org.apache.jsp;import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;import java.util.*;public final class test1_jsp extends org.apache.jasper.runtime.HttpJspBase    implements org.apache.jasper.runtime.JspSourceDependent {    public int count;   public String info(){   return "hello";   }       private static final javax.servlet.jsp.JspFactory _jspxFactory =          javax.servlet.jsp.JspFactory.getDefaultFactory();  private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;  private javax.el.ExpressionFactory _el_expressionfactory;  private org.apache.tomcat.InstanceManager _jsp_instancemanager;  public java.util.Map<java.lang.String,java.lang.Long> getDependants() {    return _jspx_dependants;  }  public void _jspInit() {    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();    _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());  }  public void _jspDestroy() {  }  public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)        throws java.io.IOException, javax.servlet.ServletException {    final javax.servlet.jsp.PageContext pageContext;    javax.servlet.http.HttpSession session = null;    final javax.servlet.ServletContext application;    final javax.servlet.ServletConfig config;    javax.servlet.jsp.JspWriter out = null;    final java.lang.Object page = this;    javax.servlet.jsp.JspWriter _jspx_out = null;    javax.servlet.jsp.PageContext _jspx_page_context = null;    try {      response.setContentType("text/html;charset=UTF-8");      pageContext = _jspxFactory.getPageContext(this, request, response,      null, true, 8192, true);      _jspx_page_context = pageContext;      application = pageContext.getServletContext();      config = pageContext.getServletConfig();      session = pageContext.getSession();      out = pageContext.getOut();      _jspx_out = out;      out.write('\r');      out.write('\n');String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";      out.write("\r\n");      out.write("\r\n");      out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");      out.write("

Summary:

1. the variables and methods defined in the JSP declaration syntax correspond to the member variables and methods of the servlet class. Therefore, the variables and methods defined in the JSP declaration can be modified using access controllers such as private and public, you can also use static modifier to change it to class attributes and class methods, but you cannot use abstract modifier to declare partial methods, because abstract methods will cause JSP to correspond to servlet programming abstract classes, as a result, the instance cannot be instantiated.

2. open multiple browsers, or even open a browser on different machines to refresh the page. This will show that all clients access the Count continuously, and all clients share the same count variable. This is because the JSP page will be compiled into a servlet class, each servlet has only one instance in the container: the variables declared in JSP are member variables, and the member variables are initialized only when the instance is created, the value of this variable is saved until the instance is destroyed.

(3) Output JSP expressions

Syntax: <% = expression % >== <% Out. println (); %> the output expression is equivalent to an HTML page element.

(4) JSP script <% Java code %>

Generally, all executable Java code can be embedded into HTML pages through JSP scripts.

1. the JSP page can contain static content, commands, expressions, small scripts, declarations, standard actions, and comments.

2. expression, script (small script), which is called Java Script element in Declaration ------- used to embed Java code

3. Small Script: embed a piece of Java code into the JSP page

Note: The code embedded in the script is Partial Variables and is located in the service method. In fact, not only can the JSP sub-branch be converted into executable code in the service method, but all static classes on the JSP page will be output by the service method output statement, this is why JSP scripts can control static content on JSP pages. JSP scripts are converted to executable code in the service method, while Java syntax does not allow defining methods in methods. Therefore, methods cannot be defined in JSP scripts.

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.