首页 技术 正文
技术 2022年11月14日
0 收藏 301 点赞 4,576 浏览 6792 个字

1、当用户在界面提交注册提交的时候,我们在UerServlet来实现具体的业务方法

标准demo:

1CommonUtils

CommonUtils类就两个方法:

lString uuid():生成长度32的随机字符,通常用来做实体类的ID。底层使用了UUID类完成;

toBean(Map, Class<T>):把Map转换成指定类型的Bean对象。通常用来获取表单数据(request.getParameterMap())封装到JavaBean中,底层使用了common-beanutils。注意,本方法要求map中键的名称要与Bean的属性名称相同才能完成映射,否则不能完成映射。

梳理下UserServlet的思路:

1、获得请求的参数,将请求的参数封装成dao数据层操作的对象;

2、对收到的参数进行合法性校验;

3、如果校验成功,将对象保存到数据库中,然后跳转到登陆页面;

4、如果校验失败,将失败的参数封装成一个map对象,将失败的原因在regist.jsp页面中显示出来

我们来看下代码:

JavaWeb网上图书商城完整项目–day02-9.提交注册表单功能之servlet层实现

我们来看看msg.jsp的代码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>信息板</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 is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" >
-->
<style type="text/css">
body {
font-size: 10pt;
color: #404040;
font-family: SimSun;
} .divBody {
margin-left: 15%;
} .divTitle {
text-align:left;
width: 900px;
height: 25px;
line-height: 25px;
background-color: #efeae5;
border: 5px solid #efeae5;
}
.divContent {
width: 900px;
height: 230px;
border: 5px solid #efeae5;
margin-right:20px;
margin-bottom:20px;
}
.spanTitle {
margin-top: 10px;
margin-left:10px;
height:25px;
font-weight: 900;
}
a {text-decoration: none;}
a:visited {color: #018BD3;}
a:hover {color:#FF6600; text-decoration: underline;}
}
</style> </head> <body>
<c:choose>
<c:when test="${code eq 'success' }">
<c:set var="img" value="/images/duihao.jpg"/>
<c:set var="title" value="成功"/>
</c:when>
<c:when test="${code eq 'error' }">
<c:set var="img" value="/images/cuohao.png"/>
<c:set var="title" value="失败"/>
</c:when> </c:choose>
<div class="divBody">
<div class="divTitle">
<span class="spanTitle">${title }</span>
</div>
<div class="divContent">
<div style="margin: 20px;">
<img style="float: left; margin-right: 30px;" src="<c:url value='${img }'/>" width="150"/>
<span style="font-size: 30px; color: #c30; font-weight: 900;">${msg }</span>
<br/>
<br/>
<br/>
<br/>
<span style="margin-left: 50px;"><a target="_top" href="<c:url value='/jsps/user/login.jsp'/>" rel="external nofollow" >登录</a></span>
<span style="margin-left: 50px;"><a target="_top" href="<c:url value='/index.jsp'/>" rel="external nofollow" >主页</a></span>
</div>
</div>
</div> </body>
</html>

1、使用了c标签

<c:choose>、<c:when>和<c:otherwise>在一起连用,可以实现Java语言中的if-else语句的功能。例如以下代码根据username请求参数的值来打印不同的结果:

msg.jsp中存在下面的几个参数

${code}
${msg}这两个属性需要从UserServlet中传递过来,
${code}的值如果等于success,那么title的值显示成功,对应的${img}对应的图片是/images/duihao.jpg
我们来看UserServlet的代码:
package com.weiyuan.goods.user.web.servlet;import java.io.IOException;
import java.util.Map;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 org.apache.commons.collections.map.HashedMap;import com.weiyuan.goods.user.domian.User;
import com.weiyuan.goods.user.service.UserService;import cn.itcast.commons.CommonUtils;
import cn.itcast.servlet.BaseServlet;/**
* Servlet implementation class UserServlet
*/
@WebServlet("/UserServlet")
public class UserServlet extends BaseServlet{
private static final long serialVersionUID = 1L;
private UserService service = new UserService();
/*
* 用户注册页面使用ajax校验/*
* 用户注册页面使用ajax校验用户名会调用该方法
* *会调用该方法
* */
public String validateLoginname(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//首先获得用户上传的用户名
String loginName = request.getParameter("loginname");
boolean flag = service.ajaxValidateLoginName(loginName);
response.getWriter().print(flag);
return null;
}
/*
* 用户注册页面使用ajax校验邮箱会调用该方法
* */
public String validateEmail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//获得用户上传的emai String email = request.getParameter("email");
System.out.println("validateEmail is called"+email);
boolean flag = service.ajaxValidateEmail(email);
response.getWriter().print(flag);
return null;
} /*
* 用户注册页面使用ajax校验验证码会调用该方法
* */
public String validateVerifyCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//获得用户上传的verfycode
String verifyCode = request.getParameter("verifyCode");
//获得session中保存的验证码
String sessionCode = (String) request.getSession().getAttribute("vCode");
//二者进行比较看是否相等
System.out.println("validateVerifyCode is called"+verifyCode+":"+sessionCode);
boolean flag = sessionCode.equalsIgnoreCase(verifyCode);
response.getWriter().print(flag);
return null;
} /*
* 当用户从邮箱点击的激活的时候会调用该方法,并且把激活码传递过来
*
* */
public String activation(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("activation is called"); //1、将请求的参数封装成User对象
User user = CommonUtils.toBean(request.getParameterMap(), User.class);
//2 、对传递过来的参数进行校验,把错误的信息封装到一个hashMap中
Map errors = validateParams(user, request);
if(errors.size() > 0){//说明参数错误,跳转到注册界面提示用户输入的参数有误
request.setAttribute("errors", errors);
return "f:/jsps/user/regist.jsp";
}
service.addUser(user);
request.setAttribute("code", "success");
request.setAttribute("msg", "用户注册成功,请马上到邮箱进行激活");
return "f:/jsps/msg.jsp";
} /*
* 当用户注册的时候会调用该方法
*
* */
public String regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("activation is called"); //1、将请求的参数封装成User对象
User user = CommonUtils.toBean(request.getParameterMap(), User.class);
//2 、对传递过来的参数进行校验,把错误的信息封装到一个hashMap中 return null;
} public Map validateParams(User user,HttpServletRequest request){
Map<String, String> map = new HashedMap();
//校验用户名
String loginName = user.getLoginname();
if(loginName == null || loginName.isEmpty()){
map.put("loginname", "用户名不能为空");
}
if(loginName.length() < 3 || loginName.length() > 20){
map.put("loginname", "用户名长度应该在3到20之间");
}
//校验用户名是否注册
if(service.ajaxValidateLoginName(loginName)){
map.put("loginname", "用户名已经被注册");
} //检查登陆密码
String loginpass = user.getLoginpass();
if(loginpass == null || loginpass.isEmpty()){
map.put("loginpass", "登陆密码不能为空");
}
if(loginpass.length() < 3 || loginpass.length() > 20){
map.put("loginname", "登陆密码的长度应该在3到20之间");
} //检查确认密码的信息
//检查登陆密码
String reloginpass = user.getReloginpass();
if(reloginpass == null || reloginpass.isEmpty()){
map.put("reloginpass", "登陆密码不能为空");
}
if(reloginpass.length() < 3 || reloginpass.length() > 20){
map.put("reloginpass", "登陆密码的长度应该在3到20之间");
}
if(!reloginpass.equalsIgnoreCase(loginpass)){
map.put("reloginpass", "两次输入的密码不一样");
} //检查邮箱
String email = user.getEmail();
if(email == null || email.isEmpty()){
map.put("email", "登陆邮箱不能为空");
}
//检查邮箱的格式是否正确
if(!email.matches("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\\.[a-zA-Z0-9_-]{2,3}){1,2})$")){
map.put("email", "邮箱格式不正确");
} //检查验证码是否相等
String verifyCode = user.getVerifyCode();
//获得session中保存的验证码
String sessionCode =(String) request.getSession().getAttribute("vCode");
if(!verifyCode.equalsIgnoreCase(sessionCode)){
map.put("verifyCode", "验证码不正确");
} return map; }}

    					
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905