要求:
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界面
mysql页面:
注意要点:
传值方法:
RegisterServlet.java页面:
request.setAttribute("Rname", RegisterName);
request.setAttribute("Rpwd", RegisterPwd);
RequestDispatcher rd=request.getRequestDispatcher("login.jsp");
rd.forward(request, response);
.jsp页面: value="${Rname}"
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
1
2 pageEncoding="UTF-8"%>
3
4
5
6
7
Insert title here8
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 }