1.EL表达式
1.1EL表达式定义
Expression Language(表达式语言)
1.2EL功能
替代JSP页面中数据访问时的复杂编码
1.3为什么使用EL
<%= ( (User)request.getAttribute("user") ).getName() %>
等价于:
${user.name}
1.4表达式语法
${}
2.JSTL表达式:
2.1JSTL定义:
JSP标签标准库(JSP Standard Tag Library)
2.2为什么使用JSTL
EL表达式中可以替代JSP的复杂代码,但是无法添加逻辑判断
EL表达式封装了数据访问的功能,而JSTL标签库则封装了逻辑控制、循环控制以及数据格式化等功能,二者结合使用才能完整实现动态页面的开发需求
2.3JSTL表达式语法
首先导入jar包,再添加指令
jstl.jar
Standard.jar
创建web工程,在WEB-INF中创建一个lib文件夹,将两个jar包复制进文件夹,然后依次右键点击add as library
常用标签:
条件标签:if choose 迭代标签:forEach
3.练习
实现需求:
类似的表格,动态数据
zs | 666 | | 2 | lisi | 777 | 3 | wangwu | 888 |
实现步骤:
1.创建web工程:
2.导包:
在WEB-INF中创建lib包,导入jstl.jar包和standard.jar包,右击选中add as library
3.导入tomcat包:
点击file,点击setting下面的project structure,选择左侧Libraries,点击+添加java,找到tomcat安装目录的bin目录,选择里面的jsp-api.jar和servlet-api-jar,选择ok
4.部署tomcat
点击右边的edit configurations,点击+,选择tomcat的local,Application server中选择Tomcat的安装目录,在De'ployment中点击添加添加Artfict,将下面的路径改为/,返回Server将两个热部署选择,点击ok
5.创建entity/User.java
在src中创建entity实体类包,创建素材User用来接收数据库数据,通常属性和数据库字段相同
package entity;
public class User {
private Integer id;
private String name;
private String password;
public User() {
}
public User(Integer id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
?6.创建UserServlet
在src中创建另一个servlet包,创建UserServlet文件,?目前没有实现数据库连接
package servlet;
import entity.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
@WebServlet(name = "UserServlet")
public class UserServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//准备数据
ArrayList<User> list = new ArrayList<User>();
User user1 = new User(1,"杨幂","111");
User user2 = new User(22,"杨幂","111");
User user3 = new User(333,"杨幂","111");
list.add(user1);
list.add(user2);
list.add(user3);
//绑定值
request.setAttribute("list",list);
//转发代码
request.getRequestDispatcher("index.jsp").forward(request,response);
}
}
?7.web.xml
实现servlet文件的添加:
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>servlet.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/UserServlet</url-pattern>
</servlet-mapping>
?8.index.jsp
<head>
<title>$Title$</title>
</head>
<body>
<table border="1px solid red" cellpadding="0" cellspacing="0">
<tr>
<td>用户id</td>
<td>用户名称</td>
<td>用户密码</td>
</tr>
//c:forEach迭代标签
<c:forEach var="user" items="${list}"> //items代表从UserServlet传递来的值,var为临时变量
<tr>//只需要一行就行,显示数据库中的内容
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.password}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
?9.c:if 实现条件判断
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<table border="1px solid red" cellpadding="0" cellspacing="0">
<tr>
<td>用户id</td>
<td>用户名称</td>
<td>用户密码</td>
</tr>
<c:forEach var="user" items="${list}">
<tr>
<td>${user.id}</td>
<%-- <td>${user.name}</td>--%>
<c:if test="${user.name eq '杨幂'}">
<td>yangmi</td>
</c:if>
<td>${user.password}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
?10:c:choose非此及彼
<c:choose>
<c:when test="${user.name eq '杨幂'}"><td>yangmi</td></c:when>
<c:otherwise><td>迪丽热巴</td></c:otherwise>
</c:choose>
?结构如上:判断选择
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<table border="1px solid red" cellpadding="0" cellspacing="0">
<tr>
<td>用户id</td>
<td>用户名称</td>
<td>用户密码</td>
</tr>
<c:forEach var="user" items="${list}">
<tr>
<td>${user.id}</td>
<c:choose>
<c:when test="${user.name eq '杨幂'}"><td>yangmi</td></c:when>
<c:otherwise><td>迪丽热巴</td></c:otherwise>
</c:choose>
<td>${user.password}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
11.实现连接数据库
package servlet;
import entity.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
@WebServlet(name = "UserServlet")
public class UserServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// //准备数据
// ArrayList<User> list = new ArrayList<User>();
// User user1 = new User(1,"杨幂","111");
// User user2 = new User(22,"dilireba","111");
// User user3 = new User(333,"杨幂","111");
// list.add(user1);
// list.add(user2);
// list.add(user3);
//访问数据库
ArrayList<User> list = new ArrayList<User>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/work1","root","111");
ps = conn.prepareStatement("select * from t_student");
rs = ps.executeQuery();
while (rs.next()){
User user = new User(rs.getInt("id"),rs.getString("name"),rs.getString("password"));
list.add(user);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//绑定值
request.setAttribute("list",list);
//转发代码
request.getRequestDispatcher("index.jsp").forward(request,response);
}
}
?4.增,删,改
增删改主体相同,sql语句不同,查需要调用返回值,略有不同
//加,获取要添加的内容
String name = request.getParameter("name");
String password = request.getParameter("password");
//定义连接,方便关闭
Connection conn = null;
PreparedStatement ps = null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//创建连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/work1", "root", "111");
//填写sql语句
ps = conn.prepareStatement("insert into t_student(`name`,password) value(?,?)");
//设置sql语句中的值,1,2为第几个问号
ps.setObject(1, name);
ps.setObject(2, password);
//更新数据库
ps.executeUpdate();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
//关闭连接,释放资源
try {
if (ps != null){
ps.close();
}
if (conn != null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//转发请求
request.getRequestDispatcher("/UServlet?flag=list").forward(request, response);
?5.查:
//创建传递的数组
ArrayList<User> list = new ArrayList<User>();
//全局变量
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//创建连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/work1","root","111");
//sql语句
ps = conn.prepareStatement("select * from t_student");
//获取返回值
rs = ps.executeQuery();
//遍历塞值
while (rs.next()){
User user = new User(rs.getInt("id"),rs.getString("name"),rs.getString("password"));
list.add(user);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//绑定request
request.setAttribute("list",list);
//页面转发
request.getRequestDispatcher("index.jsp").forward(request,response);
?
6.单个Servlet实现多个页面跳转
关键就是标识:
在访问页面的时候需要添加标识的属性,例如:http://localhost:8080/UServlet?flag=add&...
?是代表绑定数据,&是代表和,绑定多个数据用,里面可以使用id=${user.id}等来实现动态值的传入
package servlet;
import entity.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
@WebServlet(name = "UServlet")
public class UServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String flag = request.getParameter("flag");//获取标识
//判断并进入不同的页面
if (flag.equals("list")){
} else if (flag.equals("add")){
} else if (flag.equals("del")){
} else if (flag.equals("update")){
} else if (flag.equals("look")) {
}
}
}
不同的flag进入不同的模块
|