用户注册
Posted Michael2397
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用户注册相关的知识,希望对你有一定的参考价值。
步骤实现: 1.在index.jsp上点击 注册页面 跳转到register.jsp(不采用) 2.点击注册 <a href="/store/user?method=registUI"></a> 3.在userservlet中编写一个registUI 方法 请求转发到 register.jsp即可.
完成注册:
1.regist
封装数据
设置id
设置state
设置code
调用service 完成注册操作
请求转发 /jsp/msg.jsp
2.userservice中的service
调用dao
发送激活邮件
3.userdao dao 添加一条数据
前端界面
1、<!--/store/WebContent/jsp/index.jsp -- >
<li><a href="${pageContext.request.contextPath}/user?method=registUI">注册</a></li>
2、<!--/store/WebContent/jsp/register.jsp-->
<form class="form-horizontal" style="margin-top:5px;" action="${pageContext.request.contextPath }/user?method=regist" method="post">
3、增加name属性
增加过滤器
作用:解决乱码问题
还需要在web.xml中配置
package com.louis.web.filter; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Map; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; /** * 统一编码 * @author Administrator * */ public class EncodingFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { //1.强转 HttpServletRequest request=(HttpServletRequest) req; HttpServletResponse response=(HttpServletResponse) resp; //2.放行 chain.doFilter(new MyRequest(request), response); } @Override public void destroy() { // TODO Auto-generated method stub } } class MyRequest extends HttpServletRequestWrapper{ private HttpServletRequest request; private boolean flag=true; public MyRequest(HttpServletRequest request) { super(request); this.request=request; } @Override public String getParameter(String name) { if(name==null || name.trim().length()==0){ return null; } String[] values = getParameterValues(name); if(values==null || values.length==0){ return null; } return values[0]; } @Override /** * hobby=[eat,drink] */ public String[] getParameterValues(String name) { if(name==null || name.trim().length()==0){ return null; } Map<String, String[]> map = getParameterMap(); if(map==null || map.size()==0){ return null; } return map.get(name); } @Override /** * map{ username=[tom],password=[123],hobby=[eat,drink]} */ public Map<String,String[]> getParameterMap() { /** * 首先判断请求方式 * 若为post request.setchar...(utf-8) * 若为get 将map中的值遍历编码就可以了 */ String method = request.getMethod(); if("post".equalsIgnoreCase(method)){ try { request.setCharacterEncoding("utf-8"); return request.getParameterMap(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else if("get".equalsIgnoreCase(method)){ Map<String,String[]> map = request.getParameterMap(); if(flag){ for (String key:map.keySet()) { String[] arr = map.get(key); //继续遍历数组 for(int i=0;i<arr.length;i++){ //编码 try { arr[i]=new String(arr[i].getBytes("iso8859-1"),"utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } flag=false; } //需要遍历map 修改value的每一个数据的编码 return map; } return super.getParameterMap(); } }
<filter> <filter-name>EncodingFilter</filter-name> <filter-class>com.louis.web.filter.EncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
后台servlet
package com.louis.web.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 和用户相关的servlet */ public class UserServlet extends BaseServlet { public String add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("userServlet的add方法执行了"); return null; }
/*跳转到注册页面*/ public String registUI(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { return "/jsp/register.jsp"; }
}
完成注册
package com.louis.web.servlet; import java.io.IOException; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.ConvertUtils; import com.louis.domain.User; import com.louis.myconvertor.MyConverter; import com.louis.service.UserService; import com.louis.service.impl.UserServiceImpl; import com.louis.utils.UUIDUtils; /** * 和用户相关的servlet */ public class UserServlet extends BaseServlet { public String add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("userServlet的add方法执行了"); return null; } //调转到注册页面 public String registUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { return "/jsp/register.jsp"; } //完成注册 public String regist(HttpServletRequest request,HttpServletResponse response) throws Exception { //1封装数据 User user = new User(); //注册自定义转化器 ConvertUtils.register(new MyConverter(), Date.class); BeanUtils.populate(user, request.getParameterMap()); //1.1设置用户id user.setUid(UUIDUtils.getId()); //1.2设置激活码 user.setCode(UUIDUtils.getCode()); //2调用service完成注册 UserService service = new UserServiceImpl(); service.regist(user); //页面请求转发 request.setAttribute("msg", "用户已注册,请去邮箱激活"); return "/jsp/msg.jsp"; } }
package com.louis.myconvertor; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.beanutils.Converter; /* * 注册转化器,将字符串转化成日期形式 * */ public class MyConverter implements Converter { @Override public Object convert(Class clazz, Object value) { // class 要装成的类型 // object 页面上传入的值 //将object 转成 date SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { Date date = sdf.parse((String)value); return date; } catch (ParseException e) { e.printStackTrace(); } return null; } }
package com.louis.domain; import java.util.Date; public class User { /** * */ private String uid; private String username; private String password; private String name; private String email; private String telephone; private Date birthday; private String sex; private Integer state = 0;// 激活状态 1:激活 0:未激活 private String code; public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } 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 String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Integer getState() { return state; } public void setState(Integer state) { this.state = state; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } }
action,service,servelt之间的调用关系
见链接:http://pan.baidu.com/s/1pLjyrwf 密码:s57t
问题
注意:
封装数据的时候报了一个错误 是因为 字符串装成时间类型的是出现错误 BeanUtils不支持字符串装成时间 解决方案: 自定义转化器 1.编写一个类 实现Conventer接口 2.实现方法 convert(转换成的类型,前台页面传入的字符串) 3.注册转化器 在封装数据之前注册 ConvertUtils.register(new MyConventer(), Date.class);
2、
${msg}
//页面请求转发
request.setAttribute("msg", "用户已注册,请去邮箱激活");
return "/jsp/msg.jsp";
3、
${pageContext.request.contextPath}
参考:http://ps329795485-126-com.iteye.com/blog/1290662
${pageContext.request.contextPath}等价于<%=request.getContextPath()%> 或者可以说是<%=request.getContextPath()%>的EL版 意思就是取出部署的应用程序名或者是当前的项目名称 比如我的项目名称是ajax01 在浏览器中输入为http://localhost:8080/ajax01/login.jsp ${pageContext.request.contextPath}或<%=request.getContextPath()%>取出来的就是/ajax01,而"/"代表的含义就是http://localhost:8080 所以我们项目中应该这样写${pageContext.request.contextPath}/login.jsp
4、
日期转化
参考地址:http://www.blogjava.net/Werther/archive/2009/06/09/280954.html
/** * 日期转换成字符串 * @param date * @return str */ public static String DateToStr(Date date) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String str = format.format(date); return str; } /** * 字符串转换成日期 * @param str * @return date */ public static Date StrToDate(String str) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = null; try { date = format.parse(str); } catch (ParseException e) { e.printStackTrace(); } return date; }
以上是关于用户注册的主要内容,如果未能解决你的问题,请参考以下文章