黑马JavaWeb案例整理记录
文章目录
案例源码:
一、需求
实现页面跳转和用户信息的增删改查操作
二、设计
1、技术选型
Servlet+JSP+MySQL+JDBCTempleat+Duird+BeanUtilS+tomcat+BootStrap
2、数据库设计
SQL语句
create database heima; -- 创建数据库
use heima; -- 使用数据库
create table day17_user( -- 创建表
id int primary key auto_increment,
name varchar(20) not null,
gender varchar(5),
age int,
address varchar(32),
qq varchar(20),
email varchar(50)
);
三、开发步骤
1. 环境搭建
1. 创建数据库环境
2. 创建项目,导入需要的jar包
2. 编码
3. 测试
4. 部署运维
本笔记(不包含测试和运维)
四、详细过程
1、创建web项目,部署tomcat,导入jar包
jar包下载地址(里边有很多调出合适的)
链接:https://pan.baidu.com/s/1V0X99u6U1zoDyFy2xm9Cyg
提取码:h8sz
2、编写首页index.jsp
3、编写servlet、servie、daomain、dao
导入druid.properties,导入JDBCUtils类。源码都在源码链接中
五、案例升级实现
1、实现内容
- 列表查询
- 登录
- 添加
- 删除
- 修改
2、list.jsp新增设计
按钮位置
输入框
分页条
复选框
3、实现登录功能
login.jsp设计
用到了checkCodeServlet,在我的其他博客写了,链接??
login.jsp点击切换验证码实现,如下图
LoginServlet编写
@WebServlet(value = "/loginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.设置编码
request.setCharacterEncoding("utf-8");
//2.获取数据
//2.1获取用户填写验证码
String verifycode = request.getParameter("verifycode");
//3.验证码校验
HttpSession session = request.getSession();
String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER");
session.removeAttribute("CHECKCODE_SERVER");
//忽略大小写判断相等
if (!checkcode_server.equalsIgnoreCase(verifycode)){
//盐城吗不正确
//提示信息
request.setAttribute("login_msg", "验证码错误!");
//跳转登录页面
request.getRequestDispatcher("/login.jsp").forward(request,response);
return;
}
Map<String, String[]> map = request.getParameterMap();
//4.封装User对象
User user = new User();
try {
BeanUtils.populate(user, map);
}catch (IllegalAccessException e){
e.printStackTrace();
}catch (InvocationTargetException e){
e.printStackTrace();
}
//5.调用Srvice查询
UserService service = new UserServiceImpl();
User loginUser = service.login(user);
//6.判断是否登录成功
if(loginUser != null){
//登录成功
//将用户存入session
session.setAttribute("user",loginUser);
//跳转页面
response.sendRedirect(request.getContextPath()+"/index.jsp");
}else{
//登录失败
//提示信息
request.setAttribute("login_msg","用户名或密码错误!");
//跳转登录页面
request.getRequestDispatcher("/login.jsp").forward(request,response);
}
}
}
UserDaoImpl语句
/**
* 登录账号
* @param username
* @param password
* @return
*/
@Override
public User findUserByUsernameAndPassword(String username, String password) {
try {
String sql = "select * from day17_user where username = ? and password= ?";
User user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), username, password);
return user;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
4、实现添加功能
add.jsp编写
源码在顶部链接
注意事项:提交路径
AddUserServlet编写
@WebServlet(value = "/addUserServlet")
public class AddUserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.设置编码
request.setCharacterEncoding("utf-8");
//2.获取参数
Map<String, String[]> map = request.getParameterMap();
//3.封装对象
User user = new User();
try {
BeanUtils.populate(user, map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
//4.调用Service保存
UserService service = new UserServiceImpl();
service.addUser(user);
//5.跳转到userListServlet
response.sendRedirect(request.getContextPath()+"/userListServlet");
}
}
userDaoImpl语句
/**
* 添加user
* @param user
*/
@Override
public void add(User user) {
String sql = "insert into day17_user values(null,?,?,?,?,?,?,null,null)";
// template.update(sql, user.getName(), user.getGender(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail());
template.update(sql, user.getName(), user.getGender(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail());
}
5、实现删除功能
list.jsp修改
DelUserServlet
@WebServlet("/delUserServlet")
public class DelUserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取id
String id =request.getParameter("id");
//2.调用service删除
UserService service = new UserServiceImpl();
service.deleteUser(id);
//3.跳转到查询所有Servlet
response.sendRedirect(request.getContextPath()+"/userListServlet");
}
}
userDaoImpl增加语句
/**
* 根据id删除
* @param id
*/
@Override
public void delete(int id) {
String sql = "delete from day17_user where id = ?";
template.update(sql,id);
}
6、修改功能实现
注意:隐藏域提交id。下面内容会提到
list.jsp增加修改发送路径
<a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/findUserServlet?id=${user.id}">修改</a>
FindUserServlet编写
@WebServlet(value = "/findUserServlet")
public class FindUserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取id
String id = request.getParameter("id");
//2.调用Service查询
UserService service = new UserServiceImpl();
User user = service.findUserBuId(id);
//3.将user存入request
request.setAttribute("user",user);
//4.转发到update.jsp
request.getRequestDispatcher("/update.jsp").forward(request,response);
}
}
update.jsp编写
id提交隐藏域如下
以下注意地点举例,具体请查看开篇源码链接。
UpdateUserServlet编写
@WebServlet(value = "/updateUserServlet")
public class UpdateUserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.设置编码
request.setCharacterEncoding("utf-8");
//2.获取map
Map<String, String[]> map = request.getParameterMap();
//3.封装对象
User user = new User();
try {
BeanUtils.populate(user, map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
//4.调用Service修改
UserService service = new UserServiceImpl();
service.updateUser(user);
//5.跳转到查询所有Servlet
response.sendRedirect(request.getContextPath()+"/userListServlet");
}
}
UserServiceImpl实现方法
UserDaoImpl实现方法
7、删除选中功能
list.jsp编写
DelSelectServlet编写
@WebServlet(value = "/delSelectServlet")
public class DelSelectServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取所有id
String[] ids = request.getParameterValues("uid");
//
UserService service = new UserServiceImpl();
service.delSelectUser(ids);
//
response.sendRedirect(request.getContextPath()+"/userListServlet");
}
}
UserServiceImpl编写
@Override
public void delSelectUser(String[] ids) {
//判断有东西才删除,否则会空指针异常
if (ids != null && ids.length>0){
//1.遍历数组
for (String id : ids){
//2.2.调用dao删除
dao.delete(Integer.parseInt(id));
}
}
}
UserDaoImpl编写
还是用之前写的,不过就是上一步的service将选中的id列表挨个遍历传进来,挨个删除,还是用以前的方法
/**
* 根据id删除
* @param id
*/
@Override
public void delete(int id) {
String sql = "delete from day17_user where id = ?";
template.update(sql,id);
}
8、实现全选,全不选和提示是否删除框
list.jsp编写实现
有选中条目才提交删除
同样Service中也要判断一下提交的里边有无内容,不判断的话有时候会空指针异常
9、分页查询功能(分页展示数据)
改index.jsp路径
10、多条件组合查询
未完待续…
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/91105.html