在servlet中添加用户计数器的功能

时间:2012-07-22 14:20:33

标签: java java-ee tomcat servlets

我在下面开发了一个servlet,其逻辑是如果name等于saral saxena然后它移动到下一页,否则不行,我也在我的代码下面发布,我想要一个改变是它也应该是你访客号码也是如此,如果来自不同浏览器的两个不同用户点击相同的网址,那么对于第二个网址,它应该显示您是第2个访问者。下面是我目前的一段代码..

  package trial;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SendRedirectServlet
 */
public class SendRedirectServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse
            response)throws ServletException, IOException {
        //System.out.println("*************");

        response.setContentType("text/html");
          PrintWriter pw = response.getWriter();
          String name = request.getParameter("username");
          pw.println("<br> this output is generated by a simple servlet.");
         String password = request.getParameter("password");
          if(name.equals("saral")&& password.equals("saxena")){
              response.sendRedirect("/ValidUserServlet");
          }
          else{
          pw.println("u r not a valid user");
          }

        }
}

另一个servlet是......

    ackage trial;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ValidUserServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


         PrintWriter pw = response.getWriter();
          pw.println("Welcome to roseindia.net " + " ");
          pw.println("how are you");


    }


}

现在我的web.xml仪式是......

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

  <servlet>
   <servlet-name>s1</servlet-name>
   <servlet-class>trial.SendRedirectServlet</servlet-class>
  </servlet>
  <servlet-mapping>
   <servlet-name>s1</servlet-name>
   <url-pattern>/SendRedirectServlet</url-pattern>
  </servlet-mapping>

    <servlet>
   <servlet-name>s2</servlet-name>
   <servlet-class>trial.ValidUserServlet</servlet-class>
    </servlet>
  <servlet-mapping>
    <servlet-name>s2</servlet-name>
    <url-pattern>/ValidUserServlet</url-pattern>
  </servlet-mapping>
    <welcome-file-list>
<welcome-file>home.html</welcome-file>
</welcome-file-list>

</web-app>

最后我的html页面是......

    <html>

<head>
<title>New Page 1</title>
</head>

<body>

<form method="post" action="/SendRedirectServlet">
  <p>Enter your name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
   <input type="text" name="username" size="20"/></p>
  <p>Enter your password&nbsp; <input type="text" name="password"
   size="20"/></p>
  <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  &nbsp;&nbsp;
  <input type="submit" value="Submit" name="B1"/></p>
</form>

</body>

</html>

项目结构的快照是..

http://imageshack.us/photo/my-images/707/49690689.jpg/

请告知如何添加您是访客号码的功能

伙计们请指教。

1 个答案:

答案 0 :(得分:1)

1。点击容器的请求是非每个用户,但每个请求一个,即使它来自同一个浏览器。

2。 service() 方法每次请求到达并点击容器时,请将计数器放在service()方法中进行计数访问过您网站的用户数。

<强> //////////// EDITED ////////////////

现在,当我展示 HttpServlet 的示例时,该方法将是doGetdoPost ,而不是 service()此处 doGet()

以下是来自roseindia.com

的示例
public class CounterServlet extends HttpServlet{
  public void doGet(HttpServletRequest request, 
  HttpServletResponse response)
  throws ServletException, IOException {
  HttpSession session = request.getSession(true);
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  Integer count = new Integer(0);
  String head;
  if (session.isNew()) {
  head = "This is the New Session";
  } else {
  head = "This is the old Session";
  Integer oldcount =(Integer)session.getValue("count"); 
  if (oldcount != null) {
  count = new Integer(oldcount.intValue() + 1);
  }
  }
  session.putValue("count", count);
  out.println("<HTML><BODY BGCOLOR=\"#FDF5E6\">\n" +
  "<H2 ALIGN=\"CENTER\">" + head + "</H2>\n" + 
  "<TABLE BORDER=1 ALIGN=CENTER>\n"
  + "<TR BGCOLOR=\"#FFAD00\">\n" 
  +"  <TH>Information Type<TH>Session Count\n" 
  +"<TR>\n" +" <TD>Total Session Accesses\n" +
  "<TD>" + count + "\n" +
  "</TABLE>\n" 
  +"</BODY></HTML>" );
  }
}