servlet

Dao包

Userdao
package dao;

import model.User;
import util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {

    public int add(User user)  {
    Connection connection = null;
    PreparedStatement pstmt =null;
    int count = 0;
    try{
        connection = DBUtil.getConnection();
        //获得执行sql的Statement对象
        pstmt = connection.prepareStatement("insert into user (name,password,age)values(?,?,?)");
        pstmt.setString (1,user.getName());
        pstmt.setString (2,user.getPassword());
        pstmt.setString (3,user.getAge());
        //执行sql,获得结果
        count = pstmt.executeUpdate();
        System.out.println("insert操作:"+count);
        return count;
    }catch (Exception e)
    {
        e.printStackTrace();
    }finally {
        try {
            DBUtil.closeAll(null, pstmt, connection);
        }
         catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return count;
    }


    public int del(User user){
        Connection connection = null;
        PreparedStatement pstmt = null;
        int count = 0;
        try{
            connection = DBUtil.getConnection();
            System.out.println("连接成功");

            pstmt = connection.prepareStatement("delete from user where name=? and password=? and age = ?");
            pstmt.setString(1,user.getName());
            pstmt.setString(2,user.getPassword());
            pstmt.setString(3,user.getAge());

            count = pstmt.executeUpdate();
            System.out.println("delete操作:"+count);
            return count;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                DBUtil.closeAll(null,pstmt,connection);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return count;
    }

    public User selectByName(String name){
        ResultSet rs = null;
        Connection connection = null;
        PreparedStatement pstmt=null;
        DBUtil util=new DBUtil();
        User user=new User();
        try{
            connection=util.getConnection();
            pstmt=connection.prepareStatement("select*from a where name=?");
            pstmt.setString(1,name);

            while(rs.next()){
                user.setId(rs.getInt(1));
                user.setName(rs.getString(2));
                user.setPassword(rs.getString(3));
                user.setAge(rs.getString(4));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                util.closeAll(rs,pstmt,connection);
            }catch (SQLException e){
                e.printStackTrace();
            }
            return user;
        }
    }
}

Model包

User
package model;

public class User {
    private int id;
    private String name;
    private String password;
    private String age;

    //alt+insert

    public int getId() {
        return id;
    }

    public void setId(int 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;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}

Service包

UserService
package service;

import dao.UserDao;
import model.User;
import java.sql.SQLException;

public class UserService {

    UserDao userDao = new UserDao();
    public int add(User user)  {
        System.out.println("service中add方法被调用");
        return userDao.add(user);

    }
    public int del(User user){
        System.out.println("service中del方法被调用");
        return userDao.del(user);
    }
    public User selectByName(String name){
        return userDao.selectByName(name);
    }

}

Servlet包

AddServlet

其中包含了insert添加,search搜索,delete删除 3种方法

package servlet;

import model.User;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

import static org.omg.IOP.TaggedProfileHelper.insert;

public class AddServlet extends HttpServlet {
    UserService userService = new UserService();

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, IOException {
        System.out.println("执行了dopost");
        String method = req.getParameter("method");
        if(method.equals("save")){
            insert(req,resp);
            }else if (method.equals("search")){
                search(req,resp);
            }
        }

    public void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, IOException{
    User user = new User();
    String name = req.getParameter("name");
    String password = req.getParameter("password");
    String age = req.getParameter("age");

    user.setName(name);
    user.setPassword(password);
    user.setAge(age);
    System.out.println(user);

    int count = userService.add(user);
    if(count>0)
    {
    resp.sendRedirect("/index.jsp");
    }else{
        resp.getWriter().write("<h1>failed</h1>");
        }
    }

    public void search(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String name = req.getParameter("name");
        User user1 = userService.selectByName(name);
        if(user1!=null){
            resp.getWriter().write("用户名:"+user1.getName()+"密码:"+user1.getPassword()+"年龄:"+user1.getAge());
        }
        else{
            resp.getWriter().write("失败!");
        }
    }

    public void delete(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {

        User user = new User();

        String name = req.getParameter("name");
        String password = req.getParameter("password");
        String age = req.getParameter("age");

        user.setName(name);
        user.setPassword(password);
        user.setAge(age);

        System.out.println(user);

        int count = userService.del(user);
        if(count>0){
            resp.sendRedirect("/index.jsp");
        }else {
            resp.getWriter().write("<h2>failed</h2>");
        }
    }

}
hellowServlet
package servlet;

import javax.servlet.*;
import java.io.IOException;

public class HelloServlet implements Servlet {

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        servletResponse.getWriter().write("hello,servelt");
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}
loginServlet

其中包含了登陆的方法,以及用“123”“123”的账号密码进行测试实验
在输入正确错误密码后各会弹出不同的页面。

package servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, IOException {
        String name = req.getParameter("name");
        String password = req.getParameter("password");

        if(name.equals("123")&&password.equals("123")){
            resp.getWriter().write("success!");
        }else{
            resp.getWriter().write("failed!");
        }
    }

}

Util包

BDutil
package util;

import java.sql.*;

public class DBUtil {
    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("连接数据库");
        //创建连接
        Connection  connection = DriverManager.getConnection("jdbc:mysql:localhost:3306/a?useSSL=false","root","123456");
        return  connection;
    }
    public static void closeAll(ResultSet rs, Statement stmt,Connection conn) throws SQLException {
        if(rs!=null) {
            rs.close();
        }
        if(stmt!=null){
            stmt.close();
        }
        if(conn!=null){
            conn.close();;
        }
    }
}

web包

Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>helloServlet</servlet-name>
        <servlet-class>servlet.HelloServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>helloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

    <servlet>
       <servlet-name>loginservlet</servlet-name>
        <servlet-class>servlet.LoginServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>loginservlet</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>AddServlet</servlet-name>
        <servlet-class>servlet.AddServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>AddServlet</servlet-name>
        <url-pattern>/add</url-pattern>
    </servlet-mapping>
</web-app>
add.jsp

注册页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>注册</title>
</head>
<body>
<form action="/add?method=save" method="post">
    name:<input name="name" type="text">
    password:<input name="password" type="password">
    age:<input name="age" type="text">
    <input type="submit" value="注册">
</form>

</body>
</html>

在这里插入图片描述

delete.jsp

删除页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>注销账户</title>
</head>
<h1>注销</h1>
<body>
<form action="/del?method=delete" method="post"> <!-- 对应AddServlet中的if语句-->
    name:<input name="name" type="text">
    password:<input name="password" type="password">
    age:<input name="age" type="text">
    <input type="submit" value="注销" >
</form>
</body>
</html>

在这里插入图片描述

serch.jsp

搜索页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>查找</title>
</head>
<body>
<form action="/add>method=search" method="post">
    <input name="name" placeholder="请输入需要查找的名字">
    <input type="submit" value="查找">

</form>
</body>
</html>

在这里插入图片描述

index.jsp

主页面(包含登陆功能)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>ASD</title>
  </head>
  <body>
  <form action="/login" method="post">
    name:<input name="name" type="text">
    password:<input name="password" type="password">
    <input type="submit" value="login">
  </form>
  <a href="add.jsp">跳转到注册页面</a>
  <a href="search.jsp">跳转到搜索页面</a>
  <a href="delete.jsp">跳转到删除页面</a>
  </body>
</html>

在这里插入图片描述

©️2020 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值