java登录界面与mysql_【javaweb】注册登录页面与mysql连接后的验证与传值

本文介绍如何使用JavaWeb创建regist.jsp和login.jsp页面,通过RegisterServlet和UserServlet实现用户注册和登录功能。注册时,数据将插入到s_user表中,登录时验证用户名和密码。注册成功后,用户信息会传递到login.jsp页面。
摘要由CSDN通过智能技术生成

要求:

1)regist.jsp      form  action   method    input  name

2) 定义RegisterServlet     doGet()中实现获取用户名和密码获取到,连数据库---执行insert   into   s_user(uname,upwd)  values(name,pwd);

S_user  表中主键是自增长的,进行插入操作时不需要给主键复制

3) 响应结果—注册成功跳转到login.jsp

4) login.jsp获取刚注册的用户名和密码

eclipse界面

89318ad389f46991e2c6c6d28b8f813b.png

mysql页面:

8dd5f6288e5995c5fe3bf4e6b980c02b.png

注意要点:

传值方法:

RegisterServlet.java页面:

request.setAttribute("Rname", RegisterName);

request.setAttribute("Rpwd", RegisterPwd);

RequestDispatcher rd=request.getRequestDispatcher("login.jsp");

rd.forward(request, response);

.jsp页面: value="${Rname}"

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1

2 pageEncoding="UTF-8"%>

3

4

5

6

7

登录

8

9

10

16

登录页面

17

18 用户名:

19 密码:

20

21

22

23

24

25

login.jsp

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1

2 pageEncoding="UTF-8"%>

3

4

5

6

7

Insert title here

8

9

10

注册页面

11

12 用户名:

13 密码:

14

15

16

17

register.jsp

【UserServlet.java页面】

1 packagecom.shoppingmall.servlet;2 importjava.io.IOException;3 importjava.sql.Connection;4 importjava.sql.DriverManager;5 importjava.sql.PreparedStatement;6 importjava.sql.ResultSet;7 importjava.sql.SQLException;8 importjavax.servlet.ServletException;9 importjavax.servlet.annotation.WebServlet;10 importjavax.servlet.http.HttpServlet;11 importjavax.servlet.http.HttpServletRequest;12 importjavax.servlet.http.HttpServletResponse;13 @WebServlet("/UserServlet")14 public class UserServlet extendsHttpServlet {15 private static final long serialVersionUID = 1L;16 publicUserServlet() {17 super();18 }19 protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {20 response.getWriter().append("Served at: ").append(request.getContextPath());21 request.setCharacterEncoding("UTF-8");22 String name=request.getParameter("inputName");23 String pwd=request.getParameter("inputPwd");24 boolean flag= false;25 try{26 Class.forName("com.mysql.jdbc.Driver");27 String url="jdbc:mysql://localhost:3306/shopping";28 String username="root";29 String password="111111";30 Connection conn=DriverManager.getConnection(url,username,password);31 System.out.println(conn);32 String sql = "select * from employee where username = '"+ name +"' and pwd = '"+ pwd +"' ";33 PreparedStatement ps=conn.prepareStatement(sql);34 ResultSet rs=ps.executeQuery();35 while(rs.next()){36 System.out.println("验证成功!");37 flag=true;38 }39 if(flag){40 response.sendRedirect("success.jsp");41 }else{42 response.sendRedirect("index.jsp");43 }44 }catch(ClassNotFoundException e){45 e.printStackTrace();46 }catch(SQLException e){47 e.printStackTrace();48 }49 }50 protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {51 doGet(request, response);52 }53 }

【RegisterServlet.java页面】

1 packagecom.shoppingmall.servlet;2 importjava.io.IOException;3 importjava.sql.Connection;4 importjava.sql.DriverManager;5 importjava.sql.PreparedStatement;6 importjava.sql.ResultSet;7 importjava.sql.SQLException;8 importjavax.servlet.RequestDispatcher;9 importjavax.servlet.ServletException;10 importjavax.servlet.annotation.WebServlet;11 importjavax.servlet.http.HttpServlet;12 importjavax.servlet.http.HttpServletRequest;13 importjavax.servlet.http.HttpServletResponse;14 @WebServlet("/RegisterServlet")15 public class RegisterServlet extendsHttpServlet {16 private static final long serialVersionUID = 1L;17 publicRegisterServlet() {18 super();19 }20 protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {21 response.getWriter().append("Served at: ").append(request.getContextPath());22 request.setCharacterEncoding("UTF-8");23 response.setContentType("text/html;charset=utf-8");24 String RegisterName=request.getParameter("inputName");25 String RegisterPwd=request.getParameter("inputPwd");26 boolean flag= false;27 try{28 Class.forName("com.mysql.jdbc.Driver");29 String url="jdbc:mysql://localhost:3306/shopping";30 String username="root";31 String password="111111";32 Connection conn=DriverManager.getConnection(url,username,password);33 System.out.println(conn);34 String sql = "select * from employee where username = '"+ RegisterName +"' and pwd = '"+ RegisterPwd +"' "; //数据库中查询是否有35 PreparedStatement ps=conn.prepareStatement(sql);36 ResultSet rs=ps.executeQuery(sql);37 while(rs.next()){38 flag=true;39 }40 if(flag){41 System.out.println("您的账号已存在请直接登录!");42 request.setAttribute("Rname", RegisterName);43 request.setAttribute("Rpwd", RegisterPwd);44 RequestDispatcher rd=request.getRequestDispatcher("login.jsp");45 rd.forward(request, response); //以上四行是账号存在时向login.jsp传值46 }else{47 String sqlInsert = "insert into employee(username,pwd)"+"values('"+RegisterName+"','"+RegisterPwd+"')";  //向数据库中插入数据48 PreparedStatement r=conn.prepareStatement(sqlInsert);49 r.executeUpdate();50 System.out.println("注册成功!");51 request.setAttribute("Rname", RegisterName);52 request.setAttribute("Rpwd", RegisterPwd);53 RequestDispatcher rd=request.getRequestDispatcher("login.jsp");54 rd.forward(request, response);  //以上四行是账号不存在时向login.jsp传值55 }56 }catch(ClassNotFoundException e){57 e.printStackTrace();58 }catch(SQLException e){59 e.printStackTrace();60 }61 }62 protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {63 //TODO Auto-generated method stub

64 response.setCharacterEncoding("UTF-8");65 doGet(request, response);66 }67 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值