JSP basic syntax

Source: Internet
Author: User

In the tomcat environment to build a article for everyone in detail introduced the first JSP program--hello World, we should all smooth completion of it, as a beginning, I hope you learn Java EE on the road, continuous progress. Today, we introduce the basic syntax of JSP. To learn a language quickly, mastering its grammar is critical. This is fundamental.

On the one hand because of the length of reasons, on the other hand I do this article is to do a small summary, to help you clear the idea, the basic grammar of the JSP. Therefore, there are no examples of small programs on this blog post. In fact, the nature of JSP is Java, as long as you have a certain Java Foundation, JSP script segment syntax is identical to Java. After memorizing this article, I believe that you write some JSP small program will be very easy. Hope you, after reading this article, write more small programs. After all, the ability to program is not a memory of some theoretical knowledge can be, but a lot of hands-on practice.

Anyway, grammar is fundamental. In general, the basic JSP syntax includes two annotation types, three script elements, three instruction elements, and eight action instructions. The composition of the JSP page

JSP pages contain both JSP elements and template data. Template data refers to parts of the JSP engine that are not processed, that is, parts other than <%%>, such as HTML tags in the code, which are transmitted directly to the client's browser.

A JSP element is a part of a value that is processed directly by the JSP engine, and this part must conform to the JSP syntax. JSP tags are case-sensitive (HTML tags are case-insensitive), and the script syntax and instruction syntax use the traditional syntax. Two types of annotations

1.html comments

eg

2.jsp comments

<%--This is a JSP single-line comment--%> <%/* This is a JSP multiline comment */%>
Three script elements

1. Disclaimer: Member variables for the entire page

2. Expressions

<%! Variables and methods within a%> are variables and methods within a class, that is, member variables and member methods. A variable declared within <%%> is a variable of a method, which is a local variable.

3. Script Segments

Three instruction elements

1.page instruction, only import can be multiple

eg

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <%@ page import= "com.kl.bean.*"%>

The ContentType property specifies the MIME type and character encoding of the page response

The Pageencoding property is used to specify the JSP page character encoding format.

My advice: all set to UTF-8

2.include Instructions: Process the required files during the compilation phase

3.taglib, custom label for the Declaration page

Eight actions

1. Internal jump (Request forwarding): <jsp:forward >

From a JSP file, pass a Request object containing the user's requests to another JSP file. The JSP script after the tag is not executed.

The statement implements the page jump, and can carry the relevant parameters, the browser address bar data does not change, does not display parameter information.

Redirect: The redirect behavior is two requests and produces two request objects, which causes the first request object information to be lost.

Request Forwarding: Forwarding made a request, the address bar of the browser is always the first requested address. Forwarding is the handover of Request/response control within the server.

eg: with param child markers

<jsp:forward page= "loginresult.jsp" >     <jsp:param name= "username" value= "<%=user%>"/>     <jsp:param name= "Password" value= "<%=pwd%>"/> </jsp:forward>

Does not contain PARAM child tags

2. Include Page:<jsp:include>

Unlike <%@ Include%>, dynamically joins the JSP page as it runs.

<jsp:include page= "scripts/login.jsp" >     
->javabean

3. Create bean:<jsp:usebean>

Property:

Id:javabean is a class in which different instances are distinguished by ID

Scope: Default page

Page: Create the JSP for the bean and all the static include files in this file (<%@ include%>).

Request: The same demand until the next request is initiated by the client.

Session: The entire conversation is approximately understood as closing the browser.

Application: The whole application. The server starts execution of the service until the server shuts down.

The class name of the Class:javabean.

eg

<jsp:usebean id= "Checking" scope= "session" class= "Bank.checking" >    

4. Set Bean Properties:<jsp:setproperty>

Property:

Name: id attribute in the <jsp:useBean> tag

The properties of the Property:bean instance.

The value of the Value:bean instance property.

Param: Form Parameters

Grammar:

1) Set the value of the Bean property to a string.

2) Set the value of the corresponding property of the bean by the value of the parameter of the form, and the property in the form and the JavaBean property should have the same name.

Note that the Chinese parameter of the form is garbled

Post mode

Get mode

5. Get Attributes <jsp:getProperty>

6. Defining the parameter:<jsp:param>

The PARAM tag provides additional information for other tokens in the form of a "first name-value" pair and cannot be monolithic used. As a child tag of the <jsp:include>,<jsp:forward >,<jsp:plugin> tag.

7. Using the Applet plugin:<jsp:plugin>

8. Plugin Error Prompt:<jsp:fallback>

JSP basic syntax

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.