一个简单的实现servlet登录注册小功能

 

 

通过包名分类每一部分需要完成的任务。

 

 

StudentDao接口:

package cn.Dao;

import cn.pojo.Student;

public interface StudentDao {
    /**
     * 完成登录功能
     * @return
     */
    public Student selectStudent(String StudentName,String Password);

    /**
     * 注册功能
     * @return
     */
    public int AddStudent(String name,String pwd);
}

 

StudentDao的实现类:

package cn.Dao.impl;

import cn.Dao.BaseDao;
import cn.Dao.StudentDao;
import cn.pojo.Student;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StudentDaoImpl extends BaseDao implements StudentDao {
    @Override
    public Student selectStudent(String StudentName,String Password) {
        Connection conn=getConnection();
        PreparedStatement pre=null;
        ResultSet rs=null;
        Student student=null;
        String sql="select * from student where studentName=? and loginpwd=?";
        try {
            pre=conn.prepareStatement(sql);
            pre.setString(1,StudentName);
            pre.setString(2,Password);
            rs=pre.executeQuery();
            if(rs.next()){
                student=new Student();
                student.setStudentName(rs.getString("studentName"));
                student.setPassword(rs.getString("loginpwd"));

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(rs,pre,conn);
        }
        return student;
    }

    @Override
    public int   AddStudent(String name,String pwd) {
        Connection conn=getConnection();
        PreparedStatement pre=null;
        int count=0;
        String sql="insert into student values(0,?,?,now())";
        try {
            pre=conn.prepareStatement(sql);
            pre.setString(1,name);
            pre.setString(2,pwd);
            count=pre.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(null,pre,conn);
        }
        return count;
    }
}

 

BaseDao里面可以用jdbc也可以用jndi。

package cn.Dao;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.*;

public class BaseDao {
    private String driName="com.mysql.jdbc.Driver";
    private String url="jdbc:mysql://localhost:3306/firstbd";
    private String user="root";
    private String pwd="root";
    public Connection getConnection(){
        Context ctx;
        Connection conn=null;
        try {
//            Class.forName(driName);
//            conn= DriverManager.getConnection(url,user,pwd);
            ctx=new InitialContext();
            DataSource ds=(DataSource)ctx.lookup("java:/comp/env/jdbc/news");
            //在这里遇到的问题:我把后面的news写成了我数据库的名字了,其实写的是配置文件里面的name内容,记住了!!!!!!
            conn=ds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
        }
//        if (conn!=null){
//            System.out.println("连接成功");
//        }else {
//            System.out.println("连接失败");
//        }
        return conn;
    }
    public void closeAll(ResultSet rs, PreparedStatement pre, Connection conn){
        try {
            if (rs!=null){
                rs.close();
            }
            if (pre!=null){
                pre.close();
            }
            if (conn!=null){
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

 

Student实体类:

package cn.pojo;

public class Student {
    private String StudentName;
    private String Password;

    public Student() {
    }

    public Student(String studentName, String password) {
        StudentName = studentName;
        Password = password;
    }

    public String getStudentName() {
        return StudentName;
    }

    public void setStudentName(String studentName) {
        StudentName = studentName;
    }

    public String getPassword() {
        return Password;
    }

    public void setPassword(String password) {
        Password = password;
    }
}

 

学生服务类接口:

package cn.service;

import cn.pojo.Student;

public interface StudentService {
    /**
     * 学生登录
     * @return
     */
    public Student getStudent(String name,String pwd);

    /**
     * 学生注册
     * @return
     */
    public boolean AddStudent(String name,String pwd);
}

 

学生服务类接口的实现类:

package cn.service.impl;

import cn.Dao.impl.StudentDaoImpl;
import cn.pojo.Student;
import cn.service.StudentService;

import java.util.Scanner;

public class StudentServiceImpl implements StudentService {

    @Override
    public Student getStudent(String name,String pwd) {
        StudentDaoImpl studentDao=new StudentDaoImpl();
        Student student=studentDao.selectStudent(name,pwd);
        if (student!=null){
            System.out.println("登录成功");
        }else {
            System.out.println("登录失败");
        }
        return null;
    }

    @Override
    public boolean AddStudent(String name,String pwd) {
        StudentDaoImpl studentDao=new StudentDaoImpl();
        int count=studentDao.AddStudent(name,pwd);
        boolean flag=false;
        if (count>0){
            flag=true;
        }else {
            flag=false;
        }
        return flag;
    }
}

 

servlet类:用于登录功能

package cn.Servlet;

import cn.Dao.impl.StudentDaoImpl;
import cn.pojo.Student;
import cn.service.impl.StudentServiceImpl;

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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "TestServlet")
public class TestServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
//        PrintWriter out=response.getWriter();
////        HttpSession  session=request.getSession();
        String name=request.getParameter("StudentName");
        String Password=request.getParameter("Password");
//        StudentServiceImpl ssil=new StudentServiceImpl();
        StudentDaoImpl adil=new StudentDaoImpl();
        Student student=adil.selectStudent(name,Password);
        if (student!=null){
//            response.sendRedirect("Success.jsp");
            request.getRequestDispatcher("Success.jsp").forward(request,response);
        }else {
//            response.sendRedirect("Defeat.jsp");
            request.getRequestDispatcher("Defeat.jsp").forward(request,response);
        }
//        out.println("执行了 TestServlet...");
//        response.sendRedirect("Index.jsp");
//        out.flush();
//        out.close();
    }
}

 

servlet类:用于注册功能

package cn.Servlet;

import cn.Dao.StudentDao;
import cn.Dao.impl.StudentDaoImpl;
import cn.pojo.Student;

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(name = "TestServletTwo")
public class TestServletTwo extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out=response.getWriter();
        Student student=new Student();
        StudentDao studentDao=new StudentDaoImpl();
        String name=request.getParameter("StudentName");
        String Password=request.getParameter("Password");
        student.setStudentName(name);
        student.setPassword(Password);
        int count=studentDao.AddStudent(name,Password);
        out.print(count>0?"注册成功!":"注册失败!");
        out.flush();
        out.close();
    }
}

 

测试类:用于检验是否可以连接数据库,以及登录注册操作。

package cn;

import cn.Dao.BaseDao;
import cn.Dao.impl.StudentDaoImpl;
import cn.pojo.Student;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
//        System.out.println("输入姓名:");
//        Scanner input=new Scanner(System.in);
//        String name=input.next();
//        System.out.println("输入密码:");
//        String pwd=input.next();
          //检测是否可以注册成功
//        StudentDaoImpl sdil=new StudentDaoImpl();
//        int student=sdil.AddStudent(name,pwd);
//        System.out.println(student>0?"注册成功":"注册失败");
          //检测后台数据是否可以验证登录
//        StudentDaoImpl sdil=new StudentDaoImpl();
//        Student student=sdil.selectStudent(name,pwd);
//        if (student!=null){
//            System.out.println("登陆成功");
//        }else {
//            System.out.println("登录失败");
//        }
         //检查是否连接成功
        BaseDao b =new BaseDao();
        b.getConnection();
    }
}

 

登录注册界面

<%--
  Created by IntelliJ IDEA.
  User: lyy
  Date: 2020/9/23
  Time: 上午 11:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
<form action="/TestServletThree_war_exploded/TestServlet">
  姓名:<input type="text" name="StudentName"><br>
  密码:<input type="text"name="Password"><br>
  <input type="submit" value="登录">
</form>
  <a href="Register.jsp">注册</a>
  </body>
</html>
<%--http://localhost/TestServlet--%>
<%--http://localhost:80/TestServletThree_war_exploded/Index.jsp--%>

 

登录成功界面

<%--
  Created by IntelliJ IDEA.
  User: lyy
  Date: 2020/9/23
  Time: 上午 11:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h3>登录成功</h3>
</body>
</html>

 

登录失败界面

<%--
  Created by IntelliJ IDEA.
  User: lyy
  Date: 2020/9/23
  Time: 下午 08:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h3>登录失败</h3>
</body>
</html>

 

注册界面

<%--
  Created by IntelliJ IDEA.
  User: lyy
  Date: 2020/9/23
  Time: 下午 04:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="/TestServletThree_war_exploded/TestServletTwo">
    用户名:<input type="text" name="StudentName"><br>
    密码:<input type="text" name="Password"><br>
    <input type="submit" value="注册">
</form>
</body>
</html>

posted @ 2020-09-25 20:32  ..L  阅读(565)  评论(0编辑  收藏  举报