📄 testcookie.java~25~
字号:
package com.test;
import javax.servlet.http.*;
import javax.servlet.ServletConfig;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import java.util.Enumeration;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2008</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class TestCookie extends HttpServlet {
public TestCookie() {
}
public void doPost(HttpServletRequest req,
HttpServletResponse resp) throws ServletException,
IOException{
req.setCharacterEncoding("GBK");
resp.setCharacterEncoding("GBK");
PrintWriter out = resp.getWriter();
Cookie[] ck = req.getCookies();
String username = null;
String password = null;
if(ck!=null){
for (int i = 0; i < ck.length; i++) {
if(ck[i].getName().equals("username")){
username = ck[i].getValue();
}
if(ck[i].getName().equals("password")){
password = ck[i].getValue();
}
}
}
if(username==null&&password==null){
Cookie ck1 = new Cookie("username","wyg");
ck1.setMaxAge(24*60*60);
Cookie ck2 = new Cookie("password","wygtest");
out.print("第一次来,没有cookie");
resp.addCookie(ck1);
resp.addCookie(ck2);
}else{
out.print("<html><head><title>cookie的使用</title></head>");
out.print("<body><form action=\"\">"+
"<input type = text name=username value="+username+">"+
"<input type = text name=password value="+password+">"+
"<input type = submit value=提交 >"+
"</form></body>");
out.print("</html>");
}
}
public void doGet(HttpServletRequest req,
HttpServletResponse resp) throws ServletException,
IOException{
this.doPost(req,resp);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -