jsp javabean servlet登陆功能详细描述

来源:互联网 发布:电脑桌面 知乎 编辑:程序博客网 时间:2024/05/22 12:34

bean

package com.yinhe.bean;
public class Admin {
 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;
}  
}

dao

package com.yinhe.dao;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.taglibs.standard.tag.common.sql.DataSourceUtil;
import com.yinhe.bean.Admin;
import com.yinhe.util.DataSourceUtils;
public class AdminDao {
/**
* 查询admin对象
* @param username 用户名
* @param password 密码
* @return 
* @throws SQLException
*/
 public Admin findadmin(String username,String password) throws SQLException{

//用数据库连接池初始化runner对象
QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
//查数据库
String sql = "select * from admin where userName=? and password=?";
return qr.query(sql, new BeanHandler<Admin>(Admin.class),username,password);
 
 }
}

service

package com.yinhe.service;
import java.sql.SQLException;
import com.yinhe.bean.Admin;
import com.yinhe.dao.AdminDao;
public class Adminservice {
private AdminDao ad=new AdminDao();
public Admin adminLogin(String username,String password){
Admin admin=null;
try {
return ad.findadmin(username, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}

servlet

package com.yinhe.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yinhe.bean.Admin;
import com.yinhe.service.Adminservice;
public class AdminServlet extends BaseServlet{
private Adminservice as=new Adminservice();
public void login(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String mes="";
//获取前台表单中写的内容
String username=req.getParameter("username");
String password=req.getParameter("password");
//封装admin
Admin admin=as.adminLogin(username, password);
//判断
if(admin==null){
mes="账号或密码错误";
//将错误信息塞给前台显示
req.setAttribute("mes", mes);
//跳转页面 转发不需要加项目名
req.getRequestDispatcher("/admin/index.jsp").forward(req, resp);
}else{
//塞给前台显示
req.setAttribute("username", username);
req.getRequestDispatcher("/admin/home.jsp").forward(req, resp);
}
}
}

base类

package com.yinhe.servlet;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BaseServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
String method = req.getParameter("method");
//谁继承baseservlet this就是谁的对象
Class clazz = this.getClass();
// 找到method指定的方法
try {
Method m = clazz.getMethod(method, HttpServletRequest.class,
HttpServletResponse.class);
m.invoke(this, req, resp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}