servlet+jsp用户登录实现

项目结构如下:

servlet+jsp用户登录实现_第1张图片

登录界面

servlet+jsp用户登录实现_第2张图片

登录成功

servlet+jsp用户登录实现_第3张图片

登录失败

servlet+jsp用户登录实现_第4张图片

user代码段

package bean;

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

import util.DB;

public class user {
	private String username;
	private String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public boolean Check(String username,String password)throws Exception{
		Connection conn=null;
		ResultSet rest=null;
		PreparedStatement prst=null;
		String sql="select * from user where username=?";
		boolean flag=false;
		try{
			conn=DB.getConn();
			prst=conn.prepareStatement(sql);
			prst.setString(1, username);
			rest=prst.executeQuery();
			if(rest.next()){
				if(password.equals(rest.getString("password"))){
					flag=true;
				}else{
					throw new Exception("密码不对哦");
				}
			}else{
				throw new Exception("用户名不存在");
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				rest.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				prst.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return flag;
	}
}

DB代码段

package util;

import java.sql.Connection;
import java.sql.DriverManager;

public class DB {
	public static Connection getConn(){
		Connection conn=null;
		try{
			String url="jdbc:mysql://localhost/student";
			String user="root";
			String password="root";
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection(url, user, password);
			if(conn!=null){
				System.out.println("数据库连接成功!");
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return conn;
	}
}

userServlet代码段

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 bean.user;

/**
 * Servlet implementation class userServlet
 */
@WebServlet(
		urlPatterns={"/userServlet"},name="userServlet" 
		)
public class userServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public userServlet() {
        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
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");//处理表单中文乱码
		user u=new user();
		String username,password;
		try{
			username=request.getParameter("username");
			password=request.getParameter("password");
			if(u.Check(username, password)){
				u.setUsername(username);
				u.setPassword(password);
				request.getSession().setAttribute("use", u);
				request.getRequestDispatcher("/Success.jsp").forward(request, response);
			}else{
				request.getRequestDispatcher("/fail.jsp").forward(request, response);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}

login.jsp代码段

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    用户登录
    
	
	
	    
	
	
	

  
  
  
    
用户名:
密码:

success.jsp代码段

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    登录成功
    
	
	
	    
	
	
	

  
  
  
    
用户名:
密码:

fail.jsp代码段

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    登录失败
    
	
	
	    
	
	
	

  
  
  
		登录失败!返回登录
  


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