HttpSession与URL重写

package net.cmono.ch4;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * Servlet implementation class Counter
 */
@WebServlet("/counter")
public class Counter extends HttpServlet {
    private static final long serialVersionUID = 1L;
          
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Counter() {
        super();
        // TODO Auto-generated constructor stub
    }
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html; charset=UTF-8");
           
        PrintWriter writer = response.getWriter();
           
        int count = 0;
           
        HttpSession session = request.getSession();
           
        if(session.getAttribute("count") != null){
            Integer c = (Integer)session.getAttribute("count");
            count = c + 1;
        }
           
        session.setAttribute("count", count);
           
        writer.println("<html>");
        writer.println("<head>");
        writer.println("<title>HeaderServlet</title>");
        writer.println("</head>");
        writer.println("<body>");
        writer.println("<h1> Servlet Count " + count + "</h1>");
        writer.println("<a href=\"" + response.encodeURL("counter") + "\">递增</a>");
        writer.println("</body>");
        writer.println("</html>");
           
    }
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }
}


知识共享许可协议
《HttpSession与URL重写》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
相邻依据:发表时间
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     167.60ms