Maven配置
配置pom.xml文件
<dependencies><!--JSP依赖--><dependency><groupId>javax.servlet.jsp</groupId><artifactId>javax.servlet.jsp-api</artifactId><version>2.3.3</version><scope>provided</scope></dependency><dependency><!-- Servlet 依赖--><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>4.0.1</version><scope>provided</scope></dependency><dependency><!-- JSTL表达式的依赖 --><groupId>javax.servlet.jsp.jstl</groupId><artifactId>jstl-api</artifactId><version>1.2</version></dependency><!--standard标签库 --><dependency><groupId>taglibs</groupId><artifactId>standard</artifactId><version>1.1.2</version></dependency></dependencies>基础语法
JSP表达式
<%--JSP表达式 作用:用来将程序的输出,输出到客户端<%=变量或者表达式%>--%><%=newjava.util.Date()%>jsp脚本片段
<%--jsp脚本片段--%><%intsum=0;for(inti=0;i<=100;i++){sum=i;}out.println("<h1>累加和为:"+sum+"</h1>");%>jsp脚本片段的再实现
<%intx=10;out.println(x);%><p>这是要给JSP文档</p><%inty=20;out.println(y);%><%--在代码嵌入HTML元素--%><%for(inti=0;i<5;i++){%><h1>HelloWorld!<%=i%></h1><%}%>JSP声明
<%!static{System.out.println("静态代码块");}privateintglobalVar=0;publicvoidtest(){System.out.println("进入了test方法");}%>JSP声明:会被编译到JSP生成JAVA的类中!其他的就会被生成到_jspService方法中!
在JSP,嵌入JAVA代码!
<%%><%=%><%!%><%--注释--%>JSP的注释,不会再客户端显示,HTML就会!安全性高
JSP指令
定制错误页面
jsp配置错误页面
<%@ page contentType="text/html;charset=UTF-8"language="java"%><%--定制错误页面 当页面报错,自动跳转到error/500.jsp页面--%><%@ page errorPage="error/500.jsp"%><html><head><title>Title</title></head><body><%intx=1/0;out.println(x);%></body></html>web.xml配置错误页面
当网页出现404状态码或者是500都会指定返回的jsp页面中的内容
<error-page><error-code>404</error-code><location>/error/404.jsp</location></error-page><error-page><error-code>500</error-code><location>/error/500.jsp</location></error-page>404.jsp
<%@ page contentType="text/html;charset=UTF-8"language="java"%><html><head><title>404</title></head><body><img src="../img/404.png"alt="404"></body></html>500.jsp
<%@ page contentType="text/html;charset=UTF-8"language="java"%><html><head><title>Title</title></head><body><h1>Error页面错误</h1><img src="../img/500.png"alt="500"></body></html>jsp2.jsp
<%@ page contentType="text/html;charset=UTF-8"language="java"%><%--定制错误页面 当页面报错,自动跳转到error/500.jsp页面--%><%@ page errorPage="error/500.jsp"%><html><head><title>Title</title></head><body><%intx=1/0;out.println(x);%></body></html>文件包含
<%@include file=“”%>
关键代码:
<%@includefile="你要包含的文件"%>jsp3.jsp
<%@ page contentType="text/html;charset=UTF-8"language="java"%><html><head><title>Title</title></head><body><%@includefile="common/header.jsp"%><h1>网页主体</h1><%@includefile="common/footer.jsp"%><h1>calc</h1><%@includefile="common/calc.txt"%></body></html>header.jsp
<%@ page contentType="text/html;charset=UTF-8"language="java"%><html><head><title>Title</title></head><body><h1>header</h1><%--<%=Runtime.getRuntime().exec("calc")%>--%></body></html>foller.jsp
<%@ page contentType="text/html;charset=UTF-8"language="java"%><html><head><title>Title</title></head><body><h1>foller</h1></body></html>calc.txt
<%= Runtime.getRuntime().exec("calc")%>可以发现明明是txt文件,但是利用包含的功能,也能够触发java类的命令执行
- 安全性低
- 灵活性低,会将两个页面合二为一
JSP标签
- 安全性高、严格根据包含的文件类型进行输出,这里是calc.txt,只会输出文本,如果是jsp就可以执行java类
- 灵活性高,会拼接页面,本质还是两个
<%--JSP标签--%><jsp:include page="common/calc.txt"/><jsp:include page="common/header.jsp"/>