1

我在下面开发了一个 servlet,其中逻辑是如果名称等于 saral saxena,那么它会移动到下一页,否则不会,我也在我的代码下面发布,我想要一个更改是它也应该是访客编号这样,如果来自不同浏览器的两个不同用户点击相同的 url,那么对于第二个,它应该显示您是 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/

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

伙计们请指教。

4

1 回答 1

1

1.到达容器的请求不是每个用户,而是每个请求一个,即使它来自同一个浏览器。

2. service() 每次请求到达并命中 Container 时都会调用该方法,因此在 service() 方法中放置一个计数器来计算访问您网站的用户数。

///////////已编辑//////////////

现在,当我展示HttpServlet的示例时,该方法将是doGet或者doPost 代替 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>" );
  }
}
于 2012-07-22T14:26:01.757 回答