JSP Beginner's Tutorial: Learning JSP Basic Instructions

Source: Internet
Author: User
Tags expression include variables script tag variable client
js| Beginners | tutorials

1, script tag

The language to which the declaration will be used:

<%@ page language= "Java"%> indicates the JSP directive, indicating that the following scripting code will be in the Java language syntax

To reference a class in a package:

<%@ page import= "java.util.*"%> represents the class to use in the Java.util package

Declaring variables:

<%! int count=0;%> declares an integer variable and assigns an initial value of 0. declaration to conform to the Java syntax specification and to ";" End.

To declare a method:

<%! int show (int val) {{return val;} %> or <%! Area S=new Area (5.0); %> must be declared before using variables and methods.

An output expression:

<%=2*X%> the value of the direct output expression, X must be a previously declared variable (not a semicolon), equivalent to the <%=%> in ASP

HTML annotation Character:

<!--note [<%= expression%>]--> Note that you can use an expression in a comment.

Hide an annotation character:

<%--here are comments,only Can see in Server.--%> is a JSP annotation that will be ignored on the server side and not translated into HTML annotations that are not visible when the client views the source.

Note: In the <%----%>, you can write any annotation statements, but you cannot use "--%>", if you must use, please use "--%\>" at the end.

Contains another JSP file:

<%jsp:include page= "another.jsp"%> or <%@ include page= "another.jsp"%> represents the insertion of another file another.jsp on the current page

Small script (Scriptlet):

<% Java program code%> can contain multiple JSP statements, methods, variables, or expressions in Scriptlet.

Normal JSP statement:

<% for (int i=0,i<10,i++)//jsp body part
Out.println (i+ "<br>");
%>

2. Instructions

JSP instructions are page, include two kinds of

Page directive (properties: 6)

Language properties:

<@ page language= "Java"%>//indicate the language used

Import Property:

<@ page import= "java.util.*"%>//Load Package

Note: In Java, to load multiple packages, use import to separate them and separate them with semicolons, and in a JSP, such as using an import to specify multiple packages, separated by commas.

such as: <%@ page import= "java.util.*,java.lang.*"%>

You can also <%@ page import= "java.util.*"%>

<%@ page import= "java.lang.*"%> (but does not advocate this use, nonstandard. )

Session Properties:

<@ page session= "true or false"%> the default session value is True
such as: <%@ page session= "true"%>
<% if (session.getvalue ("name") ==null)
Session.putvalue ("name", "123456");
else Session.putvalue ("name", Session.getvalue ("name") + "1");
%>
<% out.println (Session.getvalue ("name"));%>

If session= "false", a compilation error occurs.

ErrorPage Properties:

Refers to the program specified by ErrorPage when an error occurs on the current page program

Writing:

<@ page errorpage= "errorpage.jsp"%>
Such as:
test.jsp: <% @page errorpage= "errorpage.jsp" @>
<%!int i=0;%>
<%=7/i%>
errorpage.jsp: <% @page iserrorpage= "true" @>
<%=exception%>
When you run test.jsp, you will see an error message that is removed by 0.

Iserrorpage Properties:

Indicates whether the current program is an exception handler for another program. Regardless of whether it is set, the exception is directed to the current program, and the question is whether the current program can get an object for this exception. If set to true, an object that will produce an exception is exception and can be used in code, and if False, using the exception program will make a compile-time error.

If you change the previous example to False, the following error occurs:

error:500
Unable to compile class for JSP
Writing:
<% @page iserrorpage= "true" @>

ContentType Properties:

Specifies the MIME type and the character encoding of the JSP file, all of which are routed to the client first.

MIME types are: Text/plain, text/html (default type), Text/html, Image/gif, Image/jpeg, Image/jpeg

Default character encoding method: Iso8859-1

Include directives

Function: is used to insert the contents of a static file into the current page, which may be an HTML file, JSP file, or other text file, in the following format:

<%@ include file= "Include.inc"%>
Such as:
NATIVE.JSP:
<body>
Native file Start here.<br>
<%@ include file= "Include.inc"%>
Native file End here.<br>
</body>
Include.inc:
Include file start here.<br>
<%! String str= "Here is include ' s context!"; %>
<% Out.pringln (str+ "<br>");%>
Include file End here.<br>
Run native.jsp, the result is as follows:
Native file start here.
Include file start here.
This is include ' s context!
Include file end here.
Native file end is here.

Note: Because the Include.inc file is inserted at compile time, only change include.inc file content, but do not modify the original JSP page, the result will still be the previous result. (Because the JSP engine was first judged that the JSP page has not been altered, it executes the existing bytecode directly, without recompiling the source code, so that changes to include are not reflected.) )



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.