GreetServlet.java

import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
public class GreetServlet extends HttpServlet
{
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
               throws ServletException,IOException
    {
        Cookie[] cookies=req.getCookies();         
        if(null!=cookies && cookies.length!=0)
        {         
            String name=null;
            String pwd=null;
             for(int i=0;i<cookies.length;i++)
             {
                Cookie cookie=cookies[i];
                String cName=cookie.getName();
                if(cName.equals("userinfo"))
                {
                    String cValue=cookie.getValue();
                    String[] userInfo=cValue.split("&");
                    for(int j=0;j<userInfo.length;j++)
                    {
                        String[] value=userInfo[j].split("=");
                        if(value[0].equals("username"))
                        {
                            name=value[1];
                        }
                        if(value[0].equals("password"))
                        {
                            pwd=value[1];
                        }
                    }
                }
            }
            if("majinzhu".equals(name) && "1234".equals(pwd))
            {
                resp.setContentType("text/html;charset=gb2312");
                PrintWriter out=resp.getWriter();
                out.println("<html>");
                out.println("<head><title>欢迎页面</title></head>");
                out.println("<body>");
                out.println(name+",欢迎你");
                out.println("<a href=login2>重新登录</a>");
                out.println("</body></html>");          
                out.close();
                return;
            }
        }
    }
}

你可能感兴趣的:(servlet)