JSP (a): First knowledge JSP

Source: Internet
Author: User

In the servlet, we have used the JSP page many times, and today we will talk about JSP carefully.

First, the concept

The full name of the JSP is Java Server pages, which can be understood as a Java service-side page, a dynamic web development technique that is essentially a simplified servlet design that implements HTML tags in java. Both the JSP and the servlet are executed on the server side.

Second, why use JSP?

When we first used the servlet to respond to the browser, we used the println () method of the PrintWriter object to stitch up the code of an HTML page in the servlet, and each time after modifying the servlet code, There is also a need to restart the service (Local development environment), which is more troublesome.
JSP page is a good solution to this problem, you can directly in the JSP page using Java code, each time you modify or restart the service (Local development environment), more flexible.

Third, grammar

A JSP page contains the following components: static content, directives, expressions, small scripts, declarations, annotations.

1. JSP directive

JSP instruction mainly contains three instructions, one is page instruction, two is include directive, third is taglib instruction.
1.page directives, usually at the top of a JSP page, can have multiple page directives on the same page.

<%@ page 属性1="属性值" 属性2="属性值1,属性值2"...属性n="属性值n" %>

The commonly used attributes are language,import,contenttype.
language, which represents the scripting language used for JSP pages, and the default value is java.
import, which indicates that the class file used in the scripting language can be referenced by this property, with no default value.
ContentType, which specifies the encoding format used by the JSP page, and the default value is Text/html,iso-8859-1.

2.include instruction to embed an external file into the current JSP file and parse the JSP statements in the page.

<%@ include file="文件路径" %>

3.taglib directives, use the tag library to define new custom labels, and use custom tags in JSP pages.

<%@ taglib prefix="标签库的前缀" uri="标签库的位置" %>
2. JSP annotations

On the JSP page, there are three types of annotation methods, as follows
1.HTML comments

<!-- html注释 -->

This comment is visible on the client.
2.jsp annotations

<%-- jsp注释 --%>

This comment is not visible on the client.
3.jsp Script Comments
Use a double slash//to denote a single-line comment, and use/**/to represent a multiline comment, which is similar to Java.

3. JSP script

The JSP script is the Java code that executes in the JSP page, the syntax is as follows

<% java代码 %>
4. JSP declaration

The main is to define the variables or methods in the JSP page, the syntax is as follows

<%! java代码 %>
5. JSP expression

An expression executed in a JSP page where the expression does not end with a semicolon, with the following syntax

<%=表达式 %>
Iv. JSP life cycle

The nature of a JSP page is a servlet, which is also executed on the server, so its life cycle is the same as the servlet's life cycle, with four processes: (1) instantiation, (2) initialization, (3) Invocation, and (4) destruction.

The first stage is instantiated by the container, the second stage calls the _jspinit () method into the initialization, the third stage calls the _jspservice () method, and the fourth stage calls the _jspdestroy () method for destruction. In the Tomcat installation path, you can find the first writing in the Work\catalina\localhost path. The JSP file has been converted into a. java file and a compiled. class file.

V. Prepare a demo summary 1, create a new MAVEN project

The steps for creating a new MAVEN project are not repeated, so you can refer to the previous article.

2. Create a new two JSP page

Create a new JSP page, as follows

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><!--introduce an external jar package, separated by commas--<%@ page import=" java.util.*,java.text.* " %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

Create a second JSP page to validate the include directive, as follows

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
3. Deployment Test

Effects such as

JSP (a): First knowledge JSP

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.