Java web登录案例
程序员文章站
2024-03-20 17:45:22
...
java web登录案例
登录案例需求
用户输入用户名和密码,若在数据库中存在提示登录成功,若在数据库中不存在提示登录失败
浏览器:输入服务器中的登录页面的地址,服务器解析命令将页面返回到浏览器。
点击登录按钮向服务器的servlet发送请求
servlet获取用户名和密码。与数据库中的表做比较。(三层架构)
servlet中创建loginservice调用方法,向service发送请求service创建dao调用方法,service在向dao发送
user为null登录失败
user不为null登录成功:向前台写入信息登录成功
步骤分析
- 创建数据库和表结构
- 创建动态的web项目
- 导入必要的架包和工具类,同时创建动态的web项目的包结构util servlet service dao bean
- 导入前端的静态的资源文件
- 编写servlet获取用户名和密码判断信息是否存在
- 编写dao时创建QueryRunner对象
登录案例后端具体实现
包结构
utils包中
package com.dzu.utils;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DataSourceUtils {
private static DataSource ds=new ComboPooledDataSource();
//获取连接池
public static DataSource getDataSource(){
return ds;
}
//获取连接
public static Connection getConnection() throws SQLException{
return ds.getConnection();
}
}
dao包中
package com.dzu.dao;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import com.dzu.bean.User;
import com.dzu.utils.DataSourceUtils;
public class Logindao {
public User getUsernameAndPwd(String username, String password) throws SQLException {
// TODO Auto-generated method stub
//创建QueryRunner
QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
//编写sql
String sql="select * from user where username=? and password =?";
//执行sql
User user=qr.query(sql,new BeanHandler<User>(User.class),username,password);
return user;
}
}
service包中
package com.dzu.service;
import java.sql.SQLException;
import com.dzu.bean.User;
import com.dzu.dao.Logindao;
public class LoginService {
public User getUserByUsernameAndPwd(String username,String password) throws SQLException {
//创建logindao
Logindao ld=new Logindao();
return ld.getUsernameAndPwd(username,password);
}
}
servlet包中
package com.dzu.servlet;
import java.io.IOException;
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.dzu.bean.User;
import com.dzu.service.LoginService;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取请求的用户名和密码
try {
String username =request.getParameter("username");
String password =request.getParameter("password");
//处理响应的乱码问题
response.setHeader("content-type", "text/html;charset=utf-8");
//创建LoginService
LoginService ls=new LoginService();
User user=ls.getUserByUsernameAndPwd(username,password);
if(user==null) {
response.getWriter().println("error");
}else {
response.getWriter().println(user.getUsername()+"登录成功");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//调用service中的方法
//根据返回的对象进行判断
}
/**
* @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);
}
}
bean包中
package com.dzu.bean;
public class User {
private int id;
private String username;
private String password;
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;
}
}
数据库表
create table user(
id int primary key auto_increment,
username varchar(20),
password varchar(20)
);
insert into user values(null,'tom','123');
insert into user values(null,'jack','123');
推荐阅读