servlet+jsp实现简易登录+注册

servlet+jsp实现简易登录+注册

实现代码

数据库连接:

GetConnection.java

GetConnection.java
package util;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import javaBean.User;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class GetConnection {
	public static Connection getConn(){
		Connection conn = null;
		try{
				//连接数据库
				Class.forName("com.mysql.jdbc.Driver");
				String url="jdbc:mysql://localhost:3306/mysql";
				String user="root";
				String password="root";
				conn=(Connection) DriverManager.getConnection(url, user, password);
		}catch(Exception e){
				e.printStackTrace();
		}
		return conn;
		}
	}
}

登录页面
login.jsp 内容相关的部分代码

身 份 证 号 码: 密 码:

相关处理

P_LoginServlet.java

package servlet;

import java.io.IOException;
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 dao.UserDao;
import dao.UserDaoImpl;
import javaBean.User;

/**
 * Servlet implementation class P_LoginServlet
 */
@WebServlet("/P_LoginServlet")
public class P_LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public P_LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		//获取用户登录输入的用户名以及密码
		String idc = request.getParameter("idc");
		String password = request.getParameter("password");
        System.out.println(idc);
		System.out.println(password);
		//通过数据库验证用户名以及密码的正确性	
		UserDao user=new UserDaoImpl();
        User userLogin = user.login(idc,password);
        if(userLogin==null) {
            System.out.println("数据库中不存在用户名密码!");
        }
        else {
            System.out.println("idc:"+userLogin.getIdc()+"password"+userLogin.getPassword());
            response.sendRedirect("Patient.jsp");
        }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

UserDao.java

package dao;

import javaBean.User;

public interface UserDao {
	public User login(String idc,String password);
}

UserDaoImpl.java

package dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

import javaBean.User;
import util.GetConnection;

public class UserDaoImpl implements UserDao{
	public User login(String idc,String password) {
		Connection conn = null;
		Statement stm = null;
		try {
			conn = new GetConnection().getConn();
			stm = (Statement) conn.createStatement();
			User user=new User(idc, password);
			String sql="select * from p_user where idc='" + idc + "'";
			ResultSet rs = stm.executeQuery(sql);
			// 查看是否合法
			while (rs.next()) {
				if (idc.equals(rs.getString("idc"))&& password.equals(rs.getString("password")))
				{
					System.out.println("登录成功");
					return user;
				}
			}
			rs.close();
			return null;
			}catch (SQLException e) {
				System.out.println("sql语句错误!");
				}
		return null;
      
    }

}

web.xml相关部分

  
    P_LoginServlet
    servlet.P_LoginServlet
  
  
    P_LoginServlet
    /AppiontmentSystem/P_LoginServlet
  

你可能感兴趣的:(servlet,jsp)