Jsp+Servlet實現(xiàn)簡單登錄注冊查詢
本文實例為大家分享了jsp+servlet實現(xiàn)簡單登錄注冊查詢的具體代碼,供大家參考,具體內容如下
1、注冊功能:
制作一個注冊頁面
用戶輸入:
用戶名
密碼
年齡
注冊成功:——>跳轉至登錄頁面進行登錄
注冊失敗:——>文字或其他形式的提示皆可
2、簡易查詢:
制作一個查詢頁面
輸入用戶名
顯示該用戶的用戶名、密碼、年齡
演示
1.啟動進入登陸頁面
2.點擊注冊,進入注冊頁面,成功跳轉到登錄頁面
失敗則提示
回到登錄頁面,登錄成功進入查詢頁面
登錄失敗顯示提示信息
輸入用戶名->顯示該用戶的用戶名、密碼、年齡
代碼
dao
public class userdao { private connection conn = null; private preparedstatement ps=null; private int result=0; private resultset rs=null; //用戶注冊 public int register(user user){ string sql="insert into users(name,password,age) value (?,?,?)"; try { //獲取數(shù)據(jù)庫連接對象 conn= jdbcutil.getconnection(); //獲取數(shù)據(jù)庫操作對象 ps=conn.preparestatement(sql); ps.setstring(1,user.getname()); ps.setstring(2,user.getpassword()); ps.setint(3,user.getage()); //執(zhí)行sql result=ps.executeupdate(); } catch (exception e) { e.printstacktrace(); }finally { jdbcutil.close(null,ps,conn); } return result; } //登錄驗證用戶信息 public int login(string username,string password){ string sql ="select count(*) from users where name=? and password=?"; try { conn=jdbcutil.getconnection(); ps=conn.preparestatement(sql); ps.setstring(1,username); ps.setstring(2,password); rs=ps.executequery(); while (rs.next()){ result=rs.getint("count(*)"); } } catch (exception e) { e.printstacktrace(); } finally { jdbcutil.close(rs,ps,conn); } return result; } //根據(jù)用戶名 顯示用戶名、密碼、年齡 public user findbyname(string username){ string sql="select name,password,age from users where name=?"; user user = null; try { conn=jdbcutil.getconnection(); ps=conn.preparestatement(sql); ps.setstring(1,username); rs=ps.executequery(); while (rs.next()){ string name = rs.getstring("name"); string password = rs.getstring("password"); int age = rs.getint("age"); user = new user(name,password,age); } } catch (exception e) { e.printstacktrace(); }finally { jdbcutil.close(null,ps,conn); } return user; } }
entity 實體類
public class user { private int id; private string name; private string password; private int age; //set... //get... //constructor... }
service
public class userserviceimpl implements userservice { userdao userdao = new userdao(); // 注冊 @override public int register(user user) { return userdao.register(user); } // 登陸 @override public int login(string username, string password) { return userdao.login(username,password); } // 根據(jù)用戶名查找信息 @override public user findbyname(string username) { return userdao.findbyname(username); } }
servlet
// findbynameservlet public class findbynameservlet extends httpservlet { protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string name = request.getparameter("name"); userservice userservice = new userserviceimpl(); user user = userservice.findbyname(name); //將查詢結果放入request作用域 request.setattribute("userinfo",user); request.getrequestdispatcher("/jsp/index.jsp").forward(request,response); } } // loginservlet public class loginservlet extends httpservlet { protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { //1 獲取 string username = request.getparameter("username"); string password = request.getparameter("password"); //2 service調用dao對數(shù)據(jù)庫操作 userservice userservice = new userserviceimpl(); int result = userservice.login(username, password); //3 成功跳轉到查詢頁面,失敗跳轉到失敗頁面 if (result>0){ response.sendredirect("/jsp/index.jsp"); }else{ response.sendredirect("/login_error.html"); } } } // registerservlet public class registerservlet extends httpservlet { protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { userservice userservice = new userserviceimpl(); user user = null; int result = 0; //1【調用請求對象】讀取【請求頭】參數(shù)信息,得到用戶注冊信息 string username, password, age; username = request.getparameter("username"); password = request.getparameter("password"); age = request.getparameter("age"); user = new user(username, password, integer.valueof(age)); //2 調用userservice——>userdao // 先查詢用戶是否存在 user byname = userservice.findbyname(username); if (byname!=null){ request.setattribute("info","用戶已存在!"); request.getrequestdispatcher("/jsp/register.jsp").forward(request,response); } // 注冊 result = userservice.register(user); //3 設置編碼格式,防止亂碼 response.setcontenttype("text/html;charset=utf-8"); printwriter out = response.getwriter(); //注冊成功:——>跳轉至登錄頁面進行登錄 //注冊失敗:——>注冊頁面提示:注冊失敗 if (result == 1) { response.sendredirect("/login.html"); } else { request.setattribute("info","注冊失敗!"); request.getrequestdispatcher("/jsp/register.jsp").forward(request,response); } } }
jdbcutil
public class jdbcutil { private jdbcutil(){} //靜態(tài)代碼塊在類加載時執(zhí)行,并且執(zhí)行一次。 static{ try { class.forname("com.mysql.cj.jdbc.driver"); } catch (classnotfoundexception e) { e.printstacktrace(); } } //獲取數(shù)據(jù)庫連接對象 public static connection getconnection() throws exception{ string url="jdbc:mysql://127.0.0.1:3306/zy?&usessl=false&servertimezone=utc&rewritebatchedstatements=true"; string user="root"; string password="rootroot"; return drivermanager.getconnection(url,user,password); } /** *關閉資源 * @param conn 連接對象 * @param ps 數(shù)據(jù)庫操作對象 * @param rs 結果集 */ public static void close(resultset rs, statement ps, connection conn){ if (rs != null) { try { rs.close(); } catch (sqlexception e) { e.printstacktrace(); } } if (ps != null) { try { ps.close(); } catch (sqlexception e) { e.printstacktrace(); } } if (conn != null) { try { conn.close(); } catch (sqlexception e) { e.printstacktrace(); } } } }
index.jsp
<%@ page import="entity.user" %> <%@ page contenttype="text/html;charset=utf-8" language="java" %> 查詢頁面輸入用戶名,查詢信息 <% user userinfo = (user) request.getattribute("userinfo"); %> <% if (userinfo != null) { %>
<% } %>
用戶名 密碼 年齡 <%=userinfo.getname()%> <%=userinfo.getpassword()%> <%=userinfo.getage()%>
register.jsp
<%@ page import="com.mysql.cj.util.stringutils" %> <%@ page contenttype="text/html;charset=utf-8" language="java" %> title <% string info =(string) request.getattribute("info"); %> <% if (!stringutils.isnullorempty(info)){ %><%=info%>
<% } %>
用戶名 密碼 年齡
web.xml
loginservlet servlet.loginservlet registerservlet servlet.registerservlet findbynameservlet servlet.findbynameservlet loginservlet /login registerservlet /register findbynameservlet /findbyname login.html