JavaWeb_利用Servlet处理注册登录请求
利用Servlet处理注册登录请求
程序结构
<%@page import="com.Gary.model.User"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <% Object o = session.getAttribute("user"); if(o!=null){ User user = (User)o; out.println("当前登录用户:"+user.getUsername()+"<br/>"); } %> <% Object o2 = application.getAttribute("userNumber"); if(o2!=null){ out.println("当前登录用户"+o2+"个<br/>"); } %> <a href="login.jsp">登录</a> <a href="register.jsp">注册</a> </body> </html>
<%@page import="com.Gary.model.User"%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <% Object o = session.getAttribute("user"); if(o!=null){ User user = (User)o; out.println("当前登录用户:"+user.getUsername()+"<br/>"); } %> <% Object msg = request.getAttribute("message"); if(msg!=null) out.println(msg); %> 登录<hr> <form action="login_do" method="post"> 用户名:<input type="text" name="username" /><br/> 密码:<input type="password" name="password" /><br/> <input type="submit" value="登录" /> </form> </body> </html>
<%@ page import="com.Gary.model.User" %> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <% //通过session的getAttribute进行访问 User user = (User)session.getAttribute("user"); %> 用户名:<%=user.getUsername()%><br/> 年龄:<%=user.getAge()%><br/> 性别:<%=user.getSex()%><br/> </body> </html>
<%@page import="com.Gary.model.User"%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <% Object o = session.getAttribute("user"); if(o!=null){ User user = (User)o; out.println("当前登录用户:"+user.getUsername()+"<br/>"); } %> <% Object msg = request.getAttribute("message"); if(msg!=null) out.println(msg); %> <form action="register_do" method="post"> 用户名:<input type="text" name="username" /><br/> 密码:<input type="password" name="password" /><br/> 年龄:<input type="text" name="age" /><br/> 性别: 男<input type="radio" name="sex" value="男" />女<input type="radio" name="sex" value="女" /><br/> <input type="submit" value="注册" /> </form> </body> </html>
package com.Gary.model; public class User { private String username; private String password; private int age; private String sex; public User(String username, String password, int age, String sex) { super(); this.username = username; this.password = password; this.age = age; this.sex = sex; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
package com.Gary.servlet; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.Gary.model.User; import com.Gary.util.DBUtil; @WebServlet("/login_do") public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); User user = DBUtil.verifyAccount(username,password); if(user==null){ // out.println("登录失败,用户名或密码错误"); request.setAttribute("message","登录失败,用户名或密码错误"); request.getRequestDispatcher("login.jsp").forward(request,response); }else{ // out.println("登录成功"); int number = 0; //获取到application对象 ServletContext application = this.getServletContext(); //第一次登录用户时if不瞒住,number=0 if(application.getAttribute("userNumber")!=null){ number = (Integer)application.getAttribute("userNumber"); } //登录过一个用户后在原基础number上进行++ number++; application.setAttribute("userNumber",number); //通过seesion的setAttribute传递数据 request.getSession().setAttribute("user", user); //登录请求的转发,将数据传递给personCenter.jsp页面 request.getRequestDispatcher("personCenter.jsp").forward(request,response); } } }
package com.Gary.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.Gary.util.DBUtil; @WebServlet("/register_do") public class RegisterServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); int age = Integer.parseInt(request.getParameter("age")); String sex = request.getParameter("sex"); boolean isSuccess = DBUtil.addUser(username,password,age,sex); //使用请求转发 if(isSuccess){ request.setAttribute("message", "注册成功,请登录"); //通过getRequestDispatcher传递路径将请求转发给login.jsp request.getRequestDispatcher("login.jsp").forward(request,response); }else{ request.setAttribute("message", "注册失败,用户名重复"); //通过getRequestDispatcher传递路径将请求转发给login.jsp request.getRequestDispatcher("register.jsp").forward(request,response); } } }
package com.Gary.util; import java.util.HashMap; import java.util.Map; import com.Gary.model.User; public class DBUtil { private static Map<String ,User>db = new HashMap<String,User>(); public static boolean addUser(String username,String password,int age,String sex) { //TODO if(db.containsKey(username)) { return false; }else { User user=new User(username,password,age,sex); db.put(username, user); return true; } } //账号校验 public static User verifyAccount(String username,String password) { if(db.containsKey(username)) { User user = db.get(username); if(user.getPassword().equals(password)) { return user; }else { return null; } } return null; } }
通过RegisterServlet.java和LoginServlet.java来处理注册登录请求
用户注册
通过户数据模型驱动User,java和DBUtild.java中对数据进行本地处理
register.jsp发送请求表单信息到register_do(RegisterServlet.java路径:@WebServlet("/register_do"))
登录同理注册
<form action="register_do" method="post"> 用户名:<input type="text" name="username" /><br/> 密码:<input type="password" name="password" /><br/> 年龄:<input type="text" name="age" /><br/> 性别: 男<input type="radio" name="sex" value="男" />女<input type="radio" name="sex" value="女" /><br/> <input type="submit" value="注册" /> </form>
RegisterServlet.java通过doPost()方法处理注册信息
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); int age = Integer.parseInt(request.getParameter("age")); String sex = request.getParameter("sex"); boolean isSuccess = DBUtil.addUser(username,password,age,sex); //使用请求转发 if(isSuccess){ request.setAttribute("message", "注册成功,请登录"); //通过getRequestDispatcher传递路径将请求转发给login.jsp request.getRequestDispatcher("login.jsp").forward(request,response); }else{ request.setAttribute("message", "注册失败,用户名重复"); //通过getRequestDispatcher传递路径将请求转发给login.jsp request.getRequestDispatcher("register.jsp").forward(request,response); } }
(如需转载学习,请标明出处)