jsp+servlet实战项目
阅读原文时间:2023年08月09日阅读:3

第一步:新建maven项目,项目中添加dao,entity,service,servlet,util包
第二步:导入依赖

第三步:数据库建表

第四步:entity实体包(疯转)

第五步:在util工具包中添加工具类

第六步:Dao包中创建Impl包,Impl包中放EmployeeDaoImpl类和UseDaoImpl类;Employee接口,UseDao接口

第七步:UseDao接口中写能实现登录和注册的方法

public Use findByUse(String username,String password)登录

public Integer register(Use use)(注册)

第八步:EmployeeDao接口写能实现增删改查的方法

public Integer addEmployee(Employee employee)

public Integer delById(Integer id)

public Integer updateEmployee(Employee employee)

public Listfindall()

public Employee findbById(Integer id)

第九步:EmployeeDapImpl类(增删改查功能)

增public Integer addEmployee(Employee employee){

return supper . executeUpdate("insert into t_emolyee(name,salary,age)value(?,?,?)",new Object[]{

employee.getName();employee.getSalary(),employee.getAge()});}

删public Integer delById(Integer id){

return super.exectueUpdate("delete  from t_emolyee where id=?",new Objject [ ]{id});}

改public Integer updateEmployee(Employee employee){

return super.executeUpdate("update t_emolyee set name=?,salary=?,age=? where id=? ",new Object[]{

employee.getName();employee.getSalary(),employee.getAge(),employee.getId()});}

查public Listfindall(){

Listlist=new ArrayList<>();

String sql="select * from t_employee"

Object [] params=null;

rs=super.ececuteQuery(sql,params)'

try catch finally

while(rs.(next)){

Employee employee=new Employee();

employee . setId(rs.getInt("id"))

employee . setName(rs.getString("name"))

employee.setSalary(rs.getString("salary"))

employee. setAge(rs.getInt("age"))

list.add(employee)

return list;

查对象

public Employe findById(Integer id)

Employee employee=new Employee();

rs=super.ExecuteQuary("select * from t_employee where id=?",new Object []{id});

try catch finally

while (rs.next()){

employee.setId(rs.getInt("id"));

employee.setName(rs.getString("name"));

employee.setSalary(rs.getString("salary"));

employee.setAge(rs.getInt("age"));}

return employee;

第十步:UseDaoImpl类(实现登录和注册)

登录(去查询数据库的数据能否和username和passworad匹配)

public Use findByUse(String username,String password){

Use uu=null;

rs=super.executeQuary("select * from t_use where username=? and password=?",new Object[]{

username,pasword});

try catch finally

while(rs.next()){

uu=new Use()

uu.setUsername(rs.getString("username"));

uu.setRealname(rs.getString("realname"));

uu.setPaaword(rs.getString("password"));}

super.closecon

return uu;

注册(用添加方法。注册完成后,把注册的值传到数据库)

public Integer register(Use use){

return super.executeUpdate("insert into t_use(username,realname,password)value(?,?,?)",new Object[]{

use.getUsername(),use.getRealname(),use.getPassword});}

第十一步:

service包中添加Impl包包里放EmployeeServiceImpl类和UseServiceImpl类

service包中的EmployeeServiceImpl有一个EmployeeService接口

service包中的UseServiceImpl有一个UseService接口

接口中的方法与dao接口中的方法一致

第十二步:

在servlet包中添加Servlet 类型的UseServlet类和employeeServlet类

useServlet包:

public class UseServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
UseServiceImpl usi=new UseServiceImpl();
String action=request.getParameter("action");
if (action.equals("register")){
String username = request.getParameter("username");
String realname = request.getParameter("realname");
String password = request.getParameter("password");
Use use=new Use();
use.setUsername(username);
use.setRealname(realname);
use.setPassword(password);
Integer flag = usi.register(use);
if (flag>0){
request.getRequestDispatcher("login.jsp").forward(request,response);
}
}else if (action.equals("login")){
String username=request.getParameter("username");
String password=request.getParameter("password");
Use use=usi.findByUse(username,password);
if (use!=null){
request.getRequestDispatcher("EmployeeServlet?action=employeeList").forward(request,response);
} } } }

第十二步:配置web.xml


http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app\_3\_1.xsd" version="3.1">

EmployeeServlet cn.kgc.servlet.EmployeeServlet
UseServlet cn.kgc.servlet.UseServlet

因为有两个Servlet所以要写两个。上面都写各自的路径,下面都写各自的url名
UseServlet /UseServlet
EmployeeServlet /EmployeeServlet

第十三步:

在webapp中导入css样式(样式是别人给的直接复制粘贴过来)

第十四步:建立register.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

regist

注册

**
把在网页中获得的对象塞到servlet中的requestg中**
用户名:
真实姓名:
密码:

**有一个跳转到登录界面的按钮**



第十五步 建立login . jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

login

登录

username:
password:



第十六步:建立list.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

regist

注册

用户名:
真实姓名:
密码:



第十七步.建立EmoployeeServlet类

public class EmployeeServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
String action=request.getParameter("action");
if (action.equals("employeeList")){
EmployeeServiceImpl esi=new EmployeeServiceImpl();
List list = esi.findAll();
request.setAttribute("list",list);
request.getRequestDispatcher("list.jsp").forward(request,response);
}
}
}