?? cookieservlet.java
字號(hào):
/*
* Created on 2004-6-13
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package com.servlet;
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;
/**
* @author haoyulong
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class CookieServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//TODO Method stub generated by Lomboz
Cookie cookie = null;
//獲取請(qǐng)求相關(guān)的cookie
Cookie[] cookies = request.getCookies( );
boolean newCookie = false;
//判斷Cookie ServletStudy是否存在
if (cookies != null){
for (int i = 0; i < cookies.length; i++){
if (cookies[i].getName( ).equals("sevlet")){
cookie = cookies[i];
}
}//end for
}//end if
if (cookie == null){
newCookie=true;
int maxAge=10000;
//生成cookie對(duì)象
cookie = new Cookie("ServletStudy","create by hyl");
cookie.setPath(request.getContextPath( ));
cookie.setMaxAge(maxAge);
response.addCookie(cookie);
}//end if
// 顯示信息
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter( );
out.println("<html>");
out.println("<head>");
out.println("<title>Cookie Info</title>");
out.println("</head>");
out.println("<body>");
out.println(
"<h2> Information about the cookie named \"ServletStudy\"</h2>");
out.println("Cookie value: "+cookie.getValue( )+"<br>");
if (newCookie){
out.println("Cookie Max-Age: "+cookie.getMaxAge( )+"<br>");
out.println("Cookie Path: "+cookie.getPath( )+"<br>");
}
out.println("</body>");
out.println("</html>");
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -