Jsp+Servlet實(shí)現(xiàn)簡(jiǎn)單登錄注冊(cè)查詢
本文實(shí)例為大家分享了jsp+servlet實(shí)現(xiàn)簡(jiǎn)單登錄注冊(cè)查詢的具體代碼,供大家參考,具體內(nèi)容如下
1、注冊(cè)功能:
制作一個(gè)注冊(cè)頁(yè)面
用戶輸入:
用戶名
密碼
年齡
注冊(cè)成功:——>跳轉(zhuǎn)至登錄頁(yè)面進(jìn)行登錄
注冊(cè)失?。骸?gt;文字或其他形式的提示皆可
2、簡(jiǎn)易查詢:
制作一個(gè)查詢頁(yè)面
輸入用戶名
顯示該用戶的用戶名、密碼、年齡
演示
1.啟動(dòng)進(jìn)入登陸頁(yè)面
2.點(diǎn)擊注冊(cè),進(jìn)入注冊(cè)頁(yè)面,成功跳轉(zhuǎn)到登錄頁(yè)面
失敗則提示
回到登錄頁(yè)面,登錄成功進(jìn)入查詢頁(yè)面
登錄失敗顯示提示信息
輸入用戶名->顯示該用戶的用戶名、密碼、年齡
代碼
dao
public class userdao { private connection conn = null; private preparedstatement ps=null; private int result=0; private resultset rs=null; //用戶注冊(cè) public int register(user user){ string sql="insert into users(name,password,age) value (?,?,?)"; try { //獲取數(shù)據(jù)庫(kù)連接對(duì)象 conn= jdbcutil.getconnection(); //獲取數(shù)據(jù)庫(kù)操作對(duì)象 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; } //登錄驗(yàn)證用戶信息 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 實(shí)體類
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(); // 注冊(cè) @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); //將查詢結(jié)果放入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調(diào)用dao對(duì)數(shù)據(jù)庫(kù)操作 userservice userservice = new userserviceimpl(); int result = userservice.login(username, password); //3 成功跳轉(zhuǎn)到查詢頁(yè)面,失敗跳轉(zhuǎn)到失敗頁(yè)面 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【調(diào)用請(qǐng)求對(duì)象】讀取【請(qǐng)求頭】參數(shù)信息,得到用戶注冊(cè)信息 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 調(diào)用userservice——>userdao // 先查詢用戶是否存在 user byname = userservice.findbyname(username); if (byname!=null){ request.setattribute("info","用戶已存在!"); request.getrequestdispatcher("/jsp/register.jsp").forward(request,response); } // 注冊(cè) result = userservice.register(user); //3 設(shè)置編碼格式,防止亂碼 response.setcontenttype("text/html;charset=utf-8"); printwriter out = response.getwriter(); //注冊(cè)成功:——>跳轉(zhuǎn)至登錄頁(yè)面進(jìn)行登錄 //注冊(cè)失?。骸?gt;注冊(cè)頁(yè)面提示:注冊(cè)失敗 if (result == 1) { response.sendredirect("/login.html"); } else { request.setattribute("info","注冊(cè)失敗!"); request.getrequestdispatcher("/jsp/register.jsp").forward(request,response); } } }
jdbcutil
public class jdbcutil { private jdbcutil(){} //靜態(tài)代碼塊在類加載時(shí)執(zhí)行,并且執(zhí)行一次。 static{ try { class.forname("com.mysql.cj.jdbc.driver"); } catch (classnotfoundexception e) { e.printstacktrace(); } } //獲取數(shù)據(jù)庫(kù)連接對(duì)象 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); } /** *關(guān)閉資源 * @param conn 連接對(duì)象 * @param ps 數(shù)據(jù)庫(kù)操作對(duì)象 * @param rs 結(jié)果集 */ 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" %> 查詢頁(yè)面輸入用戶名,查詢信息 <% 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