Java web实现登录注册功能

前台页面

Insert title here

用户名:

密码:


注册页面

用户名: 

密码: 

年龄: 

生日: 

  email: 

  tel: 

后台代码

public class Dpcdutil {

private static DataSource datasource;

// 获得配置文件的 输入流

// 放到properties 里面去

public static Properties getconfig(String path) {//"src/dbcp.properties"  | dbcp.properties

try {

// InputStream is=new FileInputStream(path);

//配置文件从项目的根路径找起

  // 使用类加载器 加载配置文件

InputStream is =Dpcdutil.class.getClassLoader().getResourceAsStream(path);

Properties p=new Properties();

p.load(is);

return p;

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return null;

}

static {

//dbcp连接池工程要根据 配置文件Properties  动态链接数据库

try {

datasource= BasicDataSourceFactory.createDataSource(getconfig("dbcp.properties"));

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static DataSource getDatasource() {

return datasource;

}

public static Connection getcon() {

try {

return datasource.getConnection();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return null;

}

}


public class Userinformation implements Serializable{

/**

*

*/

private static final long serialVersionUID = 1L;

private Integer id;

private String name;

private String password;

private Integer age;

private Date birthday;

private String email;

private String tel;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

public Date getBirthday() {

return birthday;

}

public void setBirthday(Date birthday) {

this.birthday = birthday;

}

public String getEmail() {

return email;

}

public void setEmail(String email) {

this.email = email;

}

public String getTel() {

return tel;

}

public void setTel(String tel) {

this.tel = tel;

}

@Override

public String toString() {

return "Userinfomation [id=" + id + ", name=" + name + ", password=" + password + ", age=" + age + ", birthday="

+ birthday + ", email=" + email + ", tel=" + tel + "]";

}

}


public class UserRegisterService {

public  boolean RegisterUserinfo(Userinformation userinfo) {

UserRegisterdao u= new UserRegisterdao();

int n=u.insertUserinfo(userinfo);

if(n>0) {

return true;

}else {

return false;

}

}

public boolean RegisterUserinfo2(String name, String password, String age, String birthday, String email,

String tel) {

// TODO Auto-generated method stub

Userinformation userinfo=new Userinformation();

userinfo.setName(name);

userinfo.setPassword(password);

// String--->Integer

userinfo.setAge(new Integer(age));

// String---->date  yyyy-MM-dd

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

try {

userinfo.setBirthday(sdf.parse(birthday));

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

userinfo.setEmail(email);

userinfo.setTel(tel);

UserRegisterdao u= new UserRegisterdao();

int n=u.insertUserinfo2(userinfo);

if(n>0) {

return true;

}else {

return false;

}

}

}


public class UserLoginService {

private static UserLoginService us=new UserLoginService();

private UserLoginService(){

}

public static UserLoginService getInstance(){

return us;

}

// 登录服务  只要传loginuserinfo 就可以判断 能否登录

public boolean login(LoginInfo loginuserinfo) {

//UserLoginDao uld= new UserLoginDao();

UserLoginDao uld=UserLoginDao.getInstance();

List list=uld.login(loginuserinfo);

if(list==null) {

return false;

}else if(list.size()==1) {

return true;

}else if(list.size()<1){

System.err.println("UserLoginService login() 验证登录失败");

return false;

}else {

System.err.println("UserLoginService login() 出现多用户同名 同密码 问题!");

return true;

}

}

}


//javabean 实体bean

public class UserInfomation implements Serializable {

private static final long serialVersionUID = 1L;

private Integer id;

private String name;

private String password;

private Integer age;

private Date birthday;

private String email;

private String tel;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

public Date getBirthday() {

return birthday;

}

public void setBirthday(Date birthday) {

this.birthday = birthday;

}

public String getEmail() {

return email;

}

public void setEmail(String email) {

this.email = email;

}

public String getTel() {

return tel;

}

public void setTel(String tel) {

this.tel = tel;

}

@Override

public String toString() {

return "UserInfomation [id=" + id + ", name=" + name + ", password=" + password + ", age=" + age +

", birthday=" + birthday + ", email=" + email + ", tel=" + tel + "]";

}

}

public class LoginInfo implements Serializable {

private static final long serialVersionUID = 1L;

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 class UserLoginDao {

private static UserLoginDao u=new UserLoginDao();

private UserLoginDao() {

}

public  static  UserLoginDao getInstance() {

return u;

}

public List login(LoginInfo loginuserinfo) {

DataSource datasource= Dpcdutil.getDatasource();

QueryRunner qr=new QueryRunner(datasource);

List listusr=null;

try {

listusr=qr.query("select id,name,password,age,birthday,email,tel from  userinformation where name=? and password=?", new BeanListHandler(UserInfomation.class), loginuserinfo.getUsername(),loginuserinfo.getPassword());

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return listusr;

}

}


public class UserRegisterdao {

public  int insertUserinfo(Userinformation userinfo) {

Connection con=Dpcdutil.getcon();

try {

PreparedStatement pst=con.prepareStatement("insert into userinformation(name,password,age,birthday,email,tel) values(?,?,?,?,?,?)");

//当mysql数据库设置了主键自增 ,可以不用人为插入 主键值

// pst.setObject(1,userinfo.getId() );

pst.setObject(1, userinfo.getName());

pst.setObject(2, userinfo.getPassword());

pst.setObject(3, userinfo.getAge());

pst.setObject(4, userinfo.getBirthday());

pst.setObject(5, userinfo.getEmail());

pst.setObject(6, userinfo.getTel());

int n=pst.executeUpdate();

return n;

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return 0;

}

public  int insertUserinfo2(Userinformation userinfo){

DataSource datasource= Dpcdutil.getDatasource();

QueryRunner qr=new QueryRunner(datasource);

try {

int n=qr.update("insert into userinformation(name,password,age,birthday,email,tel) values(?,?,?,?,?,?)", userinfo.getName(),userinfo.getPassword(),userinfo.getAge(),userinfo.getBirthday(),userinfo.getEmail(),userinfo.getTel());

return n;

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

  return 0;

}

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

//response.getWriter().append("Served at: ").append(request.getContextPath());

String name=request.getParameter("username");

String password=request.getParameter("password");

String age=request.getParameter("age");

String birthday=request.getParameter("birthday");

String email=request.getParameter("email");

String tel=request.getParameter("tel");

UserRegisterService urs=new UserRegisterService();

boolean n=urs.RegisterUserinfo2(name, password, age, birthday, email, tel);

if(n) {

System.out.println("注册成功");

response.sendRedirect("denglu.html");

}else {

System.out.println("注册失败");

response.sendRedirect("chuce.html");

}

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

//response.getWriter().append("Served at: ").append(request.getContextPath());

String name=null;

String password=null;

//System.out.println("请输入用户名");

name=request.getParameter("username");

//System.out.println("请输入密码");

password=request.getParameter("password");

LoginInfo loginuserinfo=new LoginInfo();

loginuserinfo.setUsername(name);

loginuserinfo.setPassword(password);

//找登录服务 验证是否登录成功

UserLoginService us=UserLoginService.getInstance();

boolean n=us.login(loginuserinfo);

if(n) {

System.out.println("登录成功 !");

request.getSession().setAttribute("username", name);

response.sendRedirect("main.jsp");

}else {

System.out.println("用户名或密码错误");

response.sendRedirect("denglu.html");

}

}


public class UserccComtroller {

// public static void main(String[] args) {

// // TODO Auto-generated method stub

//// 注册

// //registerUserinfo();

// login();

// }

public static void method1(String[] args) {

for(String n : args) {

System.out.println(n);

}

}

@Test

public  void  login() {

Scanner sc=new Scanner(System.in);

System.out.println("欢迎来到登录功能模块!");

String name=null;

String password=null;

System.out.println("请输入用户名");

name=sc.next();

System.out.println("请输入密码");

password=sc.next();

LoginInfo loginuserinfo=new LoginInfo();

loginuserinfo.setUsername(name);

loginuserinfo.setPassword(password);

//找登录服务 验证是否登录成功

//UserLoginService us=new UserLoginService();

UserLoginService us=UserLoginService.getInstance();

boolean n=us.login(loginuserinfo);

if(n) {

System.out.println("登录成功 !");

}else {

System.out.println("用户名或密码错误");

}

}

public static void registerUserinfo2() {

Scanner sc=new Scanner(System.in);

String name=null;

String password=null;

String age=null;

String birthday=null;

String email=null;

String tel=null;

System.out.println("欢迎来到注册系统!");

System.out.println("请输入用户名");

name=sc.next();

System.out.println("请输入密码");

password=sc.next();

System.out.println("请输入年龄");

age=sc.next();

System.out.println("请输入生日 格式为  年-月-日 例如 1987-12-13");

birthday=sc.next();

System.out.println("请输入email");

email=sc.next();

System.out.println("请输入电话号");

tel=sc.next();

Userinformation userinfo= new Userinformation();

userinfo.setName(name);

userinfo.setPassword(password);

// String--->Integer

userinfo.setAge(new Integer(age));

// String---->date  yyyy-MM-dd

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

try {

userinfo.setBirthday(sdf.parse(birthday));

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

userinfo.setEmail(email);

userinfo.setTel(tel);

UserRegisterService urs=new UserRegisterService();

urs.RegisterUserinfo(userinfo);

}

// 注册系统

public static void registerUserinfo() {

Scanner sc=new Scanner(System.in);

String name=null;

String password=null;

String age=null;

String birthday=null;

String email=null;

String tel=null;

System.out.println("欢迎来到注册系统!");

System.out.println("请输入用户名");

name=sc.next();

System.out.println("请输入密码");

password=sc.next();

System.out.println("请输入年龄");

age=sc.next();

System.out.println("请输入生日 格式为  年-月-日 例如 1987-12-13");

birthday=sc.next();

System.out.println("请输入email");

email=sc.next();

System.out.println("请输入电话号");

tel=sc.next();

UserRegisterService urs=new UserRegisterService();

boolean n=urs.RegisterUserinfo2(name, password, age, birthday, email, tel);

if(n) {

System.out.println("注册成功");

}else {

System.out.println("注册失败");

}

}

}

你可能感兴趣的:(Java web实现登录注册功能)