JavaWeb_(request和response)用户登录注册模板_基础版
用户登录注册模板进阶版 传送门
用户登录注册模板基础版
登录:当用户登录成功时,跳转到personCenter.jsp,当用户登录失败时,跳转到login.jsp并给出提示
注册:当用户注册成功时,跳转到login.jsp,当用户注册失败时,跳转到register.jsp并给出提示
personCenter.jsp展示用户信息
用户登录注册模板基础版 目录结构【用户登录_02版】
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <a href="login.jsp">登录</a> <a href="register.jsp">注册</a> </body> </html>
<%@ 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 msg = request.getAttribute("message"); if(msg!=null) out.println(msg); %> 登录<hr> <form action="login_do.jsp" methon="post"> 用户名:<input type="text" name="username" /><br/> 密码 :<input type="password" name="password" /><br/> <input type="submit" value="登录"/> </form> </body> </html>
<%@page import="com.Gary.util.DBUtil" %> <%@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> <% 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("登录成功"); //登录请求的转发,将数据传递给personCenter.jsp页面 request.setAttribute("user", user); request.getRequestDispatcher("personCenter.jsp").forward(request,response); } %> </body> </html>
<%@ 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 msg = request.getAttribute("message"); if(msg!=null) out.println(msg); %> <form action="register_do.jsp" methon="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>
<%@ page import="com.Gary.util.DBUtil" %> <%@ 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> <% 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); } // if(isSuccess){ // out.println(username); // out.println("<font color='green'>注册成功</font>"); // }else{ // out.println(username); // out.println("<font color='red'>注册失败</font>"); // } %> </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> <% User user = (User)request.getAttribute("user"); %> 用户名:<%=user.getUsername()%><br/> 年龄:<%=user.getAge()%><br/> 性别:<%=user.getSex()%><br/> </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.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; } }
用户注册_01版
当用户点击主页面注册时跳转到register.jsp页面,用户输入完表单后由register_do.jsp页面进行处理,户数据模型驱动User,java和DBUtild.java中对数据进行本地处理
DBUtild.java对用户ID进行同名检测,用户注册成功显示绿色提示信息,用户注册失败显示红色提示信息
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <a>登录</a> <a href="register.jsp">注册</a> </body> </html>
<%@ 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> <form action="register_do.jsp" methon="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>
<%@ page import="com.Gary.util.DBUtil" %> <%@ 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> <% 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){ out.println(username); out.println("<font color='green'>注册成功</font>"); }else{ out.println(username); out.println("<font color='red'>注册失败</font>"); } %> </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.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; } } }
开发注册界面
表单
用户表单信息注册完毕后提交表单给register_do.jsp
<form action="register_do.jsp" methon="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>
户数据模型驱动User,java存储用户信息,DBUtil.java运用字典集对数据进行本地存储
private static Map<String ,User>db = new HashMap<String,User>();
通过DBUtil.java中的addUser()方法对用户进行本地注册,返回bool值,当用户注册ID相同时返回false,否则返回true
if(db.containsKey(username)) { return false; }else { User user=new User(username,password,age,sex); db.put(username, user); return true; }
由register_do.java对用户信息进行校验
boolean isSuccess = DBUtil.addUser(username,password,age,sex); if(isSuccess){ out.println(username); out.println("<font color='green'>注册成功</font>"); }else{ out.println(username); out.println("<font color='red'>注册失败</font>"); }
用户注册_02版
当用户通过首页直接进入login.jsp时,login.jsp页面只显示登录字样,用户通过register.jsp进入login.jsp页面时,跳转到login.jsp并提示用户注册成功
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <a href="login.jsp">登录</a> <a href="register.jsp">注册</a> </body> </html>
<%@ 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 msg = request.getAttribute("message"); if(msg!=null) out.println(msg); %> 登录<hr> <form action="" methon="post"> 用户名:<input type="text" name="username" /><br/> 密码 :<input type="password" name="password" /><br/> <input type="submit" value="登录"/> </form> </body> </html>
<%@ page import="com.Gary.util.DBUtil" %> <%@ 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> <% 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); } // if(isSuccess){ // out.println(username); // out.println("<font color='green'>注册成功</font>"); // }else{ // out.println(username); // out.println("<font color='red'>注册失败</font>"); // } %> </body> </html>
<%@ 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> <form action="register_do.jsp" methon="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.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; } } }
当登录成功时,通过在register_do.jsp页面中表单传递时进行转发给login,jsp页面【跳转到login.jsp页面】
boolean isSuccess = DBUtil.addUser(username,password,age,sex); //使用请求转发 if(isSuccess){ request.setAttribute("message", "注册成功,请登录"); //通过getRequestDispatcher传递路径将请求转发给login.jsp request.getRequestDispatcher("login.jsp").forward(request,response); }
login,jsp页面最开始时进行判断是否由register.jsp传递过来信息
<% Object msg = request.getAttribute("message"); if(msg!=null) out.println(msg); %>
用户登录_01版
当用户登录后,login.jsp表单中的信息与数据库中的账号进行匹配,当用户的账号存在时且密码输入正确,提示用户登录成功,用户名不存在或用户名与账号匹配错误时提示登录失败
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <a href="login.jsp">登录</a> <a href="register.jsp">注册</a> </body> </html>
<%@ 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 msg = request.getAttribute("message"); if(msg!=null) out.println(msg); %> 登录<hr> <form action="login_do.jsp" methon="post"> 用户名:<input type="text" name="username" /><br/> 密码 :<input type="password" name="password" /><br/> <input type="submit" value="登录"/> </form> </body> </html>
<%@page import="com.Gary.util.DBUtil" %> <%@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> <% String username = request.getParameter("username"); String password = request.getParameter("password"); User user = DBUtil.verifyAccount(username,password); if(user==null){ out.println("登录失败,用户名或密码错误"); }else{ out.println("登录成功"); } %> </body> </html>
<%@ 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> <form action="register_do.jsp" methon="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>
<%@ page import="com.Gary.util.DBUtil" %> <%@ 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> <% 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); } // if(isSuccess){ // out.println(username); // out.println("<font color='green'>注册成功</font>"); // }else{ // out.println(username); // out.println("<font color='red'>注册失败</font>"); // } %> </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.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; } }
当用户提交登录信息时,由login_do处理用户登录的信息,
通过DBUtil.java中的verifyAccount()进行用户校验,当用户名存在并密码与用户相匹配时返回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; }
login_do.jsp中进行账号校验
User user = DBUtil.verifyAccount(username,password); if(user==null){ out.println("登录失败,用户名或密码错误"); }else{ out.println("登录成功"); }
用户登录_02版
登录:当用户登录成功时,跳转到personCenter.jsp,当用户登录失败时,跳转到login.jsp并给出提示
注册:当用户注册成功时,跳转到login.jsp,当用户注册失败时,跳转到register.jsp并给出提示
personCenter.jsp展示用户信息
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <a href="login.jsp">登录</a> <a href="register.jsp">注册</a> </body> </html>
<%@ 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 msg = request.getAttribute("message"); if(msg!=null) out.println(msg); %> 登录<hr> <form action="login_do.jsp" methon="post"> 用户名:<input type="text" name="username" /><br/> 密码 :<input type="password" name="password" /><br/> <input type="submit" value="登录"/> </form> </body> </html>
<%@page import="com.Gary.util.DBUtil" %> <%@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> <% 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("登录成功"); //登录请求的转发,将数据传递给personCenter.jsp页面 request.setAttribute("user", user); request.getRequestDispatcher("personCenter.jsp").forward(request,response); } %> </body> </html>
<%@ 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 msg = request.getAttribute("message"); if(msg!=null) out.println(msg); %> <form action="register_do.jsp" methon="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>
<%@ page import="com.Gary.util.DBUtil" %> <%@ 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> <% 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); } // if(isSuccess){ // out.println(username); // out.println("<font color='green'>注册成功</font>"); // }else{ // out.println(username); // out.println("<font color='red'>注册失败</font>"); // } %> </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> <% User user = (User)request.getAttribute("user"); %> 用户名:<%=user.getUsername()%><br/> 年龄:<%=user.getAge()%><br/> 性别:<%=user.getSex()%><br/> </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.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; } }
personCenter.jsp通过request内置对象得到用户信息后输出其信息
<% User user = (User)request.getAttribute("user"); %> 用户名:<%=user.getUsername()%><br/> 年龄:<%=user.getAge()%><br/> 性别:<%=user.getSex()%><br/>
login_do.jsp处理用户登录成功时和登录失败时事件
<% 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("登录成功"); //登录请求的转发,将数据传递给personCenter.jsp页面 request.setAttribute("user", user); request.getRequestDispatcher("personCenter.jsp").forward(request,response); } %>
register_do.jsp处理用户注册成功时和注册失败时事件
<% 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); } // if(isSuccess){ // out.println(username); // out.println("<font color='green'>注册成功</font>"); // }else{ // out.println(username); // out.println("<font color='red'>注册失败</font>"); // } %>