✅作者简介:热爱后端语言的大学生,CSDN内容合伙人 ✨精品专栏:C++面向对象 🔥系列专栏:javaweb 文章目录 1、需要导入的jar包以及项目结构速览2、工具类放
🔥前言
书接上文,今天我们来规范书写servlet代码,分包分类的规划项目结构。我们知道一个项目靠一个人是很难完成的,那么在团队合作的时候难免会有看不懂队友写的代码的情况出现。因此按照特定的格式来书写代码无疑可以一定程度上减少这种事情发生的概率。
工具类最终版JDBCUtils
可参考我JDBC专栏里的博文,资源文件就是图中的jdbc.properties
,里面存放数据库的驱动路径、访问路径、账号名和密码。
以此次博客要分享的项目为例:
我要做的是一个有登录和增删查询功能的网页,因此可以定义一个Person
类,实现Serializable
接口(形成习惯),封装姓名、密码等属性并提供对应的构造方法(不要忘了写无参构造)
具体代码如下:
package com.qj.entity;import java.io.Serializable;public class Person implements Serializable { private Integer id; private String name; private String pwd; private String QQ; private String beizhu; @Override public String toString() { return "Person{" + "id=" + id + ", name='" + name + '\'' + ", pwd='" + pwd + '\'' + ", QQ='" + QQ + '\'' + ", beizhu='" + beizhu + '\'' + '}'; } public Person(Integer id, String name, String pwd, String QQ, String beizhu) { this.id = id; this.name = name; this.pwd = pwd; this.QQ = QQ; this.beizhu = beizhu; } public Person() { } public Person(String name, String pwd) { this.name = name; this.pwd = pwd; } public String getQQ() { return QQ; } public void setQQ(String QQ) { this.QQ = QQ; } public String getBeizhu() { return beizhu; } public void setBeizhu(String beizhu) { this.beizhu = beizhu; } public Integer getId() { return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; }}
dao
层是与数据库对接的,存放操作数据库的接口方法。而在dao包里面还有一个impl
包用来存放接口的实现类,用来操作数据库,例如:增删改查。
这里提供了几种方法接口:
person
对象list
集合中person
信息插入到数据库中id
删除数据库中的person
package com.qj.dao;import com.qj.entity.Person;import java.util.List;public interface PersonDao { Person selectByName(String name); List<Person> selectAll(); int insertPerson(Person person); int deletePerson(Integer id);}
这里就是针对PersonDao
接口的具体实现代码,如果有看不懂的可以参考我的JDBC
专栏的文章
package com.qj.dao.impl;import com.qj.dao.PersonDao;import com.qj.entity.Person;import com.qj.util.JDBCUtils;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;public class PersonDaoImpl implements PersonDao { @Override public Person selectByName(String name) { Connection conn = null; PreparedStatement ps=null; ResultSet rs = null; Person person = null; try { conn = JDBCUtils.getConnection(); String sql = "select *from t_Person where p_name=?"; ps = conn.prepareStatement(sql); ps.setString(1, name); rs = ps.executeQuery(); if (rs.next()) { String l_name = rs.getString("p_name"); String l_pwd = rs.getString("p_passWord"); person = new Person(l_name, l_pwd); } } catch (Exception e) { System.out.println("未知异常"); e.printStackTrace(); }finally { JDBCUtils.close(rs, ps, null); } return person; } @Override public List<Person> selectAll() { Connection conn = null; PreparedStatement ps=null; ResultSet rs = null; List<Person> list = new ArrayList<>(); try { conn = JDBCUtils.getConnection(); String sql = "select *from t_Person"; ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while (rs.next()) { Integer id = rs.getInt("p_id"); String name = rs.getString("p_name"); String pwd = rs.getString("p_password"); String QQ = rs.getString("p_QQ"); String beizhu = rs.getString("p_bz"); Person p = new Person(id, name, pwd, QQ,beizhu); list.add(p); } } catch (Exception e) { System.out.println("未知异常"); e.printStackTrace(); }finally { JDBCUtils.close(rs, ps, null); } return list; } @Override public int insertPerson(Person person) { Connection conn = null; PreparedStatement ps=null; int n=0; try { conn = JDBCUtils.getConnection(); String sql = "insert into t_Person value(null,?,?,?,?)"; ps = conn.prepareStatement(sql); ps.setString(1, person.getName()); ps.setString(2, person.getPwd()); ps.setString(3, person.getQQ()); ps.setString(4, person.getBeizhu()); n = ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); }finally { JDBCUtils.close(null, ps, null); } return n; } @Override public int deletePerson(Integer id) { Connection conn = null; PreparedStatement ps=null; int n=0; try { conn = JDBCUtils.getConnection(); String sql = "delete from t_Person where p_id=?"; ps = conn.prepareStatement(sql); ps.setInt(1, id); n = ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); }finally { JDBCUtils.close(null, ps, null); } return n; }}
service
层存放调用dao层的接口,和其名字一样,是为了方便用户操作数据库而设立的。而service层下也有impl层,存放service接口的实现类。
login
为登录、showAllPerson
为显示数据库信息add
与remove
分别对应增加和删除功能package com.qj.service;import com.qj.entity.Person;import java.util.List;public interface PersonService { boolean login(String name, String pwd); List<Person> showAllPerson(); int add(Person person); int remove(Integer id);}
package com.qj.service.impl;import com.qj.dao.PersonDao;import com.qj.dao.impl.PersonDaoImpl;import com.qj.entity.Person;import com.qj.service.PersonService;import java.util.List;public class PersonServiceImpl implements PersonService { PersonDao pd = new PersonDaoImpl(); @Override public boolean login(String name, String pwd) { Boolean boo=false; Person p = pd.selectByName(name); if (p != null) { if (p.getPwd().equals(pwd)) { boo = true; } } return boo; } @Override public List<Person> showAllPerson() { return pd.selectAll(); } @Override public int add(Person person) { return pd.insertPerson(person); } @Override public int remove(Integer id) { return pd.deletePerson(id); }}
由于servlet中的service方法具有两种参数:用户请求参数req
和响应参数resp
,因此把其分到两个包下更合理。
req
放在controller
包中,负责接收用户数据以及向view中传送数据 req.setAttribute()
String
类型,第二个是Object
类型req.getRequestDispatcher().forward(req,resp)
/
开头.forword(req,resp)
(我老是忘加,导致数据无法传送)resp
放在view
包中,接收controller包传送的数据并将效果显示到浏览器页面 req.getAttribute()
Object
,通常需要进行类型强转boolean boo = (boolean) req.getAttribute("boo")
resp.sendRedirect()
/
开头这里包引入情况基本一致,因此博文里只粘贴一次,此外要注意对外访问路径均采用注解的方式
package com.qj.controller;import com.qj.service.PersonService;import com.qj.service.impl.PersonServiceImpl;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;@WebServlet("/login")public class LoginController extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String name = req.getParameter("username"); String pwd = req.getParameter("pwd"); PersonService ps = new PersonServiceImpl(); Boolean boo = ps.login(name, pwd); req.setAttribute("boo", boo); req.getRequestDispatcher("/loginView").forward(req, resp); }}
@WebServlet("/addPerson")public class AddPersonController extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); String QQ = req.getParameter("QQ"); String bz = req.getParameter("bz"); Person p = new Person(null, name, null, QQ, bz); PersonService ps = new PersonServiceImpl(); int n = ps.add(p); req.setAttribute("n", n); req.getRequestDispatcher("/addPersonView").forward(req,resp); }}
@WebServlet("/showPerson")public class ShowPersonController extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PersonService ps = new PersonServiceImpl(); List<Person> list = ps.showAllPerson(); req.setAttribute("list", list); req.getRequestDispatcher("/showPersonView").forward(req,resp); }}
@WebServlet("/delete")public class DeletePersonController extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { int id = new Integer(req.getParameter("id")); PersonService ps = new PersonServiceImpl(); int n = ps.remove(id); req.setAttribute("n", n); req.getRequestDispatcher("/deletePersonView").forward(req, resp); }}
package com.qj.view;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.io.PrintWriter;@WebServlet("/loginView")public class LoginView extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); PrintWriter pw = resp.getWriter(); boolean boo = (boolean) req.getAttribute("boo"); if (boo) { resp.sendRedirect("/MyfirstServlet/showPerson"); }else{ resp.sendRedirect("/MyfirstServlet/login.html"); } }}
@WebServlet("/addPersonView")public class AddPersonVIew extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { int n = (int) req.getAttribute("n"); if (n > 0) { resp.sendRedirect("/MyfirstServlet/showPerson"); } else { resp.sendRedirect("https://blog.csdn.net/MyfirstServlet/add.html"); } }}
@WebServlet("/showPersonView")public class ShowPersonView extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setCharacterEncoding("UTF-8"); resp.setContentType("text/html"); List<Person> list = (List<Person>) req.getAttribute("list"); PrintWriter pw = resp.getWriter(); pw.println(" "); pw.println("ID 姓名 QQ 备注"+"
"); for (Person p : list) { pw.print(+p.getId()+" "); pw.print(p.getName()+" "); pw.print(p.getQQ()+" "); pw.print(p.getBeizhu()+" "); pw.print("点击删除"); pw.println("
"); } pw.println("添加好友"); pw.println(""); pw.flush(); }}
@WebServlet("/deletePersonView")public class DeletePersonView extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setCharacterEncoding("utf-8"); resp.setContentType("text/html"); int n = (int) req.getAttribute("n"); PrintWriter pw = resp.getWriter(); if (n > 0) { resp.sendRedirect("/MyfirstServlet/showPerson"); } else { pw.println("删除失败!"); } pw.flush(); }}
在实战开发中为了提高调试效率会经常使用注解开发,给dao层或者service层阿进行方法测试
这里我就测试了一下添加和显示的功能:
package com.qj.test;import com.qj.entity.Person;import com.qj.service.PersonService;import com.qj.service.impl.PersonServiceImpl;import org.junit.Test;import java.util.List;public class PersonTest { PersonService ps = new PersonServiceImpl(); @Test public void showPserson(){ List<Person> list = ps.showAllPerson(); for (Person person : list) { System.out.println(person.toString()); } } @Test public void addPerson(){ Person person = new Person(null, "张三", null, "3110124130", "淳朴张"); int n = ps.add(person); System.out.println(n); }}
测试效果:
这里第一行的
1
意为成功插入一条数据,后面两条person
属性就是数据库中的查询结果
DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>添加好友title>head><body>输入好友信息:<br> <fORM action="/MyfirstServlet/addPerson" method="get"> 名字:<input type="text" name="name"><br> Q Q:<input type="text" name="QQ"><br> 备注:<input type="text" name="bz"><br> <input type="submit" value="添加"> form>body>html>
DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>登录界面title>head><body> <form action="/MyfirstServlet/login" method="get"> 登录账号:<input type="text" name="username"><br> 登录密码:<input type="password" name="pwd"><br> <input type="submit" value="提交"> form>body>html>
表格结构
登录界面
显示页面
添加好友界面
添加成功跳转至显示页面
删除好友并跳转至显示页面
以我目前所学就只能做到这儿了,项目虽然很简陋,但主要想分享给大家项目结构的构建。码文不易,希望能得到你们的支持,以此来激励我不断更文!
来源地址:https://blog.csdn.net/m0_58618795/article/details/126980612
--结束END--
本文标题: 【Servlet】规范项目结构|基于Mysql+JDBC+Servlet 制作简易网页|实现登录、添加、删除、显示的功能
本文链接: https://lsjlt.com/news/398491.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0