Servlet 验证 ValidateLogin

来源:互联网 发布:6s蜂窝移动数据选项 编辑:程序博客网 时间:2024/05/10 16:25
package com.xuankai.servlet;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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

public class ValidateLogin extends HttpServlet
{
    public void doGet(HttpServletRequest req,HttpServletResponse resp)
        throws ServletException,IOException
    {
        this.process(req,resp);
    }
    
    @Override
    public  void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
    {
        this.process(req,resp);
    }
    
    public void process(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException,IOException
    {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String gender = req.getParameter("gender");
        
        //转换gender的字符编码

        
        
        String[] hobbys = req.getParameterValues("hobby");
        String address = req.getParameter("address");
        String comment = req.getParameter("comment");
        
        comment = new String(comment.getBytes("iso-8859-1"),"utf-8");
        
        List<String> list = new ArrayList<String>();
        
        if(null==username||"".equals(username))
        {
            list.add("用户名不能为空!");
        }
        
        else if(username.length()<4)
        {
            list.add("用户名太短!");
        }
        
        else if(username.length()>10)
        {
            list.add("用户名太长!");
        }
        
        if(null==password||"".equals(password))
        {
            list.add("密码不能为空!");
        }
        else if(password.length()<4)
        {
            list.add("密码太短!");
        }
        else if(password.length()>10)
        {
            list.add("密码太长!");
        }
        
        if(null==gender)
        {
            list.add("选择性别!");
        }    
        
        if(null==hobbys)
        {
            list.add("必需选一个兴趣!");
        }
        else if(hobbys.length>3)
        {
            list.add("兴趣最多选三个!");
        }
        
        if(null==comment||"".equals(comment))
        {
            list.add("评论不能为空!");
        }
    
        
        if(list.isEmpty())
        {
            req.setAttribute("username",username);
            req.setAttribute("password",password);
            req.setAttribute("gender",gender);
            req.setAttribute("hobbys",hobbys);
            req.setAttribute("address",address);
            req.setAttribute("comment",comment);
            
            req.getRequestDispatcher("isLogin.jsp").forward(req,resp);
        }
        
        
        else
        {
            req.setAttribute("error",list);
            
            req.getRequestDispatcher("isError.jsp").forward(req,resp);
        }
        
    }
}


原创粉丝点击