jsp+servlet+mysql的简单使用

jsp+servlet+mysql的简单使用

一、介绍
该项目是一个简单的注册功能

二、过程
1. 导入包
jsp+servlet+mysql的简单使用_第1张图片

包结构
jsp+servlet+mysql的简单使用_第2张图片

  1. 编写jsp
    在WebContent包下面新建register.jsp

  2. 编写servlet

public class RegisterServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 处理验证码
        String _userCode = req.getParameter("ckcode");
        String _realCode = (String) req.getSession().getAttribute("checkcode_session");
        // 如果两个验证码不一致,则调回注册界面
        if (!_realCode.equals(_userCode)) {
            req.setAttribute("ckcode_msg", "验证码错误");
            req.getRequestDispatcher("/register.jsp").forward(req, resp);
            return;
        }

        // 获取表单数据
        Users _users = new Users();
        _users.setActiveCode(UUID.randomUUID().toString());// 手动设置激活码
        try {
            BeanUtils.populate(_users, req.getParameterMap());

            // 调用业务逻辑
            UserService _uUserService = new UserService();
            _uUserService.regist(_users);

            // 分发转向
            req.getSession().setAttribute("user", _users);// 把用户信息封装到session中
            req.getRequestDispatcher("/registersuccess.jsp").forward(req, resp);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            req.setAttribute("user_msg", e.getMessage());
            req.getRequestDispatcher("/register.jsp").forward(req, resp);
            return;
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

}
  1. 在web.xml中配置,在WEB-INF中新建web.xml

<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>BookStoreProjectdisplay-name>

    
    <filter>
        <filter-name>EncodingFilterfilter-name>
        <filter-class>com.llayjun.bookstore.web.filter.EncodingFilterfilter-class>
    filter>

    <filter-mapping>
        <filter-name>EncodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

    
    <servlet>
        <servlet-name>CheckImgServletservlet-name>
        <servlet-class>com.llayjun.bookstore.web.servlet.CheckImgServletservlet-class>
    servlet>

    <servlet-mapping>
        <servlet-name>CheckImgServletservlet-name>
        <url-pattern>/imageCodeurl-pattern>
    servlet-mapping>

    
    <servlet>
        <servlet-name>RegisterServletservlet-name>
        <servlet-class>com.llayjun.bookstore.web.servlet.RegisterServletservlet-class>
    servlet>

    <servlet-mapping>
        <servlet-name>RegisterServletservlet-name>
        <url-pattern>/registerurl-pattern>
    servlet-mapping>

    <welcome-file-list>
        <welcome-file>register.jspwelcome-file>
    welcome-file-list>
web-app>
  1. 运行效果图
    jsp+servlet+mysql的简单使用_第3张图片

  2. 输入注册信息,同意并提交,在数据库中查看

github代码地址
https://github.com/llayjun/JAVA_WEB_BookStoreProject

你可能感兴趣的:(Java,Web)