JSP&EL&JSTL
1.JSP(Java Server Page)
1.JSP简介
从用户角度看待:jsp是一个网页;从底层来看,jsp是一个Java类,他继承了Servlet,所以可以直接说jsp就是一个Servlet。
意义
html 多数情况下用来显示静态网页,但是我们有时候需要在网页上显示一些动态数据。html不支持写Java代码,jsp里面可以写Java代码。为了更好的用户体验,更好的与用户交流。
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Hello,JSP</h1>
</body>
</html>
2.JSP指令
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ 指令名%> jsp指令
jsp的三大指令:include,page,taglib
1.page指令
language=“java” 表明jsp页面中可以写Java代码。
contentType=“text/html; charset=UTF-8” 告知浏览器文件内容类型以及编码方式
text/html MIMEType表明这是一个文本,html网页。
pageEncoding=“UTF-8” jsp内容编码
extends属性说明该jsp文件继承自谁,默认为extends=“httpjspbase”;
import属性导包,快捷键alt+/
session属性,值有‘true’或者‘false’,由于控制这个jsp界面里面能否直接使用session对象。
jsp翻译后的Java文件目录: apache-tomcat-7.0.94\work\Catalina\localhost\JSPDemo01\org\apache\jsp
errorPage 属性,错误的界面,值需要给错误页面的路径。当页面出错时,便会自动跳转到errorPage中。
isErrorPage 属性,指定当前页面为错误页面,该页面可以直接使用exception对象。
2.include指令
<%@ include file="xxx.jsp" %>
用于包含另外一个jsp内容,静态包含,把另外一个页面的内容拿过来一起输出。
3.taglib指令
<%@ taglib prefix="..." uri="..." %>
引入标签库,uri属性引入标签库的路径,prefix属性指定标签库的别名
3.JSP动作标签
1.<jsp:include>
page属性 指定包含的页面,动态包含,先将被包含的页面运行一下,然后在将运行的结果包含到当前页面。
2.<jsp:forward>
page属性 指定要跳转到哪一个页面。等同于请求转发;
request.getRequestDispatcher("xxx.jsp").forward(request,response)
3.<jsp:param name=“xxx” value=“xxx”>
在跳转或者在包含的时候将某个参数传递过去,一般写在<jsp:include>或者<jsp:forward>内。
在传递过去的页面内通过:
request.getParameter("xxx")
获取参数的值。
4.JSP内置对象(四个作用域)
内置对象:我们可以直接在jsp页面中使用的对象,不用创建。
前四个为作用域对象,表示这些对象可以存值,他们的取值范围具有限定。
存值:setAttribute(“name”,value)
取值:getAttribute(“name”)
1.pageContext【PageContext】
作用域只限定当前页面
使用该对象可以获取其他8个jsp内置对象。
2.request【HttpServletRequest】
作用域包含当前页面,使用请求转发时,request也可作用于要跳转到的页面。重定向则不能作用于要跳转到的页面。
作用域只限于一次请求,只要服务器对该请求做出了响应,这个域中的属性值就没有了。
3.session【HttpSession】
作用域限于一次会话(多次请求与响应)。
4.application【ServletContext】
作用域整个工程都可以访问,服务器关闭就不能访问了。
5.config【ServletConfig】
可以获取初始化参数:
config.getInitParameter("xxxx")
6.page【Object】
当前jsp翻译成Java类的实例对象this
7.exception【Throwable】
只有在page指令中isErrorPage="true"的页面中才exception这个内置对象。
8.out【JspWriter】
9.response【HttpServletResponse】
优先输出response对象输出的内容,在输出out输出的内容,把out对象输出的内容放置到response对象的输出缓冲区。
2.EL表达式
为了简化jsp代码,具体一点就是为了简化jsp里面的Java代码。
写法格式
<%
pageContext.setAttribute("name", "page");
request.setAttribute("name", "request");
session.setAttribute("name", "session");
application.setAttribute("name", "application");
%>
按普通手段取值<br/>
<%=pageContext.getAttribute("name") %>
<%=request.getAttribute("name") %>
<%=session.getAttribute("name") %>
<%=application.getAttribute("name") %>
<br>使用EL表达式取值,指定作用域<br>
${ pageScope.name }
${ requestScope.name }
${ sessionScope.name }
${ applicationScope.name }
<br>使用EL表达式取值,不指定作用域<br>
${ name }
不指定作用域,查找顺序为page--request--session--application
如果域中所存的值是数组:
<hr>
<%
String[] a = {"aa","bb","cc"};
pageContext.setAttribute("arr", a);
%>
使用el表达式取出域中数组的值<hr>
${ arr[0] }
${ arr[1] }
${ arr[2] }
如果域中存的是集合数组:
<hr>
<%
List<String> list = new ArrayList<String>();
list.add("dd");
list.add("ee");
list.add("ff");
pageContext.setAttribute("list", list);
%>
使用el表达式取出域中数组的值<hr>
${ list[0] }
${ list[1] }
${ list[2] }
中括号去的是有下标容器中的数据
如果域中存的是Map数据
<hr>
<%
Map map = new HashMap();
map.put("aa", "11");
map.put("bb", "22");
map.put("cc", "33");
map.put("dd", "44");
map.put("dd.ee", "44");
pageContext.setAttribute("map", map);
%>
使用el表达式取出域中Map的值<hr>
${ map.aa }
${ map.bb }
${ map.cc }
${ map.dd }
${ map["dd.ee"] }
取值方式:
如果这份值是有索引(下标)的,那么直接使用中括号,没有下标,就使用"."
从对象取出属性值。
<%
User user = new User("张三",18);
pageContext.setAttribute("user", user);
%>
${ empty user }//判断user是否为空,user为attrname
${ user.name },
${ user.id }
隐式对象(11个内置对象)
- pageContext
作用域相关对象
-
pageScope
-
requestScope
-
sessionScope
-
applicationScope
头信息相关对象
-
header
-
headerValues
请求参数相关对象
-
param
-
paramValues
cookie
全局初始化参数
- initParam
3.JSTL(JSP Standard Tag Library)JSP标准标签库
1.JSTL简介
简化jsp代码编写,替换**<%%>写法,一般与EL表达式**配合使用。
1.导入JSTL支持jar文件,jstl.jar和standard.jar
2.在页面上使用taglib来引入标签库。
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
使用1.1版本,1.0版本不支持EL表达式。
2.JSTL 基本用法
<c:set> 声明一个变量或对象,并将其存放到域中。
String name = "zhangsan";
<c:set value="zhangsan" var="name" scope="session"></c:set>
${ name }默认存到page域中<br>
可以通过scope属性指定存放到那个作用域中。
${ sessionScope.name}
<c:if test=" "> 判断语句,没有else分支,test属性中写EL表达式,var属性可以声明一个变量将判断结果存储起来,scope属性将指定存储的作用域。
<hr/>
<c:set value="18" var="age" scope="session"></c:set>
<c:if test="${ age>26 }">
年龄大于16岁;
</c:if>
<c:if test="${ age<=26 }">
年龄小于等于26岁;
</c:if>
<c:forEach>
<hr>
jstl循环<br/>
step属性指定了循环的步长
<c:forEach begin="1" end="10" step="2" var="i">
${ i }
</c:forEach>
<hr>
<%
List list = new ArrayList();
list.add(new User("aa",1));
list.add(new User("bb",2));
list.add(new User("cc",3));
list.add(new User("dd",4));
pageContext.setAttribute("li", list);
%>
<c:forEach items="${ li }" var="user">
${ user.name },${ user.id }<br/>
</c:forEach>
items属性指定要遍历的对象,必须写EL表达式,var属性指定一个变量名去接收每一次遍历得到的值。
4.实例:学生管理系统
登录页面 login.jsp LoginServlet
1.获取信息
2.查询数据库
3.判断账号信息
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录页面</title>
<style type="text/css">
#div1{
margin-left:auto;
margin-right:auto;
margin-top:300px;
width:400px;
height:300px;
background-color:grey;
text-align:center;
border-radius:20px;
}
#div2{
margin-top:50px;
font-size:35px;
text-align:center;
color:yellow;
}
.inp{
margin-top:10px;
width:200px;
height:40px;
border:1px solid;
border-radius:25px;
font-size:20px;
text-align:center;
}
.btn{
margin-top:10px;
width:150px;
height:40px;
font-size:18px;
border:1px solid;
border-radius:50px;
text-align:center;
}
</style>
</head>
<body>
<div id="div1">
<div id="div2">欢迎使用学生管理系统</div>
<form action="LoginServlet" method="post">
<input class="inp" type="text" name="account" placeholder="请输入你的账户"><br/>
<input class="inp" type="password" name="password" placeholder="请输入你的密码"><br>
<input class="btn" type="submit" value="登入">
</form>
</div>
</body>
</html>
LoginServlet.java
package com.nike.web.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.nike.web.bean.Stu;
import com.nike.web.dao.StuDao;
import com.nike.web.dao.UserDao;
import com.nike.web.dao.impl.StuDaoImpl;
import com.nike.web.dao.impl.UserDaoImpl;
/**
* 用于处理登录的Servlet;
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
UserDao userDao = new UserDaoImpl();
StuDao stuDao = new StuDaoImpl();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
String user = request.getParameter("account");
String pwd = request.getParameter("password");
System.out.println(user+","+pwd);
boolean isLogin = userDao.login(user, pwd);
if(isLogin){
//1.查询出来所有的学生信息
List<Stu> stus = stuDao.findAll();
request.getSession().setAttribute("stus", stus);
//2.跳转
response.sendRedirect("stu_list.jsp");
}else{
response.getWriter().println("用户名或者密码错误");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
stu_list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>学生信息管理系统</title>
</head>
<body>
<table border="1" width="700px">
<tr align="center">
<td>编号</td><td>姓名</td><td>年龄</td><td>性别</td><td>地址</td><td>操作</td>
</tr>
<c:forEach var="stu" items="${ sessionScope.stus }">
<tr align="center">
<td>${ stu.id }</td><td>${ stu.name }</td><td>${ stu.age }</td><td>${ stu.gender }</td><td>${ stu.address }</td><td><a href="#">删除</a> <a href="#">修改</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>