Learning jsp from scratch (2) -- Basic JSP syntax

Source: Internet
Author: User
Tags html comment
 

Part 1: items created by default in JSP files.

Take myeclipse8.0 as an example to create a new JSP file. The following code is created by default.

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

Line 3 illustrates that the language used on this page is Java, the Java package introduced, and the encoding method. The preceding content starts with "<% @ page" and ends with "%>. You can also put it in multiple rows. If you put it in multiple rows, each row requires a start and end sign.

Java is the default package imported during JSP compilation. lang. *, javax. servlet. *, javax. servlet. JSP. *, javax. servlet. HTTP. * The preceding package does not need to be imported.

Encoding method. If you want to use an intermediate file, you can select gb2312 or GBK as the encoding method.

<HTML>

If you want to change the content displayed on the page, modify it in <body> </body>.

The basic syntax for inserting Java code in JSP.

1. Declare the variable <%> or <%! %>

<%! %> The declared global variable can be used in the entire JSP file. <%> the declared variable can only be used after the declared position.

Example:

<%!int a=3; %>

2. Insert General Java code <%>

Example:

    <%int a=3;%>    <%a++; %>

3. Insert expression <% = %>

Example:

    <%int a=3;%>    <%a++; %>    <%=a %>

Add the preceding content in the middle of <body> </body>, run tomcat, and access the page in the browser. The page is displayed.


4. Insert comments <% -- %> or <%/**/%>

The client does not see the content in the comment when viewing the source code, which is highly secure.

If an HTML comment is inserted, the format is <! -->

You can see the comments when viewing the source code.

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.