美文网首页
UserInfoServlet.class

UserInfoServlet.class

作者: 张威先森 | 来源:发表于2018-10-16 18:04 被阅读0次

    package com.controller;

    import java.io.IOException;

    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import com.model.bean.UserInfo;
    import com.model.service.UserInfoService;

    /**

    • Servlet implementation class UserInfoServlet
      */
      public class UserInfoServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;

      /**

      • @see HttpServlet#HttpServlet()
        */
        public UserInfoServlet() {
        super();
        // TODO Auto-generated constructor stub
        }

      /**

      • @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
        */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

      }

      /**

      • @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
        */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        System.out.println("进入dopost方法");
        UserInfo user=new UserInfo();
        getParam(request,user);
        String error=validate(user);
        if("".equals(error)){
        //校验通过
        try {
        //校验用户名是否重复
        boolean flag=UserInfoService.getInstance().valiadateUsername(user); //返回true即重复
        if(flag){ //用户名重复 返回该页面 即请求转发
        //请求转发的实现方式
        RequestDispatcher rd=request.getRequestDispatcher("User.html");
        rd.forward(request, response);
        }else{//用户名不重复 跳转页面 即重定向方式
        UserInfoService.getInstance().saveUser(user);
        //重定向的方式跳转到result.html页面
        response.sendRedirect("login.html");
        }
        } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        }else{
        //校验不通过
        //请求转发实现
        RequestDispatcher rd=request.getRequestDispatcher("User.html");
        rd.forward(request, response);
        }

    }
    
    private String validate(UserInfo user) {
        StringBuffer sbf=new StringBuffer("");
        //用户名不能为空
        if(user.getUsername()==null||"".equals(user.getUsername())){
            sbf.append("用户名不为空");
        }
        
        if(user.getPhone()!=null&&user.getPhone().length()!=11){
            sbf.append("电话号码必须是11位");
        }
        return sbf.toString();
    }
    
    private void getParam(HttpServletRequest request, UserInfo user) {
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String newpassword=request.getParameter("newpassword");
        String name=request.getParameter("name");
        String email=request.getParameter("email");
        String phone=request.getParameter("phone");
        String role=request.getParameter("role");
        String skin=request.getParameter("skin");
        
        user.setUsername(username);
        user.setPassword(password);
        user.setNewpassword(newpassword);
        user.setName(name);
        user.setEmail(email);
        user.setPhone(phone);
        user.setRole(role);
        user.setSkin(skin);
        
    }
    

    }

    相关文章

      网友评论

          本文标题:UserInfoServlet.class

          本文链接:https://www.haomeiwen.com/subject/wnahzftx.html