Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2535660
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2011-07-11 16:25:06

 
 
  1. import java.io.IOException;
  2. import java.io.PrintWriter;

  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.*;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;


  8. public class SetCookie extends HttpServlet {

  9.     /**
  10.      * Constructor of the object.
  11.      */
  12.     public SetCookie() {
  13.         super();
  14.     }

  15.     /**
  16.      * Destruction of the servlet.

  17.      */
  18.     public void destroy() {
  19.         super.destroy(); // Just puts "destroy" string in log

  20.         // Put your code here

  21.     }

  22.     /**
  23.      * The doGet method of the servlet.

  24.      *
  25.      * This method is called when a form has its tag value method equals to get.
  26.      *
  27.      * @param request the request send by the client to the server
  28.      * @param response the response send by the server to the client
  29.      * @throws ServletException if an error occurred
  30.      * @throws IOException if an error occurred
  31.      */
  32.     public void doGet(HttpServletRequest request, HttpServletResponse response)
  33.             throws ServletException, IOException {

  34.         
  35.         for(int i=0; i<3; i++){
  36.             //缺省的maxAge为-1,表示cookie仅用于当前浏览器

  37.             Cookie cook = new Cookie("Session-Cookie-"+i,"Cookie-Value-S"+i);
  38.             response.addCookie(cook);
  39.             cook = new Cookie("Persistent-Cookie-"+i,"Cookie-Value-P"+i);
  40.             //cookie在一个小时内有效,无论用户是否退出浏览器

  41.             cook.setMaxAge(3600);
  42.             response.addCookie(cook);
  43.         }
  44.         response.setContentType("text/html");
  45.         PrintWriter out = response.getWriter();
  46.         String title = "Setting Cookies..";
  47.         
  48.         
  49.         out.println(""</SPAN><SPAN style="COLOR: #0000cc">+</SPAN>title<SPAN style="COLOR: #0000cc">+</SPAN><SPAN style="COLOR: #ff00ff">""+"

    "+title+"

    "
    );
  50.         
  51.     }

  52.     /**
  53.      * The doPost method of the servlet.

  54.      *
  55.      * This method is called when a form has its tag value method equals to post.
  56.      *
  57.      * @param request the request send by the client to the server
  58.      * @param response the response send by the server to the client
  59.      * @throws ServletException if an error occurred
  60.      * @throws IOException if an error occurred
  61.      */
  62.     public void doPost(HttpServletRequest request, HttpServletResponse response)
  63.             throws ServletException, IOException {

  64.         doGet(request,response);
  65.     }

  66.     /**
  67.      * Returns information about the servlet, such as
  68.      * author, version, and copyright.
  69.      *
  70.      * @return String information about this servlet
  71.      */
  72.     public String getServletInfo() {
  73.         return "This is my default servlet created by Eclipse";
  74.     }

  75.     /**
  76.      * Initialization of the servlet.

  77.      *
  78.      * @throws ServletException if an error occurs
  79.      */
  80.     public void init() throws ServletException {
  81.         // Put your code here

  82.     }

  83. }
 
 
  1. import java.io.IOException;
  2. import java.io.PrintWriter;

  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.*;


  8. public class ShowSession extends HttpServlet {

  9.     /**
  10.      * Constructor of the object.
  11.      */
  12.     public ShowSession() {
  13.         super();
  14.     }

  15.     /**
  16.      * Destruction of the servlet.

  17.      */
  18.     public void destroy() {
  19.         super.destroy(); // Just puts "destroy" string in log

  20.         // Put your code here

  21.     }

  22.     /**
  23.      * The doGet method of the servlet.

  24.      *
  25.      * This method is called when a form has its tag value method equals to get.
  26.      *
  27.      * @param request the request send by the client to the server
  28.      * @param response the response send by the server to the client
  29.      * @throws ServletException if an error occurred
  30.      * @throws IOException if an error occurred
  31.      */
  32.     public void doGet(HttpServletRequest request, HttpServletResponse response)
  33.             throws ServletException, IOException {

  34.         response.setContentType("text/html");
  35.         PrintWriter out = response.getWriter();
  36.         //Provides a way to identify a user across more than one page request

  37.         //or visit to a Web site and to store information about that user.

  38.         HttpSession session = request.getSession(true);
  39.         Integer accessCount = (Integer)session.getAttribute("accessCount");
  40.         String heading;
  41.         if(accessCount == null){
  42.             accessCount = new Integer(0);
  43.             heading = "Welcome, the firt time see you..";
  44.         }else{
  45.             heading ="Welcome Back..";
  46.             accessCount = new Integer(accessCount.intValue() + 1);
  47.             
  48.         }
  49.         
  50.         session.setAttribute("accessCount", accessCount);
  51.         out.println("");
  52.         out.println("");
  53.         out.println(" A Servlet");
  54.         out.println(" ");
  55.         out.println(" " +heading +"
    "
    );
  56.         out.println(session.getId()+"--"+ session.getLastAccessedTime()+"--"+session.getAttribute("accessCount"));
  57.         out.println(" ");
  58.         out.println("");
  59.         out.flush();
  60.         out.close();
  61.     }

  62.     /**
  63.      * The doPost method of the servlet.

  64.      *
  65.      * This method is called when a form has its tag value method equals to post.
  66.      *
  67.      * @param request the request send by the client to the server
  68.      * @param response the response send by the server to the client
  69.      * @throws ServletException if an error occurred
  70.      * @throws IOException if an error occurred
  71.      */
  72.     public void doPost(HttpServletRequest request, HttpServletResponse response)
  73.             throws ServletException, IOException {

  74.         doGet(request,response);
  75.     }

  76.     

  77.     /**
  78.      * Initialization of the servlet.

  79.      *
  80.      * @throws ServletException if an error occurs
  81.      */
  82.     public void init() throws ServletException {
  83.         // Put your code here

  84.     }

  85. }
阅读(1732) | 评论(1) | 转发(0) |
0

上一篇:Servlet学习记录1

下一篇:jsp:useBean

给主人留下些什么吧!~~

tianbianfei2011-07-12 16:16:57

牛人,俺刚从java转android,请教高手一个问题:大家快来参与讨论吧:http://doumiw.com/market/community/t!showTopic.do?topicId=24