IDEA(2020版)实现JSP基本语法

查看全文:IDEA(2020版)实现JSP基本语法 – 每天进步一点点


在JSP文件中可以嵌套很多内容,例如JSP的脚本元素和注释等,这些内容的编写都需要遵循一定的语法规范。本节将对JSP的基本语法进行详细讲解。

一、创建JSP页面

右击 Web文件夹,选择New—>JSP/JSPX,名称为timeInfo

代码参考如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<html>
<head>
    <title>JSP页面---显示系统时间</title>
</head>
<body>
<%
    Date date = new Date();
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String today =df.format(date);

当前时间:<%=today%>
</body>
</html>

在浏览器里输入下面的地址,请注意tomcat的地址

http://localhost:8080/chapter06/timeInfo.jsp

运行结果如下:

二、JSP脚本元素

1.JSP Scriptlets

右击web,选择New—>JSP/JSPX,名称为example01,代码参考为:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>JSP Scriptlets</title>
</head>
<body>
<%
    int a = 1, b = 2; //定义两个变量a,b
    out.println(a+b);

</body>
</html>

运行效果如下:

2.声明标识

基本格式

<%! 定义变量或方法%>

右击web文件夹,选择New—>JSP/JSPX,名称为example02,代码参考为:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>JSP声明语句</title>
</head>
<%!
    public int print() { //定义print方法
        int a = 1, b = 2; //定义两个变量a,b
        return a+b;
    }

<body>
<br />
<%
    out.println(print());//调用print()方法,输出其返回值

</body>
</html>

在浏览器中输入

http://localhost:8080/chapter06/example02.jsp

运行结果如下:

3.JSP表达式

基本格式如下:

<%= expression %>

右击web文件夹,选择New—>JSP/JSPX,名称为example03,代码参考为:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>JSP Scriptlets</title>
</head>

<%!
int a = 1, b = 2; //定义两个变量a,b

<body>
<%=a+b %><br />
</body>

</html>

在浏览器中输入

http://localhost:8080/chapter06/example03.jsp

运行结果如下:

二、JSP注释

1.带有JSP表达式的注释

 


 

 

查看全文:IDEA(2020版)实现JSP基本语法 – 每天进步一点点

posted on 2025-12-04 17:09  longkui  阅读(0)  评论(0)    收藏  举报

导航