赞
踩
作用
写法
package com.bz.oa.web.action;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
// @WebServlet(name="a", urlPatterns = {"/a"})
// urlPatterns只有一个时可以简写,如下
// @WebServlet(name="a", urlPatterns = "/a")
// urlPatterns和value 相同,可以简写,如下
// @WebServlet(name="a", value = "/a")
// 注解中只有value属性时可以省略属性名,如下
@WebServlet("/a")
public class AServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String servletName = getServletName();
out.println("<h1>servlet name = " + servletName + "</h1>");
String contextPath = request.getContextPath();
out.println("<h1>contextPath = " + contextPath + "</h1>");
}
}
JSP文件的扩展名可以不为xxx.jsp。可以在CATALINA_HOME(tomcat)/conf/web.xml文件的中进行配置
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>
在jsp文件直接编写代码,都会自动翻译到对应的java文件中的server方法的out.write(“翻译到这里”);中。
JSP的page指令,解决响应时的中文乱码问题
<!--响应的内容类型为text/html;采用的字符集为:UTF-8-->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
JSP中如何编写java程序,会自动翻译到对应的java文件中的server方法中。
<% java语句; %>
JSP专业注释,不会被翻译到Java源代码中
<%-- JSP注释内容 --%>
声明,会自动翻译到对应的java文件中的server方法之外(很少用)
<%! java代码; %>
如何在浏览器上输出java变量
<%
int num = 10;
%>
<%
out.write("num = " + num);
%>
<%-- 或 --%>
<%=num %>
目录结构
src
|----com.bz.oa
|----bean
|----Dept.java
|----utils
|----DBUtil
|----web.action
|----DeptServlet
|----resources
|----jdbc.properties
web
|----WEB-INF
|----lib
|-----mysql jdbc驱动包
|----web.xml
|----addDept.jsp
|----deptList.jsp
|----detail.jsp
|----editDept.jsp
|----error.jsp
|----index.jsp
addDept.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>新增部门</title>
</head>
<body>
<h1>新增部门</h1>
<hr/>
<form action="<%=request.getContextPath()%>/dept/add" method="post">
部门编号<input type="number" name="deptno" /><br/>
部门名称<input type="text" name="dname" /><br/>
部门位置<input type="text" name="address" /><br/>
<input type="submit" value="保存" />
</form>
</body>
</html>
deptList.jsp
<%@ page import="com.bz.oa.bean.Dept" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta charset='utf-8' />
<title>部门列表</title>
</head>
<body>
<script type="text/javascript">
function del(deptno) {
if (window.confirm('删除后无法 恢复,是否确定删除?')) {
document.location.href = "<%=request.getContextPath()%>/dept/delete?deptno=" + deptno;
}
}
</script>
<h1>部门列表</h1>
<br/>
<table border="1" align="center" width="50%">
<tr>
<th>部门编号</th>
<th>部门名称</th>
<th>操作</th>
</tr>
<%
List<Dept> depts = (List<Dept>)request.getAttribute("deptList");
for(Dept dept : depts) {
%>
<tr>
<td><%=dept.getDeptno()%></td>
<td><%=dept.getDname()%></td>
<td>
<a href="javascript:void(0);" onClick="del(<%=dept.getDeptno()%>)">删除</a>
<a href="<%=request.getContextPath()%>/dept/detail?deptno=<%=dept.getDeptno()%>&isEdit=1">编辑</a>
<a href="<%=request.getContextPath()%>/dept/detail?deptno=<%=dept.getDeptno()%>&isEdit=0">详情</a>
</td>
</tr>
<%
}
%>
</table>
<hr/>
<a href="<%=request.getContextPath()%>/addDept.jsp">新增部门</a>
</body>
</html>
detail.jsp
<%@ page import="com.bz.oa.bean.Dept" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta charset="utf-8" />
<title>部门详情</title>
</head>
<body>
<%
Dept dept = (Dept) request.getAttribute("deptDetail");
String isEdit = request.getParameter("isEdit"); // 0:详情 1:修改
if (isEdit.equals("1")) {
%>
<h1>修改部门</h1>
<hr/>
<form action="<%=request.getContextPath()%>/dept/update" method="post">
部门编号<input type="number" name="deptno" value="<%=dept.getDeptno()%>" readonly />
<br/>
部门名称<input type="text" name="dname" value="<%=dept.getDname()%>" />
<br/>
部门位置<input type="text" name="address" value="<%=dept.getAddress()%>" />
<br/>
<input type="submit" value="保存" />
</form>
<%
} else {
%>
<h1>部门详情</h1>
<hr/>
<p>部门编号:<%=dept.getDeptno()%></p>
<p>部门名称:<%=dept.getDname()%></p>
<p>部门地址:<%=dept.getAddress()%></p>
<input type="button" value="返回" onClick="window.history.back()" />
<%
}
%>
</body>
</html>
editDept.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta charset="UTF-8">
<title>部门编辑</title>
</head>
<body>
<h1>修改部门</h1>
<hr/>
<form action="<%=request.getContextPath()%>/deptList.jsp" method="post">
部门编号<input type="number" name="deptno" value="deptno" readonly />
<br/>
部门名称<input type="text" name="dname" value="dname" />
<br/>
部门位置<input type="text" name="address" value="address" />
<br/>
<input type="submit" value="保存" />
</form>
</body>
</html>
error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>error</title>
</head>
<body>
<h1>操作失败</h1>
<a href="<%=request.getContextPath()%>/dept/list">返回</a>
</body>
</html>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>欢迎使用oa系统</title>
</head>
<body>
<a href="<%=request.getContextPath()%>/dept/list">查看部门列表</a>
</body>
</html>
jdbc.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://ip地址:3306/bztest
user=root
pwd=root
Dept.java
package com.bz.oa.bean;
import java.util.Objects;
/**
* 一个普通的Java类,这个Java类可以封装零散的数据,代表了一个部门对象
*/
public class Dept {
private String deptno;
private String dname;
private String address;
public Dept() {
}
public Dept(String deptno, String dname, String address) {
this.deptno = deptno;
this.dname = dname;
this.address = address;
}
public String getDeptno() {
return deptno;
}
public void setDeptno(String deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Dept{" +
"deptno='" + deptno + '\'' +
", dname='" + dname + '\'' +
", address='" + address + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Dept dept = (Dept) o;
return Objects.equals(deptno, dept.deptno) && Objects.equals(dname, dept.dname) && Objects.equals(address, dept.address);
}
@Override
public int hashCode() {
return Objects.hash(deptno, dname, address);
}
}
DBUtil.java
package com.bz.oa.utils;
import java.sql.*;
import java.util.ResourceBundle;
/**
* JDBC工具类
*/
public class DBUtil {
// 静态变量,在类加载时执行
// 并且是有顺序的,自上而下的顺序
// 注意:这里的resources目录是在src下的
private static ResourceBundle bundle = ResourceBundle.getBundle("resources.jdbc");
private static String driver = bundle.getString("driver");
private static String url = bundle.getString("url");
private static String user = bundle.getString("user");
private static String pwd = bundle.getString("pwd");
static {
try {
// 注册驱动
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 获取数据库连接对象
* @return connection 数据库连接对象
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, pwd);
}
/**
* 释放资源
* @param conn 数据库连接对象
* @param stat 数据库操作对象
* @param res 结果集对象
*/
public static void close(Connection conn, Statement stat, ResultSet res) {
if (res != null) {
try {
res.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stat != null) {
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
DeptServlet.java
package com.bz.oa.web.action;
import com.bz.oa.bean.Dept;
import com.bz.oa.utils.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@WebServlet({"/dept/list", "/dept/detail", "/dept/add", "/dept/delete", "/dept/update"})
public class DeptServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String servletPath = request.getServletPath();
if("/dept/list".equals(servletPath)) {
doList(request, response);
} else if("/dept/detail".equals(servletPath)) {
doDetail(request, response);
} else if("/dept/add".equals(servletPath)) {
doSave(request, response);
} else if("/dept/delete".equals(servletPath)) {
doDel(request, response);
} else if("/dept/update".equals(servletPath)) {
doUpdate(request, response);
}
}
/**
* 修改部门信息
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
private void doUpdate(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
String deptno = request.getParameter("deptno");
String dname = request.getParameter("dname");
String address = request.getParameter("address");
Connection conn = null;
PreparedStatement ps = null;
int count = 0;
try {
conn = DBUtil.getConnection();
String sql = "update t_dept set dname=?, address=? where deptno = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, dname);
ps.setString(2, address);
ps.setString(3, deptno);
count = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, null);
}
String contextPath = request.getContextPath();
if(count > 0) {
// 修改成功
response.sendRedirect(contextPath + "/dept/list");
} else {
// 修改失败
response.sendRedirect(contextPath + "/error.jsp");
}
}
/**
* 连接数据库,查询所有的部门信息,将部门信息收集好,然后跳转到JSP做页面展示
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
private void doList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 准备一个容器,用来专门存储部门
List<Dept> depts = new ArrayList<>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "select deptno, dname, address from t_dept";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()) {
String deptno = rs.getString("deptno");
String dname = rs.getString("dname");
String address = rs.getString("address");
System.out.println(deptno + ", " + dname + ", " + address);
// 将以上零散的数据封装成java对象
Dept dept = new Dept(deptno, dname, address);
// 将部门对象放到集合中
depts.add(dept);
}
// 将部门对象集合放到请求域中
request.setAttribute("deptList", depts);
// 转发(不用重定向)
request.getRequestDispatcher("/deptList.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs);
}
}
/**
* 获取部门详情
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
private void doDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String deptno = request.getParameter("deptno");
String isEdit = request.getParameter("isEdit"); // 0:详情 1:修改
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Dept dept = new Dept();
try {
conn = DBUtil.getConnection();
String sql = "select dname, address from t_dept where deptno = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, deptno);
rs = ps.executeQuery();
if(rs.next()) {
dept.setDeptno(deptno);
dept.setDname(rs.getString("dname"));
dept.setAddress(rs.getString("address"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs);
}
request.setAttribute("isEdit", isEdit);
request.setAttribute("deptDetail", dept);
// 转发到jsp页面
request.getRequestDispatcher("/detail.jsp").forward(request, response);
}
/**
* 添加部门
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
private void doSave(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String deptno = request.getParameter("deptno");
String dname = request.getParameter("dname");
String address = request.getParameter("address");
Connection conn = null;
PreparedStatement ps = null;
int count = 0;
try {
conn = DBUtil.getConnection();
// 开启事务
conn.setAutoCommit(false);
String sql = "insert into t_dept (deptno, dname, address) values (?, ?, ?)";
ps = conn.prepareStatement(sql);
ps.setString(1, deptno);
ps.setString(2, dname);
ps.setString(3, address);
count = ps.executeUpdate();
// 提交事务
conn.commit();
} catch (SQLException e) {
if(conn != null) {
// 回滚事务
try {
conn.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, null);
}
if (count > 0) {
// 新增成功
response.sendRedirect(request.getContextPath() + "/dept/list");
} else {
// 新增失败
response.sendRedirect(request.getContextPath() + "/error.jsp");
}
}
/**
* 删除部门
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
private void doDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
String deptno = request.getParameter("deptno");
Connection conn = null;
PreparedStatement ps = null;
int count = 0;
try {
conn = DBUtil.getConnection();
// 开启事务
conn.setAutoCommit(false);
String sql = "delete from t_dept where deptno = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, deptno);
count = ps.executeUpdate();
// 提交事务
conn.commit();
} catch (SQLException e) {
if (conn != null) {
// 回滚事务
try {
conn.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, null);
}
if (count > 0) {
// 删除成功
response.sendRedirect(request.getContextPath() + "/dept/list");
} else {
// 删除失败
response.sendRedirect(request.getContextPath() + "/error.jsp");
}
}
}
新增t_user表
drop table if exists t_user;
create table t_user (
id int primary key auto_increment,
username varchar(255),
password varchar(255)
);
insert into t_user (username, password) values ('bz', '123');
commit;
select * from t_user;
index.jsp调整
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>欢迎使用oa系统</title>
</head>
<body>
<form action="<%=request.getContextPath()%>/user/login" method="post">
<h1>用户登录</h1>
<hr>
用户名:<input type="text" name="username" /><br/>
密码:<input type="password" name="password"/><br/>
<input type="submit" value="登录">
</form>
<a href="<%=request.getContextPath()%>/dept/list">查看部门列表</a>
</body>
</html>
UserServlet.java
package com.bz.oa.web.action;
import com.bz.oa.utils.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import javax.xml.transform.Result;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@WebServlet("/user/login")
public class UserServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
Connection conn = null;
PreparedStatement ps = null;
ResultSet res = null;
boolean success = false;
try {
conn = DBUtil.getConnection();
String sql = "select * from t_user where username=? and password=?";
ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
res = ps.executeQuery();
if(res.next()) {
success = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, res);
}
String contextPath = request.getContextPath();
if(success) {
// 登录成功
response.sendRedirect(contextPath + "/dept/list");
} else {
// 登录失败
response.sendRedirect(contextPath);
}
}
}
需要使用URL重写机制,在URL后追加“;sessionid=第一次响应的sessionid值”
http://xxxx;sessionid=xxxxxxxxxxxxxxxxx
URL重写机制会导致开发成本加大,原因:每个请求后都需要加sessionid。所以目前的方案是如果不允许cookie,就建议别使用该系统。
第一种:在web.xml文件配置
<!-- session的超时时长是30分钟 -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
第一次请求:服务器生成session对象,并响应给浏览器,浏览器以Cookie形式保存
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1BrX1N0r-1658934971915)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220727214129522.png)]
第二次请求:浏览器从Cookie取sessionid,发送请求时一并发送给服务器,服务器根据拿到的sessionid查询session列表中的数据
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zujuMqwt-1658934971918)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220727214211792.png)]
在java的Servlet规范中,session对应的类名:HttpSession(jakarta.servlet.http.HttpSession)
session机制属于B/S结构的一部分
// 从服务器中获取session对象,如果获取不到,就新建session对象
HttpSession session = request.getSession();
// 从服务器中获取session对象,如果获取不到,返回null
HttpSession session = request.getSession(false);
cookie机制和session机制不属于java的机制,都是HTTP协议。
只要是web开发,不管哪种语言,cookie和session都是需要的。
HTTP协议规定:
Cookie cookie = new Cookie(k, v);
// 单位:秒
// 设置cookie在一小时之后失效
cookie.setMaxAge(60 * 60);
// 手动设置cookie的path
cookie.setPath("/webServlet");
假如当前发送请求路径为:http://localhost:8080/webServlet/aaa/bbb生成cookie
默认情况
手动设置cookie的path
// 如果浏览器没有提交cookie,这个方法返回值为null,并不是返回一个长度为0的数组
Cookie[] cookies = request.getCookies();
if(cookies != null) {
for(Cookie cookie : cookies) {
String name = cookie.getName();
String value = cookie.getValue();
System.out.println(name + " = " + value);
}
}
EL表达式有什么用?
EL表达式出现在JSP中主要是:
EL表达式的三大功效:
EL表达式的基本语法:
${表达式}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。