本文主要是介绍JavaWeb学习-MVC基础开发系列-2-用户注册功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前面一篇,我们把开发练习的环境和代码都准备好了,这篇我们直接来写用户注册的代码。先来分析一下用户注册的页面,我们需要给用户提供一个页面,点击注册链接,进入到注册表单页面。用户输入用户名和密码,点击注册,提示用户是否完成注册。
1.在service层写一个注册接口
接着前面一篇代码,前面我们写完了dao层的接口和实现类。这里我们在service下新建一个接口,里面就一个注册方法。
package com.anthony.service;import com.anthony.domain.User;public interface UserService {/*** 用户注册* @param user* @throws Exception*/public void register(User user) throws Exception;}
2.写注册接口实现类
package com.anthony.service.impl;import com.anthony.dao.UserDao;
import com.anthony.dao.impl.UserDaoImpl;
import com.anthony.domain.User;
import com.anthony.service.UserService;public class UserServiceImpl implements UserService {public void register(User user) throws Exception {//业务层掉DAOUserDao userDao = new UserDaoImpl();userDao.addUser(user);}}
接下来本来是写web层servlet如何调用UserDao,但是我们没有表单数据,所以接下来我们先写表单数据
3.首页添加登录和注册入口
我们创建一个index.jsp,里面提供两个链接,登录和注册,点击分别跳转login.jsp和regist.jsp
index.jsp代码
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>网站首页</title>
</head>
<body><a href="">登录</a><a href="register.jsp">注册</a></body>
</html>
这里我们登录先不管,本篇就完成注册。部署到tomcat,浏览器打开,效果是这样的。
4. 写注册表单
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册</title>
</head>
<body><form action="${pageContext.request.contextPath}/servlet/regServlet" method="post">用户名:<input type="text" name="username"/><br/>密码:<input type="password" name="password"/><br/>确认密码:<input type="password" name="repassword"/><br/>邮箱:<input type="text" name="email"/><br/>生日:<input type="text" name="birthday"/><br/><input type="submit" value="注册"></form>
</body>
</html>
表单效果是这样的
5.写一个servlet类
上面我们在register这个表单中设置了跳转到/servlet/regServlet这个Controller处理,所以接下来就来写这个servlet.
在web.servlet包下新建一个RegServlet.java
package com.anthony.web.servlet;import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.beanutils.BeanUtils;import com.anthony.domain.User;
import com.anthony.service.UserService;
import com.anthony.service.impl.UserServiceImpl;public class RegServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("UTF-8");resp.setContentType("text/html;charset=UTF-8");//获取表单数据User user = new User();try {BeanUtils.populate(user, req.getParameterMap());//调用业务层方法/逻辑UserService us = new UserServiceImpl();us.register(user);} catch (Exception e) {e.printStackTrace();}//分发转向resp.getWriter().write("注册成功,2秒之后跳转到主页");resp.setHeader("refresh", "2;url="+req.getContextPath()+"/index.jsp");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req,resp);}}
web.xml配置如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"><display-name>J2EE_Demo</display-name><servlet><servlet-name>regServlet</servlet-name><servlet-class>com.anthony.web.servlet.RegServlet</servlet-class></servlet><servlet-mapping><servlet-name>regServlet</servlet-name><url-pattern>/servlet/regServlet</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list>
</web-app>
部署之后,访问index.jsp然后点击注册
点击注册之后发现报500错误,指向了这行代码错误。
BeanUtils.populate(user, req.getParameterMap());
网上查询得到commons-beanutils-1.9.3.jar,使用这个1.9.3版本的jar就会出这个异常,换成低版本就没有问题
重启tomcat服务器,然后重新打开浏览器,走注册流程就可以成功注册。为了验证注册成功,我本地查询下MySQL表看看用户是否真的添加进来了。
发现网页提示注册成功,但是数据库表没有数据,而且后台报错了,出了一个异常。
五月 29, 2019 4:11:46 下午 org.apache.commons.beanutils.converters.DateConverter toDate
警告: (N.B. Re-configure Converter or use alternative implementation)
org.apache.commons.beanutils.ConversionException: DateConverter does not support default String to 'Date' conversion.
修改RegServlet.java代码如下
package com.anthony.web.servlet;import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;import com.anthony.domain.User;
import com.anthony.service.UserService;
import com.anthony.service.impl.UserServiceImpl;public class RegServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("UTF-8");resp.setContentType("text/html;charset=UTF-8");//获取表单数据User user = new User();try {//解决日期是字符串的异常ConvertUtils.register(new Converter() {public Object convert(Class type, Object value) {Date date1 = null;if(value instanceof String) {String date = (String)value;SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");try {date1 = sdf.parse(date);} catch (ParseException e) {e.printStackTrace();}}return date1;}}, Date.class);BeanUtils.populate(user, req.getParameterMap());//调用业务层方法/逻辑UserService us = new UserServiceImpl();us.register(user);} catch (Exception e) {e.printStackTrace();}//分发转向resp.getWriter().write("注册成功,2秒之后跳转到主页");resp.setHeader("refresh", "2;url="+req.getContextPath()+"/index.jsp");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req,resp);}}
上面解决date日期和字符串转换的异常,利用下面代码一行搞定,已经帮我们封装好了这个代码。
package com.anthony.web.servlet;import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;import com.anthony.domain.User;
import com.anthony.service.UserService;
import com.anthony.service.impl.UserServiceImpl;public class RegServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("UTF-8");resp.setContentType("text/html;charset=UTF-8");//获取表单数据User user = new User();try {//解决日期是字符串的异常ConvertUtils.register(new DateLocaleConverter(), Date.class);BeanUtils.populate(user, req.getParameterMap());//调用业务层方法/逻辑UserService us = new UserServiceImpl();us.register(user);} catch (Exception e) {e.printStackTrace();}//分发转向resp.getWriter().write("注册成功,2秒之后跳转到主页");resp.setHeader("refresh", "2;url="+req.getContextPath()+"/index.jsp");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req,resp);}}
再次注册就可以成功,查询数据库就插入数据进来了。本篇先不管两个密码输入不一致的校验。
这篇关于JavaWeb学习-MVC基础开发系列-2-用户注册功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!