现在的位置: 首页 > 综合 > 正文

Servlet学习笔记(二)—–计数器实例

2013年08月15日 ⁄ 综合 ⁄ 共 1193字 ⁄ 字号 评论关闭

一.在服务器记录网站被点击数,并显示在客户浏览器上

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.util.*;

 

public class hitcountServlet extends HttpServlet

{

       static int count;

       //Servlet的所有初始化代码,在第一次装入servlet时被调用

       public void init(ServletConfig config) throws ServletException

       {

              super.init(config);

       }

       //接受客户请求,缺省方式是doGet()

       public void doGet(HttpServletRequest req,

                     HttpServletResponse resp)

              throws ServletException,

                     java.io.IOException

   {

     //设置响应客户流览器而发送的内容,这里设置为文本类型

            resp.setContentType("text/html");

//文本输出流

            PrintWriter out=resp.getWriter();

           

            count++;

            out.println("<html>");

            out.println("<head><title>BasicServlet</title></head>");

            out.println("<body>");

            out.println("You are user number "+String.valueOf(count)+" visiting our Web site"+"/n");;

            out.println("</body></html>");

   }

  

  /* public String getServletInfo()

   {

            return "BasicServlet Information";

   }*/

}

 

------服务器端启动J2EE服务器

配置Deploytool,新建New Application,新建Web Component, hitcountwebcontextWebContext,hitcountComponent Alias

 

 

-----在客户浏览器上输入http://127.0.0.1:8000/hitcountwebcontext/hitcount

 

抱歉!评论已关闭.