国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

Java Se!rvlet 編程及應用之Cookie的使用方法

2019-11-02 14:39:43
字體:
來源:轉載
供稿:網友

    Cookie 是一小塊可以嵌入HTTP 請求和響應中的數據,它在服務器上產生,并作為響應頭域的一部分返回用戶。瀏覽器收到包含Cookie 的響應后,會把Cookie 的內容用“關鍵字/值” 對的形式寫入到一個客戶端專為存放Cookie 的文本文件中。瀏覽器會把Cookie 及隨后產生的請求發給相同的服務器,服務器可以再次讀取Cookie 中存Cookie 可以進行有效期設置,過期的Cookie 不會發送給服務器。

  Servlet API 提供了一個Cookie 類,封裝了對Cookie 的一些操作。Servlet 可以創建一個新的Cookie,設置它的關鍵字、值及有效期等屬性,然后把Cookie 設置在HttpServletResponse 對象中發回瀏覽器,還可以從HttpServletRequest 對象中獲取Cookie。

  編程思路:Cookie 在實際的Servlet 編程中是很廣泛應用,下面是一個從Servlet 中獲取Cookie 信息的例子。

  ShowCookies.java 的源代碼如下:

import javax.servlet.*;
import javax.servlet.http.*;
/**
* <p>This is a simple servlet that displays all of the
* Cookies present in the request
*/
public class ShowCookies extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException
{
// Set the content type of the response
resp.setContentType("text/html;charset=gb2312");
// Get the PrintWriter to write the response
java.io.PrintWriter out = resp.getWriter();
// Get an array containing all of the cookies
Cookie cookies[] = req.getCookies();
// Write the page header
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Cookie Information</title>");
out.println("</head>");
out.println("<body>");
if ((cookies == null) || (cookies.length == 0)) {
out.println("沒有 cookies ");
}
else {
out.println("<center><h1>響應消息中的Cookies 信息 </h1>");
// Display a table with all of the info
out.println("<table border>");
out.println("<tr><th>Name</th><th>Value</th>" + "<th>Comment</th><th>Max Age</th></tr>");
for (int i = 0; i < cookies.length; i++) {
Cookie c = cookies[i];
out.println("<tr><td>" + c.getName() + "</td><td>" +
c.getValue() + "</td><td>" + c.getComment() + "</td><td>" + c.getMaxAge() + "</td></tr>");
}
out.println("</table></center>");
}
// Wrap up
out.println("</body>");
out.println("</html>");
out.flush();
}
/**
* <p>Initialize the servlet. This is called once when the
* servlet is loaded. It is guaranteed to complete before any
* requests are made to the servlet
* @param cfg Servlet configuration information
*/
public void init(ServletConfig cfg)
throws ServletException
{
super.init(cfg);
}
/**
* <p>Destroy the servlet. This is called once when the servlet
* is unloaded.
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 锦屏县| 清水河县| 佳木斯市| 长垣县| 康乐县| 会东县| 金秀| 石狮市| 广饶县| 济南市| 威信县| 宁武县| 固原市| 荣昌县| 水富县| 新昌县| 长宁区| 贵南县| 宜州市| 土默特右旗| 绥阳县| 东宁县| 平昌县| 垣曲县| 黎城县| 高淳县| 奉新县| 增城市| 英超| 怀集县| 安阳县| 巴林左旗| 温泉县| 裕民县| 乌拉特中旗| 三河市| 泸定县| 云林县| 乌拉特前旗| 和林格尔县| 太仓市|