用户登录界面(1.8版本--网站框架改进)
Posted ksyoon
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用户登录界面(1.8版本--网站框架改进)相关的知识,希望对你有一定的参考价值。
1 CREATE DATABASE db_user CHARACTER SET utf8; 2 3 CREATE TABLE users ( 4 userid TINYINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, 5 username varchar(20), 6 passwd varchar(20), 7 email varchar(30), 8 priority int 9 ); 10 11 DROP TABLE users; 12 13 SELECT * FROM users; 14 15 INSERT INTO users(username,passwd,email,priority) VALUES(‘admin‘,‘admin‘,‘[email protected]‘,1); 16 INSERT INTO users(username,passwd,email,priority) VALUES(‘shunping‘,‘shunping‘,‘[email protected]‘,1); 17 INSERT INTO users(username,passwd,email,priority) VALUES(‘tester1‘,‘tester2‘,‘[email protected]‘,5); 18 INSERT INTO users(username,passwd,email,priority) VALUES(‘tester2‘,‘tester2‘,‘[email protected]‘,5); 19 INSERT INTO users(username,passwd,email,priority) VALUES(‘tester3‘,‘tester3‘,‘[email protected]‘,5); 20 INSERT INTO users(username,passwd,email,priority) VALUES(‘tester4‘,‘tester4‘,‘[email protected]‘,5); 21 INSERT INTO users(username,passwd,email,priority) VALUES(‘tester5‘,‘tester5‘,‘[email protected]‘,5); 22 INSERT INTO users(username,passwd,email,priority) VALUES(‘tester6‘,‘tester6‘,‘[email protected]‘,5); 23 INSERT INTO users(username,passwd,email,priority) VALUES(‘tester7‘,‘tester7‘,‘[email protected]‘,5); 24 INSERT INTO users(username,passwd,email,priority) VALUES(‘tester8‘,‘tester8‘,‘[email protected]‘,5); 25 26 //快速增加记录 27 INSERT INTO users(username,passwd,email,priority) SELECT username,passwd,email,priority FROM users; 28 29 SELECT COUNT(*) FROM users;
1 package com.tsinghua; 2 3 import javax.servlet.http.*; 4 import java.io.*; 5 6 public class Login3 extends HttpServlet { 7 public void doGet(HttpServletRequest req,HttpServletResponse res) { 8 try { 9 res.setContentType("text/html;charset=utf8"); 10 PrintWriter pw = res.getWriter(); 11 String info1 = req.getParameter("info1"); 12 String info3 = req.getParameter("info3"); 13 pw.println("<html>"); 14 pw.println("<body>"); 15 //用户尚未登录提醒 16 if(info1!=null) { 17 pw.println("<span>您还没有登录</span>"); 18 } 19 if(info3 != null) { 20 pw.println("<span>您的用户名或密码错误</span>"); 21 } 22 pw.println("<h1>用户登录</h1>"); 23 pw.println("<form action=loginCl3>"); 24 pw.println("用户名:<input type=text name=username><br/>"); 25 pw.println("密码:<input type=pasword name=passwd><br/>"); 26 pw.println("<input type=checkbox name=checked value=2>两周之内不需要登录<br/>"); 27 pw.println("<input type=submit value=login>"); 28 pw.println("</form>"); 29 pw.println("</body>"); 30 pw.println("</html>"); 31 32 }catch(Exception ex) { 33 ex.printStackTrace(); 34 } 35 } 36 public void doPost(HttpServletRequest req,HttpServletResponse res) { 37 this.doGet(req,res); 38 } 39 }
1 package com.tsinghua; 2 3 import java.sql.*; 4 5 public class ConnDB { 6 private Connection con = null; 7 public Connection getConn() { 8 try { 9 Class.forName("com.mysql.jdbc.Driver"); 10 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_user","root",""); 11 }catch(Exception ex) { 12 ex.printStackTrace(); 13 } 14 return con; 15 } 16 }
1 //与表users一一映射 2 package com.tsinghua; 3 4 import java.sql.*; 5 6 public class UserBean { 7 private int userId; 8 private String userName; 9 private String passwd; 10 private String email; 11 private int priority; 12 public void setUserId(int userId) { 13 this.userId = userId; 14 } 15 public int getUserId() { 16 return this.userId; 17 } 18 public void setUserName(String userName) { 19 this.userName = userName; 20 } 21 public String getUserName() { 22 return this.userName; 23 } 24 public void setPasswd(String passwd) { 25 this.passwd = passwd; 26 } 27 public String getPasswd() { 28 return this.passwd; 29 } 30 public void setEmail(String email) { 31 this.email = email; 32 } 33 public String getEmail() { 34 return this.email; 35 } 36 public void setPriority(int priority) { 37 this.priority = priority; 38 } 39 public int getPriority() { 40 return this.priority; 41 } 42 }
1 //处理users表--->操作UserBean 2 //业务逻辑处理 3 4 package com.tsinghua; 5 6 import java.sql.*; 7 import java.util.*; 8 import com.tsinghua.*; 9 10 public class UserBeanCl { 11 private Connection ct=null; 12 private PreparedStatement ps=null; 13 private ResultSet rs=null; 14 private int pageCount=0; 15 16 public int getPageCount() { 17 return this.pageCount; 18 } 19 20 public ArrayList getResultByPage(int pageNow,int pageSize) { 21 22 //将数据库中记录分页 23 ArrayList arraylist = new ArrayList(); 24 try { 25 int rowCount=0; 26 ConnDB cd = new ConnDB(); 27 ct = cd.getConn(); 28 ps = ct.prepareStatement("select count(*) from users"); 29 rs = ps.executeQuery(); 30 if(rs.next()) { 31 rowCount = rs.getInt(1); 32 } 33 //计算pageCount 34 if(rowCount % pageSize == 0) { 35 pageCount = rowCount / pageSize; 36 }else { 37 pageCount = rowCount / pageSize + 1; 38 } 39 40 ps = ct.prepareStatement("select * from users limit ?,?"); 41 ps.setInt(1,(pageNow-1)*pageSize); 42 ps.setInt(2,pageSize); 43 rs = ps.executeQuery(); 44 if(rs.next()) { 45 UserBean ub = new UserBean(); 46 ub.setUserId(rs.getInt(1)); 47 ub.setUserName(rs.getString(2)); 48 ub.setPasswd(rs.getString(3)); 49 ub.setEmail(rs.getString(4)); 50 ub.setPriority(rs.getInt(5)); 51 arraylist.add(ub); 52 } 53 54 }catch(Exception ex) { 55 ex.printStackTrace(); 56 }finally { 57 this.close(); 58 } 59 return arraylist; 60 } 61 62 //验证用户 63 public boolean checkUser(String user,String pass) { 64 boolean bool = false; 65 try { 66 ConnDB cd = new ConnDB(); 67 ct = cd.getConn(); 68 ps = ct.prepareStatement("select passwd from users where username=?"); 69 ps.setString(1,user); 70 rs=ps.executeQuery(); 71 if(rs.next()) { 72 String sPasswd = rs.getString(1); 73 if(sPasswd.equals(pass)) { 74 bool=true; 75 } 76 } 77 }catch(Exception ex) { 78 ex.printStackTrace(); 79 }finally { 80 this.close(); 81 } 82 return bool; 83 } 84 public void close() { 85 try { 86 if(rs!=null) { 87 rs.close(); 88 rs=null; 89 } 90 if(ps!=null) { 91 ps.close(); 92 ps=null; 93 } 94 if(ct!=null) { 95 ct.close(); 96 ct=null; 97 } 98 }catch(Exception ex) { 99 ex.printStackTrace(); 100 } 101 } 102 103 }
1 package com.tsinghua; 2 3 import javax.servlet.http.*; 4 import java.io.*; 5 import java.sql.*; 6 import com.tsinghua.*; 7 8 public class LoginCl3 extends HttpServlet { 9 public void doGet(HttpServletRequest req,HttpServletResponse res) { 10 11 try { 12 String user = req.getParameter("username"); 13 String pass = req.getParameter("passwd"); 14 15 //检验用户 16 UserBeanCl ubc = new UserBeanCl(); 17 if(ubc.checkUser(user,pass)){ 18 19 String checked = req.getParameter("checked"); 20 if(checked!=null) { 21 22 //创建Cookie 23 Cookie username = new Cookie("myName",user); 24 Cookie passwd = new Cookie("myPasswd",pass); 25 //设置存在时间 26 username.setMaxAge(14*24*60*60); 27 passwd.setMaxAge(14*24*60*60); 28 //将Cookie回写到客户端 29 res.addCookie(username); 30 res.addCookie(passwd); 31 } 32 33 34 HttpSession hs = req.getSession(true); 35 //添加session属性 36 hs.setAttribute("uname",user); 37 //设置存在时间 38 hs.setMaxInactiveInterval(20); 39 40 41 //跳转到欢迎页面 42 res.sendRedirect("wel3?user="+user); 43 44 }else { 45 res.sendRedirect("login3?info3=err3"); 46 } 47 }catch(Exception ex) { 48 ex.printStackTrace(); 49 } 50 } 51 public void doPost(HttpServletRequest req,HttpServletResponse res) { 52 this.doGet(req,res); 53 } 54 }
1 package com.tsinghua; 2 import javax.servlet.http.*; 3 import java.io.*; 4 import java.sql.*; 5 import java.util.*; 6 import com.tsinghua.*; 7 8 public class Wel3 extends HttpServlet { 9 public void doGet(HttpServletRequest req,HttpServletResponse res) { 10 //数据库 11 Connection con = null; 12 PreparedStatement ps = null; 13 ResultSet rs = null; 14 try { 15 //html格式声明 16 res.setContentType("text/html;charset=utf8"); 17 //分页 18 int pageSize=3; //每页记录数 19 int pageNow=1; //希望当前页数 20 21 String user=""; 22 String pass=""; 23 24 //动态的接收pageNow 25 String sPageNow = req.getParameter("pageCur"); 26 if(sPageNow!=null) { 27 pageNow = Integer.parseInt(sPageNow); 28 } 29 30 31 PrintWriter pw = res.getWriter(); 32 //获取session属性 33 HttpSession hs = req.getSession(true); 34 String name = (String)hs.getAttribute("uname"); 35 36 //session不存在 37 if(name==null) { 38 //检测是否有Cookie信息 39 Cookie []allCookies = req.getCookies(); 40 if(allCookies != null) { 41 for(int i=0;i<allCookies.length;i++) { 42 Cookie temp = allCookies[i]; 43 if(temp.getName().equals("myName")) { 44 user = temp.getValue(); 45 }else if(temp.getName().equals("myPasswd")) { 46 pass = temp.getValue(); 47 } 48 } 49 if(!user.equals("")&&!pass.equals("")) { 50 res.sendRedirect("loginCl3?username="+user+"&passwd="+pass); 51 }else { 52 //跳转登陆页面,提醒用户未登录 53 res.sendRedirect("login3?info1=err1"); 54 55 } 56 } 57 } 58 59 UserBeanCl ubc = new UserBeanCl(); 60 ArrayList arraylist = ubc.getResultByPage(pageNow,pageSize); 61 int pageCount = ubc.getPageCount(); 62 63 //欢迎用户(用户名) 64 pw.println("Welcome your coming,"+name+"<br/>"); 65 //图片 66 pw.println("<img width=100px height=100px src=imgs/KSYoon.jpg>"); 67 //输出分页后users表中信息 68 pw.println("<table border=1>"); 69 pw.println("<tr>"); 70 pw.println("<th>userid</th>"); 71 pw.println("<th>username</th>"); 72 pw.println("<th>passwd</th>"); 73 pw.println("<th>email</th>"); 74 pw.println("<th>priority</th>"); 75 pw.println("</tr>"); 76 77 for(int i=0;i<arraylist.size();i++) { 78 UserBean ub = (UserBean)arraylist.get(i); 79 pw.println("<tr>"); 80 pw.println("<td>"+ub.getUserId()+"</td>"); 81 pw.println("<td>"+ub.getUserName()+"</td>"); 82 pw.println("<td>"+ub.getPasswd()+"</td>"); 83 pw.println("<td>"+ub.getEmail()+"</td>"); 84 pw.println("<td>"+ub.getPriority()+"</td>"); 85 pw.println("</tr>"); 86 } 87 88 pw.println("</table>"); 89 90 //上一页 91 if(pageNow!=1) { 92 pw.println("<a href=wel3?pageCur="+(pageNow-1)+">上一页</a>"); 93 } 94 95 //页数链接 96 for(int i=pageNow;i<pageNow+10;i++) { 97 //想要显示的当前页 98 pw.println("<a href=wel3?pageCur="+i+">"+i+"</a>"); 99 } 100 101 //下一页 102 if(pageNow!= pageCount) { 103 pw.println("<a href=wel3?pageCur="+(pageNow+1)+">下一页</a>"); 104 } 105 }catch(Exception ex) { 106 ex.printStackTrace(); 107 } 108 } 109 public void doPost(HttpServletRequest req,HttpServletResponse res) { 110 this.doGet(req,res); 111 } 112 }
以上是关于用户登录界面(1.8版本--网站框架改进)的主要内容,如果未能解决你的问题,请参考以下文章