好友
阅读权限10
听众
最后登录1970-1-1
|
package Binggan;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CookieServer extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
Cookie cookie=null;
Cookie[] cookies = request.getCookies();
if (cookies!=null) {
for (Cookie cookie2 : cookies) {
if(cookie2.getName().equals("name")){
cookie=cookie2;
}
}
}
if(cookie==null){
cookie=new Cookie("name", "1");
response.addCookie(cookie);
response.getWriter().println("第一次来");
}else{
Integer valueOf = Integer.valueOf( cookie.getValue());
valueOf++;
response.getWriter().println("你是第"+valueOf+"次来");
cookie=new Cookie("name", valueOf+"");
response.addCookie(cookie);
}
//Cookie 只能存放String (key,value)
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
|
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|