javaweb--demo小项目

。。。。。。。。数据库准备:表准备

 

 

 java数据库配置连接《JDBC准备》:

 

 

 Druid工具类准备:

 

复制代码
package com.gton.servlet.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.sql.*;
import java.util.Properties;

/**
 * @program: Jdbc-start
 * @description: Druid工具类
 * @author: GuoTong
 * @create: 2020-09-01 15:23
 **/
public class DruidUtils {

    //初始化连接池
    static DataSource dataSource;

    static {
        //创建druid连接池
        try {
            Properties properties = new Properties();
            properties.load(DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties"));

            dataSource = DruidDataSourceFactory.createDataSource(properties);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static DataSource getDataSource(){
        return dataSource;
    }
    public static Connection getConnection(){
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //关闭资源
    public static void close(Connection conn, Statement stmt) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Connection conn, PreparedStatement stmt) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Connection conn, Statement stmt, ResultSet rs) {

        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        close(conn, stmt);
    }

    public static void close(Connection conn, PreparedStatement stmt, ResultSet rs) {

        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        close(conn, stmt);
    }
}
复制代码

 

 

 

DB.properties准备:

 

 

 。。。。。实体类:

复制代码
package com.gton.servlet.entity;

/**
 * @program: javaweb-tomcat
 * @description:
 * @author: GuoTong
 * @create: 2020-09-14 16:09
 **/
public class Student {
    private int id;
    private String name;
    private  String pwd;

    public Student() {
    }

    public Student(int id, String name, String pwd) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pwd='" + pwd + '\'' +
                '}';
    }

    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 getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}
复制代码

DAO层开发:

 

复制代码
package com.gton.servlet.dao;

import com.gton.servlet.entity.Student;
import com.gton.servlet.utils.DruidUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: javaweb-tomcat
 * @description:
 * @author: GuoTong
 * @create: 2020-09-14 16:11
 **/
public class StudentDao {

    public List<Student>  findByPwdAndName(String name, String pwd) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Student> list=new ArrayList<>();
        try {
            //1.获取连接
            connection = DruidUtils.getConnection();
            //2 第二部sql
            String sql = "select * from student where name =? and pwd =?";
            //获取sql操作对象
            preparedStatement = connection.prepareStatement(sql);
            //设置参数
            preparedStatement.setString(1, name);
            preparedStatement.setString(2, pwd);
            //执行sql
            resultSet = preparedStatement.executeQuery();
            //接收参数
            while (resultSet.next()) {
                Student student = new Student();
                student.setId(resultSet.getInt("id"));
                student.setName(resultSet.getString("name"));
                student.setPwd(resultSet.getString("pwd"));
                list.add(student);
            }
            if (list.size()>0){
                System.out.println("查询成功,登录正确");
                return  list;
            }else {
                System.out.println("检查账号密码,登录失败");
                return null;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close(connection, preparedStatement, resultSet);
        }
        return null;
    }

}
复制代码

 

Service层开发:

 

复制代码
package com.gton.servlet.service;

import com.gton.servlet.dao.StudentDao;
import com.gton.servlet.entity.Student;

import java.util.List;

/**
 * @program: javaweb-tomcat
 * @description:
 * @author: GuoTong
 * @create: 2020-09-14 16:25
 **/
public class StudentService {
    private StudentDao studentDao =new StudentDao();

    public List<Student> findByPwdAndName(String name,String pwd){
        return studentDao.findByPwdAndName(name,pwd);
    }
}
复制代码

 

Servlet开发:

 

 

 

 

复制代码
package com.gton.servlet;

import com.gton.servlet.entity.Student;
import com.gton.servlet.service.StudentService;
import org.omg.CORBA.Request;

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.time.chrono.IsoChronology;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @program: javaweb-tomcat
 * @description:
 * @author: GuoTong
 * @create: 2020-09-14 16:00
 **/
@WebServlet(urlPatterns = {"/login"})
public class LoginServlet extends HttpServlet {
    /**
     *
     *response 设置状态码:setStatus(int sc)
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("UTF-8");
        req.setCharacterEncoding("UTF-8");

        String name =req.getParameter("user");
        String pwd =req.getParameter("pwd");
        if (name!=null && pwd !=null){
            List<Student> byPwdAndName = new StudentService().findByPwdAndName(name, pwd);
            if (byPwdAndName.size()>=0){
                req.setAttribute("name",name);
                req.getRequestDispatcher("/success").forward(req,resp);
            }else {
                req.setAttribute("name","您输入的用户名有问题");
                req.getRequestDispatcher("/fail").forward(req,resp);
            }
        }else {
            System.out.println("错误,"+name+":"+pwd);
        }


    }
}
复制代码

 

Servlet开发:

 

 

复制代码
package com.gton.servlet;

import com.gton.servlet.entity.Student;
import com.gton.servlet.service.StudentService;
import org.omg.CORBA.Request;

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.time.chrono.IsoChronology;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @program: javaweb-tomcat
 * @description:
 * @author: GuoTong
 * @create: 2020-09-14 16:00
 **/
@WebServlet(urlPatterns = {"/login"})
public class LoginServlet extends HttpServlet {
    /**
     *
     *response 设置状态码:setStatus(int sc)
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("UTF-8");
        req.setCharacterEncoding("UTF-8");

        String name =req.getParameter("user");
        String pwd =req.getParameter("pwd");
        if (name!=null && pwd !=null){
            List<Student> byPwdAndName = new StudentService().findByPwdAndName(name, pwd);
            if (byPwdAndName.size()>=0){
                req.setAttribute("name",byPwdAndName.get(0).getName());
                req.getRequestDispatcher("/success").forward(req,resp);
            }else {
                req.setAttribute("name","您输入的用户名或者密码有问题");
                req.getRequestDispatcher("/fail").forward(req,resp);
            }
        }else {
            System.out.println("错误,"+name+":"+pwd);
        }


    }
}
复制代码

 

成功的Servlet:

复制代码
package com.gton.servlet;

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;

/**
 * @program: javaweb-tomcat
 * @description:
 * @author: GuoTong
 * @create: 2020-09-14 16:02
 **/

@WebServlet(urlPatterns = {"/success"})
public class SuccessServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("UTF-8");
        req.setCharacterEncoding("UTF-8");

        String name = (String) req.getAttribute("name");

        PrintWriter writer = resp.getWriter();
        writer.print(name + "欢迎您");
        writer.flush();
        writer.close();

    }
}
复制代码

失败的servlet:

复制代码
package com.gton.servlet;

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;

/**
 * @program: javaweb-tomcat
 * @description: ${description}
 * @author: GuoTong
 * @create: 2020-09-14 16:05
 **/
@WebServlet(urlPatterns = {"/fail"})
public class FialServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");

        String name = (String) request.getAttribute("name");

        PrintWriter writer = response.getWriter();
        writer.print(name);
        writer.flush();
        writer.close();
    }
}
复制代码

前端的入口页面:

 

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/login" method="post">
    <label for="isuser">用户名</label>
    <input type="text" name="user" id="isuser"/>
    <br>
    <label for="isuser">密码</label>
    <input type="password" name="pwd" id="ispwd"/>
    <br>
    <input type="submit" value="提交">
</form>

</body>
</html>
复制代码

 

posted on   白嫖老郭  阅读(743)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示