java mysql登录和注册功能
Frank Kong 人气:0初学JAVA EE,老师留下一小作业,用JAVA实现与服务器端交互,实现登录和注册功能,初学一种专业课很多老师都会留下一种让学生实现登录和注册的作业。
下面是记录的实现步骤:
1、首先是账号密码输入框和按钮:
登录:
<form action="login.do" method="get"> <table> <tr><td>账号:</td><td><input style="line-height: 28px;" type="text" name="id" /></td></tr> <tr><td>密码:</td><td><input style="line-height: 28px;" type="password" name="password" /></td></tr> <tr><td> <a href="register.jsp" rel="external nofollow" >还没注册?点击注册</a></td><td><input id="login_in" type="submit" value=" 登录 "></td></tr> </table> </form>
注册:
<form name="form1" action="register.do" method="post"> <table width="200" border="0"> <tr> <td colspan="2"> <div id="status">注册新用户</div> </td> <tr> <td>*用户名</td> <td><input type="text" name="username" size="10" id="username"></td> </tr> <tr> <td>*密码</td> <td><input type="password" name="password1" size="10"></td> </tr> <tr> <td>*确认密码</td> <td><input type="password" name="password2" size="10"></td> </tr> <tr> <td>Email</td> <td><input type="text" name="email" size="10"></td> </tr> <tr> <td colspan="2"><a href="login.jsp" rel="external nofollow" >返回登录页</a> <input type="submit" name="submit" value="提交注册"></td> </tr> </table> </form>
两个form表的属性里都有action,可理解为是用来标记登录和注册的,在web.xml中和对应的servlet绑定
2、数据库建表,插入数据
在服务器中打开MySQL,建表
create table login( uname char(15) primary key, password char(15) not null, email char(20) )
3、sql语句内嵌到java语句中,查找(登录)和插入(注册)用户信息,以下类名即为文件的名字
登录:
import java.sql.*; public class userlogin { String drv = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/login"; String usr = "root"; String pwd = "***";//自己的密码 public boolean isuserlogin(String id,String password){ boolean isValid = false; String sql="select * from ulogin where uname='"+id+"' and password='"+password+"'"; try{ Class.forName(drv).newInstance(); Connection conn = DriverManager.getConnection(url,usr,pwd); Statement stm = conn.createStatement(); ResultSet rs = stm.executeQuery(sql); if(rs.next()){ isValid = true; } rs.close(); stm.close(); conn.close(); }catch (Exception e) { e.printStackTrace(); System.out.println(e); } if(isValid){//判断用户名以及密码是否与设定相符 return true; } else return false; } }
注册:
import java.sql.*; public class register { String drv = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/login"; String usr = "root"; String pwd = "***"; public boolean userregister(String id,String password,String email){ boolean b = false; String sql = "select * from ulogin where uname='"+id+"'"; try{ Class.forName(drv).newInstance(); Connection conn = DriverManager.getConnection(url,usr,pwd); Statement stm = conn.createStatement(); ResultSet rs = stm.executeQuery(sql); if(!rs.next()){ sql = "insert into ulogin(uname,password,email) values('"+id+"','"+password+"','"+email+"')"; stm.execute(sql); b = true; } rs.close(); stm.close(); conn.close(); }catch (Exception e) { e.printStackTrace(); System.out.println(e); } if(b) { return true; } else return false; } }
4、构建servlet来处理事务,如果连接数据库查找成功,则登录成功,否则失败;如果注册成功返回登录界面
登录:由用户输入信息,所以使用doGet方法
@WebServlet(name = "ServletLogin") public class ServletLogin extends HttpServlet { public void init() throws ServletException { } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ boolean b=false; userlogin MyPOJO=new userlogin();//新建MyPOJO类的对象myPOJO //根据标示名获取JSP文件中表单所包含的参数 String id=request.getParameter("id"); String password=request.getParameter("password"); String result = ""; b=MyPOJO.isuserlogin(id,password);//使用模型对账号和密码进行验证,返回一个boolean类型的对象 PrintWriter out = response.getWriter(); if(b){ //如果验证结果为真,跳转至登录成功页面 out.println("success"); result = "success"; //Cookie Cookie username= new Cookie("username",id); //设置路径,这个路径即该工程下都可以访问该cookie 如果不设置路径,那么只有设置该cookie路径及其子路径可以访问 username.setPath("/"); username.setMaxAge(60*60); response.addCookie(username); response.sendRedirect("index.jsp"); } else { //如果验证结果为假,跳转至登录失败页面 out.println("fail"); result = "fail"; response.sendRedirect("JSP/LoginFailed.jsp" ); } out.write(result); out.flush(); out.close(); System.out.println(result); }
注册:使用doPost方法
@WebServlet(name = "ServletRegister") public class ServletRegister extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { boolean b = false; register myPOJO=new register(); String id=request.getParameter("username"); String password=request.getParameter("password1"); String email = request.getParameter("email"); b=myPOJO.userregister(id,password,email); if(b){ response.sendRedirect("login.jsp"); } else{ response.sendRedirect("register.jsp"); } } }
5、在web.xml中将servlet和url-pattern进行匹配
<servlet> <servlet-name>ServletLogin</servlet-name> <servlet-class>service.ServletLogin</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletLogin</servlet-name> <url-pattern>/login.do</url-pattern> </servlet-mapping> <servlet> <servlet-name>ServletRegister</servlet-name> <servlet-class>service.ServletRegister</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletRegister</servlet-name> <url-pattern>/register.do</url-pattern> </servlet-mapping>
6、测试运行
将服务器端的mysql数据库中加入一条用户信息:
insert into ulogin values('test1','123',null);
网页上输入该信息:
点击登录:登陆成功
注册:在注册框中输入信息:
提交后在数据库中查找该用户信息:
注意:项目中应该导入jsp-api.jar和servlet-api.jar两个jar包
加载全部内容