下面图老师小编要向大家介绍下Servlet实现访问次数的例子,看起来复杂实则是简单的,掌握好技巧就OK,喜欢就赶紧收藏起来吧!
【 tulaoshi.com - Web开发 】
//ShowTimesServlet.java /*Servlet实现访问次数的例子!*/
import java.io.*;
import java.util.zip.*;
import javax.servlet.*;
import javax.servlet.http.*;
/*
这个类实现访问次数。显示访问次数!看是第几次访问!
*/
public class ShowTimesServlet extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, java.io.IOException {
response.setContentType("text/html");
HttpSession session = request.getSession();
String heading;
Integer accessCount =(Integer)session.getAttribute("accessCount");
if(accessCount == null) {
accessCount = new Integer(0);
heading = "Welcom,You are first time to visit!";
}
else {
heading = "Welcome Backer";
accessCount = new Integer(accessCount.intValue()+1);
}
session.setAttribute("accessCount",accessCount);
PrintWriter out = response.getWriter();
out.println("The title:"+heading);
out.println("Access count: "+accessCount);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, java.io.IOException {
doGet(request,response);
}
}
http://blog.csdn.net/laiahu/archive/2007/01/23/1491695.aspx
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)来源:http://www.tulaoshi.com/n/20160219/1616869.html
看过《Servlet实现访问次数的例子》的人还看了以下文章 更多>>