1.什么是JSP
Java Server Pages:Java服务端页面,也和Servlet一样,用于动态Web技术。
最大的特点:
- 写JSP就像在写HTML
- 区别:
1)HTML只给用户提供静态的数据;
2)JSP页面中可以嵌入Java代码,为用户提供动态数据;
2.JSP原理
思路:JSP到底怎么执行的
- 代码层面没有任何问题
- 服务器内部工作
tomcat中有一个work目录;
IDEA中使用tomcat的话会在IDEA中生成一个work目录
发现转变成了java程序!
浏览器向服务器发送请求,不管访问什么资源,其实都是在访问Servlet
JSP最终也会被转化为java类
JSP本质上就是Servlet
//初始化
public void jspInit() {
}
public void _jspInit() {
}
//销毁
public void jspDestroy() {
}
protected void _jspDestroy() {
}
//jspService
public abstract void _jspService(HttpServletRequest var1, HttpServletResponse var2) throws ServletException, IOException;
1.判断请求
2.内置的一些对象
final javax.servlet.jsp.PageContext pageContext; //页面上下文
javax.servlet.http.HttpSession session = null; //session
final javax.servlet.ServletContext application; //application
final javax.servlet.ServletConfig config; //config
javax.servlet.jsp.JspWriter out = null; //out
final java.lang.Object page = this; //page:当前页面
HttpServletRequest var1; //请求
HttpServletResponse var2; //响应
3.输出页面前增加的代码
response.setContentType("text/html"); //设置响应的页面类型
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
4.以上的这些对象我们可以在JSP文件中直接使用
在JSP文件中:
只要是JAVA代码就会原封不动地输出;
如果是HTML代码,就会被转换为
out.write("<html>\r\n");
这样的格式输出到前端。
3.JSP基础语法
JSP作为java技术的一种应用,java所有语法都支持,也拥有一些自己扩充的语法(了解即可)
JSP表达式
<%--JSP表达式
作用:用来将程序的输出给输出到客户端
<%= 变量或者表达式%>
--%>
<%=
new java.util.Date()
%>
JSP脚本片段
<%--JSP脚本片段--%>
<%
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
out.println("<h1>Sum="+sum+"</h1>");
%>
脚本片段的再实现
<%
int x = 10;
out.println(x);
%>
<p>this is a JSP file.</p>
<%
int y = 20;
out.println(y);
%>
<hr>
<%--在代码中嵌入HTML元素--%>
<%
for (int i = 0; i < 5; i++) {
%>
<h1>hello,hui! <%=i%></h1>
<%
}
%>
JSP声明
<%!
static {
System.out.println("loading Servlet!");
}
private int globalVar = 0;
public void jspInit(){
System.out.println("进入了方法hui");
}
%>
JSP声明:会被编译到JSP生成的Java的类中!其他的就会被生成到_jspService方法中!
在JSP中嵌入Java代码即可。
<%%>
<%=%>
<%!%>
注:JSP的注释不会被客户端显示,但是HTML的可以。
4.JSP指令
<%@ page arg... %>
使用:
<%--显示的声明这是一个错误页面--%>
<%@ page isErrorPage="true" %>
<%--设置为UTF-8解码--%>
<%@ page pageEncoding="UTF-8" %>
小技巧:我们可以在web.xml文件中自定义错误页面,比如404页面,500页面等!
具体写法如下:
<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>
<%@ include file=""%>
使用:
<%--@include 会将两个以页面合二为一--%>
<%@ include file="common/header.jsp"%>
<h1>我是网页主体</h1>
<%@ include file="common/footer.jsp"%>
但在实际开发中我们并不会使用这个方法,有弊端:比如两个jsp文件使用了同名的变量,便会造成冲突。
我们一般使用下面的这种jsp标签的写法:
它也会进行拼接页面,但本质上还是多个页面
<%--jsp标签
jsp:include: 拼接页面,本质还是三个
--%>
<jsp:include page="/common/header.jsp"/>
<h1>我是网页主体</h1>
<jsp:include page="/common/footer.jsp"/>
效果:
5.九大内置对象
- PageContext 存东西
- Request 存东西
- Response
- Session 存东西
- Application (ServletContext) 存东西
- config (ServletConfig)
- out
- page
- exception
pageContext.setAttribute("name1","hui1"); //保存的数据只在一个页面中有效
request.setAttribute("name2","hui2"); //保存的数据只在一次请求中有效,请求转发会携带这个数据
session.setAttribute("name3","hui3"); //保存的数据只在一次会话中有效,从浏览器打开到关闭
application.setAttribute("name4","hui4"); //保存的数据在服务器中有效,从打开服务器到关闭服务器
从底层到高层(作用域):page->request->session->application
<%-- 通过pageContext取出我们保存的值--%>
<%
//从pageContext取出,我们通过寻找的方式来
//从底层到高层(作用域)page->request->session->application
String name1 = (String) pageContext.findAttribute("name1");
String name2 = (String) pageContext.findAttribute("name2");
String name3 = (String) pageContext.findAttribute("name3");
String name4 = (String) pageContext.findAttribute("name4");
String name5 = (String) pageContext.findAttribute("name5");//不存在
%>
<%--使用EL表达式输出 ${}--%>
<h1>取出的值为:</h1>
<h3>${name1}</h3>
<h3>${name2}</h3>
<h3>${name3}</h3>
<h3>${name4}</h3>
<h3>${name5}</h3>
<h3><%=name5%></h3>
取出数据效果:
request:客户端向服务器发送请求,产生的数据用户看完就没用了,比如:新闻;
session:客户端向服务器发送请求,产生的数据用户用完,一会还要用,不如:购物车;
application:客户端向服务器发送请求,产生的数据,一个用户用完了,其他用户还要用,比如:聊天数据;
6.JSP标签,JSTL标签,EL表达式
<!-- standard标签库-->
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- JSTL表达式依赖-->
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl-api -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
JSP标签
请求转发,并且携带参数:
<jsp:forward page="jsptag2.jsp">
<jsp:param name="name" value="hui"/>
<jsp:param name="age" value="19"/>
</jsp:forward>
获取参数:
<%--取出参数--%>
name:<%=request.getParameter("name")%>
age:<%=request.getParameter("age")%>
JSTL表达式
JSTL标签库的使用就是为了弥补HTML标签的不足;他自定义了许多的标签,可以供我们使用,标签的功能和Java代码一样。
核心标签(掌握部分)
JSTL标签库使用步骤
- 引入对应的taglib
- 使用其中的方法
- 在Tomcat中也需要引入jstl的包,否则会报错:JSTL解析错误
导入头文件
<%--引入JSTL核心标签库,我们才可以使用JSTL标签--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
c:if
<body>
<h4>if测试</h4>
<hr>
<form action="coreIf.jsp" method="get">
<%--
EL表达式获取表单中的数据
${param.参数名}
--%>
<input type="text" name="username" value="${param.username}">
<input type="submit">
</form>
<%--判断如果提交的用户名是管理员,则登陆成功--%>
<c:if test="${param.username=='admin'}" var="isAdmin">
<c:out value="管理员欢迎您!"/>
</c:if>
<c:out value="${isAdmin}"/>
</body>
效果:
c:choose
<c:choose>
<c:when test="${score>=90}">
<c:out value="你的成绩优秀"/>
</c:when>
<c:when test="${score>=60&&score<90}">
<c:out value="你的成绩为及格"/>
</c:when>
<c:when test="${score<60}">
<c:out value="你的成绩不及格"/>
</c:when>
</c:choose>
效果:
c:choose
var:每一次遍历出来的变量 i
tems:要遍历的对象begin:起始
end:结束
step:步长
<body>
<%
ArrayList<String> people = new ArrayList<>();
people.add("1");
people.add("2");
people.add("3");
people.add("4");
people.add("5");
request.setAttribute("list",people);
%>
<%--
var:每一次遍历出来的变量
items:要遍历的对象
begin:起始
end:结束
step:步长
--%>
<c:forEach var="people" items="${list}">
<c:out value="${people}"/> <br>
</c:forEach>
<hr>
<c:forEach var="people" items="${list}" begin="1" end="3" step="2">
<c:out value="${people}"/> <br>
</c:forEach>
</body>
效果:
格式化标签
SQL标签
XML标签
EL表达式:${}
- 获取数据
- 执行运算
- 获取web开发的常用对象