servlet登录验证并返回错误信息

index.jsp(登录界面)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    用户登录
	
	
	    
	
	
	
	
    
  
  	
用户名:   ${requestScope.namemsg}${requestScope.nameError}
密码:   ${requestScope.pwdError}${requestScope.pwdmsg}
 

loginServlet(登录验证)

package com.dwx.servlet;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
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 com.dwx.bean.user;
import com.dwx.bean.userDao;

/**
 * 用户登录验证
 */
@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public loginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		//判断用户名、密码是否为空
		if(username==""||username.length()==0){
			request.setAttribute("namemsg","用户名为空!");
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}
		if(password==""||password.length()==0){
			request.setAttribute("pwdmsg","密码为空!");
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}
		//判断用户名、密码是否正确
		ResultSet rst=userDao.selectUser(username, password);
		try {
			if(rst.next()){
				if(password.equals(rst.getString("password"))){
					request.setAttribute("username",username);
					request.getRequestDispatcher("items.jsp").forward(request, response);
				}else{
					request.setAttribute("pwdError","密码不正确!");
					request.getRequestDispatcher("index.jsp").forward(request, response);
				}
			}else{
				request.setAttribute("nameError","用户名不存在!");
				request.getRequestDispatcher("index.jsp").forward(request, response);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

userDao.java(查询用户)

package com.dwx.bean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.dwx.util.dao;
public class userDao {
	public static ResultSet selectUser(String username,String password){
		Connection conn=dao.getConnection();
		String sql="select * from user where username=?";
		ResultSet rst=null;
		try{
			PreparedStatement prst=conn.prepareStatement(sql);
			prst.setString(1, username);
			rst=prst.executeQuery();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return rst;
	}
}

dao.java(建立数据库连接)

package com.dwx.util;
import java.sql.Connection;
import java.sql.DriverManager;
public class dao {
	public static Connection getConnection(){
		Connection conn=null;
		try{
			Class.forName("com.mysql.jdbc.Driver");		//加载驱动
			String url="jdbc:mysql://localhost:3306/db_item";
			String username="root";
			String password="root";
			//创建数据库连接
			conn=DriverManager.getConnection(url,username,password);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return conn;
	}
}

效果演示:

1、用户名为空

servlet登录验证并返回错误信息_第1张图片

2、密码为空

servlet登录验证并返回错误信息_第2张图片

3、用户名不存在

servlet登录验证并返回错误信息_第3张图片

4、密码不正确

servlet登录验证并返回错误信息_第4张图片

5、登录成功

servlet登录验证并返回错误信息_第5张图片


你可能感兴趣的:(java,web)