JSP
JSP
1.什么是JSP?
Java Server Pages:Java服务端页面,也和Servlet一样用于动态web开发
最大的特点:写JSP就像写html,区别:html只给用户提供静态的数据、JSP页面中可以嵌入Java代码,为用户提供动态信息
2.原理
思路:JSP怎么执行(服务器获取页面时对应的jsp产生新的.java文件)
在服务器内部的工作:浏览器向服务器发送请求,不管访问什么资源,其实都是在访问Servlet,而JSP则是多了一个转换的步骤,转换完成后返回给用户
1.JavaServletPage中的常用方法
//初始化
void init(ServletConfig var1) throws ServletException;
//服务
void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;
//销毁
void destroy();
2.内置对象
final javax.servlet.jsp.PageContext pageContext;
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.PageContext _jspx_page_context = null;
3.输出页面前增加的代码
response.setContentType("text/html; charset=UTF-8");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, false, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
out = pageContext.getOut();
_jspx_out = out
4.注意事项
- 以上的对象我们可以在JSP页面中直接使用
- 在jsp页面中,只要是java代码就会原封不动的输出到.java文件中;html代码会被转换成out.write("\r\n")的格式输出到.java文件中
3.JSP基础语法
jsp作为java技术的一种应用,他拥有一些扩充的语法,java中的所有语法都支持
1.jsp使用时pom.xml中的依赖dependencies
<!-- servlet的依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!--jsp的依赖-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</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>
<!--standard标签库-->
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
2.基础语法
jsp表达式
<%--用来将程序的输出输出到客户端
<%= 变量表达式%>
--%>
<%= new java.util.Date()%>
jsp脚本片段
<%
int sum = 0;
for (int i = 0; i < 100; i++) {
sum += i;
}
out.write("<h1> Sum = "+sum+" \n<h1>");
%>
<%--在代码中嵌套html元素--%>
<%
for (int i = 0; i < 5; i++) {
%>
<h1>hello world <%=i%></h1> <!--等价于${i}-->
<% }%>
jsp声明
<%!
static {
System.out.println("loading servlet...");
}
private int globalVar = 0;
public void test(){
System.out.println("进入了方法test()");
}
%>
jsp声明会被编译到jsp生成java的类中,其他的会生成到_jspService方法中,在jsp中嵌入java代码即可
4.JSP指令
<%@ page args ...%>
<jsp: include: page = "">
5.九大内置对象
- PageContext(作用域:保存的东西只在一个页面中有效)
- Request(作用域:保存的东西只在一个请求中有效,请求转发也会携带这个数据)
- Response
- Session(作用域:在一次会话中有效->从打开浏览器到关闭浏览器)
- Application(作用域: 属于ServletContext类型,保存的东西只在服务器中有效)
- config(ServletConfig)
- out
- page
- exception
pageContext.setAttribute("name1", "zh");
request.setAttribute("name2", "yh");
session.setAttribute("name3","xh");
application.setAttribute("name4", "wh");
<%--
使用findAttribute()时的查询顺序(双亲委派机制)
pageContext->request->session->application
--%>
pageContext.setAttribute()的源码可以说明这一层关系
//PageContext中的作用域定义
public static final int PAGE_SCOPE = 1;
public static final int REQUEST_SCOPE = 2;
public static final int SESSION_SCOPE = 3;
public static final int APPLICATION_SCOPE = 4;
public void setAttribute(String name, Object attribute) {
this.mPage.put(name, attribute);
}
//可以通过该方法手动设置作用域
public void setAttribute(String name, Object attribute, int scope) {
switch(scope) {
case 1:
this.mPage.put(name, attribute);
break;
case 2:
this.mRequest.put(name, attribute);
break;
case 3:
this.mSession.put(name, attribute);
break;
case 4:
this.mApp.put(name, attribute);
break;
default:
throw new IllegalArgumentException("Bad scope " + scope);
}
}
使用场景总结
- request:客户端向服务器发送请求,产生的数据用户看完就没用了(但是可以通过转发延长生命周期),比如:新闻等,用户看完不再使用
- session:客户端向服务器发送请求,产生的数据,用户看完一会儿还有用,比如:购物车等,用完后暂时未存入数据库,一会儿还要使用
- application:客户端向服务器发送请求,产生的数据,一个用户看完了,其他的用户还可能使用,比如:群聊、评论等
6.JSP标签、JSTL标签、EL表达式
EL表达式:${}
- 获取数据
- 执行运算
- 获取web开发的常用对象
- 需要导入的dependency
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<!--standard标签库-->
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
JSP标签(常用)
<jsp:forward page = "/hello.jsp">
<jsp:param name = "name" value = "ygw"></jsp:param>
<jsp:param name = "age" value = "0"></jsp:param>
</jsp:forward>
<%--在转发到页面中的取值方式--%>
<%=request.getParameter("name")%>
<%=request.getParameter("age")%>
JSTL表达式(重点)
JSTL标签库的使用就是为了弥补html标签的不足;JSTL自定义了许多标签,标签的功能和Java一样
-
格式化标签
-
SQL 标签
-
XML 标签
-
JSTL 函数
-
核心标签(掌握部分)
使用步骤(以核心标签为例):
1.引入对应的taglib
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
2.使用其中的标签
tomcat中也需要引入jstl的包否则会报错:jstl解析错误
- c:if标签
<body>
<form action="coreif.jsp" method="get">
<input type="text" name="username" value="${param.username}">
<input type="submit" value="登录">
</form>
<%--判断提交的如果是管理员则登陆成功--%>
<%--java实现--%>
<%
if(request.getParameter("username").equals("admin")){
System.out.println("登录成功!!!");
}
%>
<%--jstl实现--%>
<c:if test="${param.username == 'admin'}" var="isAdmin">
<c:out value="Login Successfully!!!"></c:out>
</c:if>
<c:out value="${isAdmin}"></c:out>
</body>
- c:set、c:choose、c:when
<body>
<c:set var="scope" value="85"></c:set>
<c:choose>
<c:when test="${scope >= 90}">
<c:out value="优秀"></c:out>
</c:when>
<c:when test="${scope >= 80}">
<c:out value="良"></c:out>
</c:when>
<c:when test="${scope >= 70}">
<c:out value="一般"></c:out>
</c:when>
</c:choose>
</body>
- c:forEach
<body>
<%
ArrayList<String> people = new ArrayList<>();
people.add(0, "张三");
people.add(1, "李四");
people.add(2, "王五");
people.add(3, "赵六");
people.add(4, "张七");
request.setAttribute("list", people);
%>
<c:forEach var="people" items="${list}">
<c:out value="${people}" ></c:out>
</c:forEach>
</body>