package com.itheima.cookie.app2;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//产生登录界面
//获取特定的cookie
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
//获取特定Cookie
String username="";
String checked="";
Cookie []cs=request.getCookies();
for(int i=0;cs!=null&&i<cs.length;i++){
if(Constants.USERINFO.equals(cs[i].getName())){
//找到啦
username=cs[i].getValue();
checked="checked='checked'";
}
}
out.write("<form action='"+request.getContextPath()+"/servlet/ResultServlet' method='post'>");
out.write("用户名:<input type='text' name='username' value='"+username+"'/><br/>");
out.write("密码:<input type='password' name='password' value=''/><br/>");
out.write("<input type='checkbox' name='remember'"+checked+" />记住用户名<br/>");
out.write("<input type='submit' value='登录'/><br/>");
out.write("</form>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
|
|