电影网站:www.aikan66.com
项目网站:www.aikan66.com
游戏网站:www.aikan66.com
图片网站:www.aikan66.com
书籍网站:www.aikan66.com
学习网站:www.aikan66.com
Java网站:www.aikan66.com
iOS网站:www.aikan66.com
----
新建web项目,名字web05-CounterServlet
新建servlet,名字CounterServlet
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context=getServletContext(); Integer count = (Integer)context.getAttribute("counter"); if(null==count) { count = new Integer(1); }else{ count = new Integer(count.intValue()+1); } response.setContentType("text/html;charset=utf-8"); PrintWriter out=response.getWriter(); out.println("该页面已经被访问了"+"<b>"+count+"</b>"+"次"); context.setAttribute("counter", count); out.close(); }
----
配置web.xml为
<servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>CounterServlet</servlet-name> <servlet-class>CounterServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CounterServlet</servlet-name> <url-pattern>/product.html</url-pattern> </servlet-mapping>
注意:本servlet中的URL指定的是/product.html,对用户来说是一个静态网页,屏蔽了服务器的实现细节。
----
浏览器访问:http://localhost:8080/web05-CounterServlet/product.html
结果: