Initial JSP

1. What is JSP

        1. JSP (Java Server Pages): Embed Java script code in HTML

              Static content is static text in a JSP page, which is basically HTML text and has nothing to do with Java and JSP syntax.

Small chestnuts:

<%@page import="java.util.*"%>
<%@page import="java.text.SimpleDateFormat"%>  
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Output current date</title>
</head>
<!-- This is an HTML comment (client can see the source code) -->
<%-- This is a JSP comment (client cannot see the source code) --%>
<body>
hello today is
	<%
	//Java logic code writing area
	SimpleDateFormat formater = new SimpleDateFormat("yyyy年 MM月dd日");
	String str = formater.format(new Date());
%>
	<!--Render expression-->
	<%=str%>
</body>
</html>
The result is as follows:


2. JSP instruction elements

           1. The role of the JSP instruction element is to control some features of the JSP page at runtime by setting the attributes in the instruction.

          2. JSP instructions generally start with "<%@" and end with "%>".

           The code fragment belonging to the JSP instruction in the above chestnut is:

           <%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>

            page: The instruction is a kind of instruction for setting the current page, usually located at the top of the JSP page, and a JSP page can contain multiple

                         page directive. It should be noted that the page directive is only valid for the current JSP page.

            page syntax:

                        <%@ page attribute 1 = "attribute value 1" attribute 2 = "attribute value 2" ...... %>

            The JSP container will use the default directive attribute value. If you need to set multiple attribute values ​​for an attribute in the page directive, separate the periods with commas.

                         <%@ page attribute 1 = "attribute value 1" attribute 2 = "attribute value 2", "attribute value 3" ...... %>

page common attributes:

Attributes describe Defaults
language Specifies the scripting language used by the JSP page java
import Use this attribute to refer to the class file used in the scripting language without
contentType Used to specify the type of JSP page running result, the encoding format that the browser needs to use to display the content text/html ; charset=Ios-8859-1 

Attribute details :

(1), language attribute: The language attribute in the page command is used to specify the script language used by the current JSP page, the current JSP version

            Only Java can be used as a scripting language. This property can not be set, because JSP uses Java as the script by default.

(2) Import attribute: The import attribute in the page directive is frequently used in actual development. Through the import attribute can be in the JSP file

            The class is referenced in the script fragment. If an import attribute introduces multiple classes, you need to separate the multiple classes with commas.

            The format is as follows:

            <%@ page import="java.util.* , java.text.* "%>

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

            <%@ page import="java.text.* "%>

(3), contentType attribute: The setting of this attribute is very important in the development process and is often used. Chinese garbled characters have always been a problem

            A problem for developers, and the contentType property can set the encoding format. This setting tells the web container to

            The content of the response is displayed on the browser in whatever format and encoding is used.

            The format is as follows:

            <%@ page contentType=" Text/html ; charset=UTF-8 "%>

2. JSP script elements

       In JSP pages, scriptlets, expressions, and declarations are collectively referred to as JSP script elements.

        1. What is a small script

            小脚本可以包含任意的Java片段,形式比较灵活,通过在JSP页面中编写小脚本可以执行复杂的操作和

            业务处理。编写方法是将Java程序片段插入<%%>标记中。

            例如:

<%
	//Java逻辑代码编写区
	SimpleDateFormat formater = new SimpleDateFormat("yyyy年 MM月dd日");
	String str = formater.format(new Date());
	out.print(str);
%>

这段代码中使用了JSP的一个内置对象out,out.print()方法用于页面中输出数据

2、什么是表达式

        表达式是对数据的表示,系统将作为一个值进行计算显示。当需要在页面中输出一个Java变量或

        着表达式值时。

        语法:

           <%=java变量或表达式%>

            小栗子:

<body>
	<%
		int[] value = { 60, 70, 80 };
		for (int i : value) {
			//out.print(i);
	%>
		<%=i%>
	<%
		}
	%>
</body>

需要注意的是:在Java中语法的规定中,每一条语句末尾必须使用分号代表结束。而在JSP中使用表达式输出数

据时,不能再表达式结尾处添加分号。

问答题:如何实现在JSP页面中计算两个数的和,并将结果输出显示?

小栗子:

<body>
	<%
		int A=10,B=20;
		int C=A+B;
	%>
	数字<%=A %>和<%=B %>的求和结果为:<%=C %>
</body>





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324549194&siteId=291194637
jsp