我开发了一个 servlet,用户在其中输入名称,并在单击提交按钮时向用户致意,我还想添加它应该显示当前登录的用户数量的功能,例如如果我从一个浏览器应该显示登录的用户数是 1,因为我是第一个用户,如果其他人从下一个浏览器点击 servlet,它应该显示他是第 2 个访问者,所以它会显示他的数字当前登录的用户,请告知如何实现这一目标..
该项目的 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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>FirstDemo</display-name>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.saral.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/helloServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>home.html</welcome-file>
</welcome-file-list>
</web-app>
HTML是...
<html>
<head>
<title> A Simple web Application</title>
</head>
<body>
<form method="get" action="helloServlet">
Name<input type="text" name="txtName">
<br><input type="submit" value="Login">
</form>
</body>
</html>
请告知如何添加登录用户数的功能。下面是我的项目结构的快照是..在此处输入链接描述
我在下面添加了计数器类,如图所示......
package com.saral;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class SessionCounter implements HttpSessionListener
{
private static int count;
public SessionCounter()
{
}
public void sessionCreated(HttpSessionEvent arg0)
{
count++;
ServletContext sContext = arg0.getSession().getServletContext();
synchronized (sContext)
{
sContext.setAttribute("sessionCount", new Integer(count));
}
}
public void sessionDestroyed(HttpSessionEvent arg0)
{
count--;
ServletContext sContext = arg0.getSession().getServletContext();
synchronized (sContext)
{
sContext.setAttribute("sessionCount", new Integer(count));
}
}
}
我也将我的servlet修改为这个..
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class First
*/
//@WebServlet("/First")
public class MyServlet 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 {
// TODO Auto-generated method stub
String name=request.getParameter("txtName");
response.setContentType("text/html");
request.getAttribute("sessionCount");
PrintWriter out=response.getWriter();
out.println("Hello,"+name);
out.println("<br> this output is generated by a simple servlet.");
out.println("--->"+(request.getAttribute("sessionCount")));
out.close();
}
}
但是我得到的会话是空的……在输出中……http://imageshack.us/photo/my-images/405/sessionoutput.jpg/会话的输出
请告知我如何获得那里的价值我现在得到空仪式,我希望我首先请求它应该显示 1,如果我打开第二个它应该显示 2,请告诉我如何实现这一点,因为我坚持这个.