概述
JSP模式2 实现用户注册的功能
一 实现的思路:
1.Register.jsp
负责向用户显示注册的页面,用户名、密码、确认密码、email
2.ControllerServlet.java
用来接收客户端提交的请求,然后进行验证:语法格式的验证和该用户是否是已经存在的用户,如果该用户是合法的,将该用户加入到users表中,然后跳转到loginSuccess.jsp页面上,显示用户信息。否则跳转到register.jsp,将错误信息显示出来
3.RegisterFormBean.java:用来封装用户输入的表单的数据,并可以做基本语法格式的验证
4.UserBean.java :保存用户的信息,包括用户名、密码以及确认密码
5.DbUtil.java: 集合对象 user,<”zhangsan”,user>
添加用户的方法 insertUser(User user)
6.自定义异常类---提示用户注册错误,写一个异常类 DbUtilException
二 实现的代码:
1.register.jsp
<%@ page language="java"import="java.util.*"pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() +"://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP'register.jsp' starting page</title>
<meta http-equiv="pragma"content="no-cache">
<meta http-equiv="cache-control"content="no-cache">
<meta http-equiv="expires"content="0">
<meta http-equiv="keywords"content="keyword1,keyword2,keyword3">
<meta http-equiv="description"content="This ismy page">
<!--
<link rel="stylesheet"type="text/css" href="styles.css">
-->
</head>
<body>
<jsp:useBean id="registerForm"class="com.csdn.beans.RegisterFormBean"
scope="request"/>
<jsp:setPropertyproperty="*"name="registerForm"/>
<form action="registerServlet"method="post">
<table>
<tbody>
<tr>
<td>
用户名:
</td>
<td>
<input type="text"name="name"value="<%=registerForm.getName()%>"/>
</td>
<td>
<%=registerForm.getErrorMsg("name") %>
</td>
</tr>
<tr>
<td>
密 码:
</td>
<td>
<input type="password"name="password1"value="<%=registerForm.getPassword1()%>"/>
</td>
<td>
<%=registerForm.getErrorMsg("password1") %>
</td>
</tr>
<tr>
<td>
确认密码:
</td>
<td>
<input type="password"name="password2"value="<%=registerForm.getPassword2()%>"/>
</td>
<td>
<%=registerForm.getErrorMsg("password2") %>
</td>
</tr>
<tr>
<td>
email:
</td>
<td>
<input type="text"name="email"value="<%=registerForm.getEmail()%>"/>
</td>
<td>
<%=registerForm.getErrorMsg("email") %>
</td>
</tr>
<tr>
<td>
<input type="submit"name="submit"value="注册" />
</td>
<td>
</td>
<td>
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
2.ControllerServlet.java
public void doPost(HttpServletRequestrequest, HttpServletResponse response)
throwsServletException, IOException {
RequestDispatcher rd =null;
//控制器
//获取表单提交的数据封装在registerFormBean对象中,进行语法格式的验证
RegisterFormBeanregisterForm = newRegisterFormBean();
request.setAttribute("registerForm", registerForm);
registerForm.setName(request.getParameter("name"));
registerForm.setPassword1(request.getParameter("password1"));
registerForm.setPassword2(request.getParameter("password2"));
registerForm.setEmail(request.getParameter("email"));
//验证
if(!registerForm.validte()){
//转向到register.jsp
rd = request.getRequestDispatcher("/register.jsp");
rd.forward(request, response);
return;
}
//将registerFormBean对象中的数据存储到UserBean对象中,然后调用DbUtil中的insertUtil
UserBean user = new UserBean();
user.setName(registerForm.getName());
user.setPassword(registerForm.getPassword1());
user.setPassword(registerForm.getPassword2());
user.setEmail(registerForm.getEmail());
DbUtil db = DbUtil.getInstance();
try {
db.insertUser(user);
} catch(DbUtilException ex) {
// 处理该异常
registerForm.setErrorMsg("name", ex.getErrorMsg("name"));
//转到register.jsp页面中
rd = request.getRequestDispatcher("/register.jsp");
rd.forward(request, response);
return;
}
//得到session对象
HttpSession session=request.getSession();
session.setAttribute("user", user);
rd =request.getRequestDispatcher("/loginSuccess");
rd.forward(request,response);
}
3.RegisterFormBean.java
package com.csdn.beans;
import java.util.Hashtable;
publicclass RegisterFormBean {
private String name = "";
private String password1 ="";
private String password2 ="";
private String email = "";
public RegisterFormBean() {
super();
// TODOAuto-generated constructor stub
}
public RegisterFormBean(String name,String password1, String password2,
String email) {
super();
this.name =name;
this.password1 =password1;
this.password2 =password2;
this.email =email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name =name;
}
public String getPassword1() {
returnpassword1;
}
public void setPassword1(Stringpassword1) {
this.password1 =password1;
}
public String getPassword2() {
returnpassword2;
}
public void setPassword2(Stringpassword2) {
this.password2 =password2;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email =email;
}
@Override
public String toString() {
return"RegisterFormBean [email=" + email + ", name=" + name
+ ", password1=" + password1 + ", password2=" + password2 +"]";
}
// 基本格式的验证
private Hashtable errors = new Hashtable();
@SuppressWarnings("unchecked")
public boolean validte() {
boolean okAll = true;
// 语法格式的验证
if(name.trim().equals("")) {
errors.put(name, "请重新输入");
okAll = false;
}
if(password1.length() > 10 || password1.length() < 6) {
errors.put("password1", "密码必须包含6-10个字符");
okAll = false;
}
if(!password2.equals("password1")) {
errors.put("password2", "密码不一致");
okAll = false;
}
if(!email.matches("[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+")){
errors.put("email", "邮箱格式不正确");
okAll = false;
}
return okAll;
}
public void setErrorMsg(String err,String errMsg) {
if (err != null &&errMsg != null){
errors.put(err, errMsg);
}
}
public String getErrorMsg(String err){
Object message =(String) errors.get(err);
return (String)((message == null)? "" : message);
}
}
4.UserBean.java
package com.csdn.beans;
publicclass UserBean {
private String name;
private String password;
private String email;
public UserBean() {
super();
// TODOAuto-generated constructor stub
}
public UserBean(String name, Stringpassword, String email) {
super();
this.name =name;
this.password =password;
this.email =email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name =name;
}
public String getPassword() {
return password;
}
public void setPassword(Stringpassword) {
this.password =password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email =email;
}
@Override
public String toString() {
return"UserBean [email=" + email + ", name=" + name + ",password="
+ password + "]";
}
//验证方法
public boolean validatePassword(Stringpassword){
if(this.password.equals(password)){
return true;
}else{
return false;
}
}
}
5.DbUtil.java
package com.csdn.util;
import java.util.Hashtable;
import sun.security.jca.GetInstance;
import com.csdn.beans.UserBean;
import com.csdn.exception.DbUtilException;
//单利类的实现
publicclass DbUtil {
private Hashtable users = new Hashtable();
static DbUtil instance = new DbUtil();
private DbUtil(){
UserBean user1 = new UserBean();
user1.setName("张三");
user1.setPassword("1234567");
user1.setEmail("zhangsan@163.com");
UserBean user2 = new UserBean();
user2.setName("李四");
user2.setPassword("1234567");
user2.setEmail("zhangsan@163.com");
UserBean user3 = new UserBean();
user3.setName("王五");
user3.setPassword("1234567");
user3.setEmail("zhangsan@163.com");
users.put(user1.getName(),user1);
users.put(user2.getName(),user2);
users.put(user3.getName(),user3);
}
public static DbUtil getInstance(){
return instance;
}
public UserBean getUser(String name){
UserBean user =(UserBean) users.get(name);
return user;
}
public void insertUser(UserBean user) throwsDbUtilException{
if(user==null){
return;
}
StringuserName=user.getName();
if(users.get(userName)!=null){
//说明该用户名已经存在
DbUtilException ex = new DbUtilException();
ex.setErrorMsg("name","该用户已经存在");
throwex;
}
//说明该用户不存在,添加
users.put(userName,user);
}
}
6.自定义异常类
package com.csdn.exception;
import java.util.Hashtable;
//异常处理类
publicclass DbUtilException extendsException {
// 存储错误消息
private Hashtable errors = new Hashtable();
public void setErrorMsg(String err,String msg) {
if (err != null &&msg != null){
errors.put(err, msg);
}
}
public String getErrorMsg(String err){
Object message =(String) errors.get(err);
return (String)((message == null)? "" : message);
}
}
最后
以上就是调皮灯泡为你收集整理的JSP模式2 实现用户注册的功能的全部内容,希望文章能够帮你解决JSP模式2 实现用户注册的功能所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复