JavaWeb-用户登录案例

用户登录需要和数据库进行匹配.

这是数据库中的表.

id userame password email
1 Tom 1234 [email protected]
2 Jerry 1234 [email protected]

 

 

登录页面




login.html







	
Username:
Password:

 

JavaWeb-用户登录案例_第1张图片

web.xml



  WEB13
  
    driver
    com.mysql.jdbc.Driver
  
  
    abc
    com.itheima.servlet.QuickStratServlet
    
      url
      jdbc:mysql:///mydb
    
    3
  
  
    abc
    /quickStratServlet
  
  
    1.html
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
  
    QuickStartServlet2
    com.itheima.servlet.QuickStartServlet2
  
  
    QuickStartServlet2
    /quickStartServlet2
  
  
    
    LoginServlet
    LoginServlet
    com.itheima.login.LoginServlet
  
  
    LoginServlet
    /login
  
  
    
    ContextServlet
    ContextServlet
    com.itheima.context.ContextServlet
  
  
    ContextServlet
    /context
  
  
    
    ContextServlet2
    ContextServlet2
    com.itheima.context.ContextServlet2
  
  
    ContextServlet2
    /context2
  
package com.web.login;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.web.domain.User;
import com.web.utils.DataSourceUtils;

public class LoginServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获得用户名和密码
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		// 从数据库中验证用户名和密码是否匹配
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select * from user where username=? and password=?";
		User user = null;
		try {
			user = runner.query(sql, new BeanHandler(User.class),
					username, password);
		} catch (SQLException e) {

			e.printStackTrace();
		}
		// 根据返回的结果给出讯息
		if (user != null) {
			// 登录成功
			response.getWriter().write("Login success");
			response.getWriter().write(user.toString());
		} else {
			// 登录失败
			response.getWriter().write("Login failure");
		}

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}
package com.web.domain;

public class User {
	private int id;
	private String username;
	private String password;
	private String email;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	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 String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password="
				+ password + ", email=" + email + "]";
	}

}


	
		root
		root
		com.mysql.jdbc.Driver
		jdbc:mysql:///web01
	
 

 

你可能感兴趣的:(JavaWeb)