JSP Basic Grammar Learning

Source: Internet
Author: User
Tags define comments expression html page include
js| syntax installation, we began to enter the JSP grammar learning. If you are unfamiliar with Java programming, you will need to consult Sun's technical guidelines. However, web developers do not need to do much Java development. In addition to a few method calls, Java code in a JSP Web page should be minimized.











remember this, first look at the JSP instructions and scripting principles, and then we will explain JavaBeans and specific objects. There are 5 kinds of JSP directives and script elements. In JSP1.0, most of the JSP code begins with a separate label "<%" and ends with "%>". In the new JSP1.1 definition, there is a compatible XML-compliant version of JSP.





JSP directives and script elements





instruction <%@ Instruction%>





Statement <%! Statement%>





expression <%= expression%>





Code Snippet/script segment <% code snippet%>





annotation <%--annotation--%>





instruction




The
JSP instruction is the engine of the JSP. They do not directly produce any visual output, but simply indicate what the engine needs to do with the remaining JSP pages. Directives are marked by <%@?%>. The main two directives are page and include. Directive Taglib is not discussed in this article, but it is used when creating a custom label in JSP1.1.





can find the instruction page at the top of almost any JSP page. Although not necessary, you can define such things as where to look for Java class support.





<%@ page import= "java.util.Date"%>





indicates where the network user is directed when a Java run problem occurs:





<%@ page errorpage= "errorpage.jsp"%>





need to manage information at the user session level, which is likely to span multiple Web pages (more on the JavaBeans section):





<%@ page session= "true"%>




The
directive "include" allows you to divide your content into more manageable elements, such as elements that include a normal page header or footer. The included web page can be a fixed HTML page or more JSP content:





<%@ include file= "filename.jsp"%>





Statement




The
JSP declaration allows you to define variables at the page level to hold the information or to define the support methods that the remaining JSP pages might require. If you find yourself writing too much code, it's usually best to write to a separate Java class. The declaration is defined by the <%!?%>. You must end the variable declaration with a semicolon, while any content must be a valid Java statement: <%! inti=0; %>.





expression





through the expressions in the JSP, the result of the evaluated expression is converted to a string and included directly in the output page. The JSP string is marked by the <%=?%> label, and is not included except for the quoted part of the string.





<%= I%>





<%= "Hello"%>





Code Snippet/script fragment




The
JSP code snippet or script fragment is embedded in the "<%?%>" tag. This Java code runs when the Web server responds to the request. There may be pure HTML or XML code around the script fragment, where code snippets allow you to create conditional execution code or simply invoke another piece of code. For example, the following code combination uses expressions and script fragments to display the string "Hello" in the H1, H2, H3, and H4 tags. Script fragments are not limited to one line of source code:





<% for (Inti=1 i<=4; i++) {%>





<H<%=i%>>Hello</H<%=i%>>





<%}%>





annotation




The last key element of the
JSP is about embedding annotations. Although you can always add HTML comments to your files, users will see the comments as soon as they view the page source code. If you do not want the user to see the annotation, embed it in the <%--?--%> tag:





<%--Comment for server side only--%>











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.