標籤:family row date() action set jdbcutil dao層 Servle attr
登入:
前端頁面:
<body> <form action="/webtext/LogingServlet" method="post"> <input type="text" name="username"><br> <input type="text" name="userpassword"><br> <input type="submit" value="登入"><br> </form></body>
domain:
public class user { private int uid; private String username; private String userpassword; public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpassword() { return userpassword; } public void setUserpassword(String userpassword) { this.userpassword = userpassword; } @Override public String toString() { return "user [uid=" + uid + ", username=" + username + ", userpassword=" + userpassword + "]"; }}
dao層:
public int load(user user) throws SQLException{ Connection conn=JDBCUtils.getConn(); String sql="select * from user where username=? and userpassword=?"; PreparedStatement pst=conn.prepareStatement(sql); pst.setString(1, user.getUsername()); pst.setString(2, user.getUserpassword()); ResultSet rs = pst.executeQuery(); int count=0; while(rs.next()){ count=rs.getInt(1); } JDBCUtils.close(rs, pst, conn); return count;}
service層:
public boolean load(user user){ boolean flag=false; try { int count=Dao.load(user); if(count==1){ flag=true; }else{ flag=false; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return flag;}
登陸的同時檢測登陸人數:
public class LogingServlet extends HttpServlet { private service Service=new service(); public void init() throws ServletException { //擷取ServletContext對象 ServletContext servletContext=this.getServletContext(); //向ServletContext域存入初始count int count=0; servletContext.setAttribute("count", count); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username=request.getParameter("username"); String password=request.getParameter("userpassword"); user user=new user(); user.setUsername(username); user.setUserpassword(password); boolean flag=Service.load(user); if(flag==true){ //擷取ServletContext對象 ServletContext servletContext=this.getServletContext(); //擷取當前count值 int count=(int)servletContext.getAttribute("count"); //改變count值 count++; response.getWriter().write("success"+user+"ni shi di"+count+"ge fang wen de ren"); //重新整理count值 servletContext.setAttribute("count", count); }else{ response.getWriter().write("failure"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
註冊:
dao層
//註冊驗證 public int zhuceyz(String username) throws SQLException{ Connection conn=JDBCUtils.getConn(); String sql="select username from user where username=?"; PreparedStatement pst=conn.prepareStatement(sql); pst.setString(1, username); ResultSet rs = pst.executeQuery(); int count=0; while(rs.next()){ count=rs.getInt(1); } JDBCUtils.close(rs, pst, conn); return count; } //存入註冊資訊 public int zhuce(user user) throws SQLException{ Connection conn=JDBCUtils.getConn(); String sql="insert into user(username,userpassword) values(?,?)"; PreparedStatement pst=conn.prepareStatement(sql); pst.setString(1, user.getUsername()); pst.setString(2, user.getUserpassword()); int row=pst.executeUpdate(); JDBCUtils.close(pst, conn); return row; }}
service層
//註冊驗證 public boolean zhuceyz(String username){ boolean flag=false; try { int count=Dao.zhuceyz(username); if(count==0){ flag=true; }else{ flag=false; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return flag; } //存入註冊資訊 public boolean zhuce(user user){ boolean flag=false; try { int count=Dao.zhuce(user); if(count==1){ flag=true; }else{ flag=false; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return flag; }}
伺服器端
public class ZhuceServlet extends HttpServlet { private service Service=new service(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { while(true){ String username=request.getParameter("username"); String password=request.getParameter("userpassword"); boolean flag=Service.zhuceyz(username); if(flag==false){ response.getWriter().write("yibeizhanyong"); return; }else if(flag==true){ user user=new user(); user.setUsername(username); user.setUserpassword(password); boolean flag1=Service.zhuce(user); if(flag==true){ response.getWriter().write("success"+user); }else{ response.getWriter().write("failure"); } } return; } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
WEB簡單的登入註冊功能(分層)