查看全文:https://www.longkui.site/program/java/idea2020jsp1/7223/
在JSP文件中可以嵌套很多内容,例如JSP的脚本元素和注释等,这些内容的编写都需要遵循一定的语法规范。本节将对JSP的基本语法进行详细讲解。
一、创建JSP页面
右击 Web文件夹,选择New—>JSP/JSPX,名称为timeInfo
代码参考如下:
<%@ pagecontentType="text/html;charset=UTF-8"language="java"%><%@ pageimport="java.util.Date"%><%@ pageimport="java.text.SimpleDateFormat"%><html><head><title>JSP页面---显示系统时间</title></head><body><% Datedate=new Date();SimpleDateFormatdf=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,代码参考为:
<%@ pagecontentType="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,代码参考为:
<%@ pagecontentType="text/html;charset=UTF-8"language="java"%><html><head><title>JSP声明语句</title></head><%!public intprint(){//定义print方法 int a=1, b=2;//定义两个变量a,breturna+b;}<body><br /><% out.println(print());//调用print()方法,输出其返回值</body></html>在浏览器中输入
http://localhost:8080/chapter06/example02.jsp
运行结果如下:
3.JSP表达式
基本格式如下:
<%=expression %>右击web文件夹,选择New—>JSP/JSPX,名称为example03,代码参考为:
<%@ pagecontentType="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表达式的注释
后续可查看全文:https://www.longkui.site/program/java/idea2020jsp1/7223/