简体   繁体   English

如何使用servlet映射显示在线用户

[英]how to show online user using servlet mapping

i want to show online users using this servlet ...... 我想显示使用此servlet的在线用户......

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import javax.servlet.ServletContext;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.chatapp.useroperation.Client;

@WebServlet(name = "onlineUsersServlet", urlPatterns = { "/getOnlineUsersList" })
public class ListOfOnlineUsers extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String commaSepeparatedStr ="";
        ServletContext appScope = request.getServletContext();
        String channel = request.getParameter("channel");
        final Map<String, List<Client>> clients = (Map<String, List<Client>>) appScope.getAttribute(LoginServlet.CLIENTS);
        System.out.println(clients);
        if(clients.size()> 0){
            final List<Client> onlineClients = clients.get(channel);
            if(onlineClients !=null){
                for (Client client : onlineClients) {
                    if(commaSepeparatedStr.equals("") ){
                        commaSepeparatedStr = client.getUserName();
                    }else{
                        commaSepeparatedStr =commaSepeparatedStr+","+ client.getUserName();

                    }
                }
            }
        }
        response.getWriter().write(commaSepeparatedStr);
        response.flushBuffer(); 

    }

}

how can i pass value to this servlet from a jsp so that it store the username in its list......is it possible to put value in that servlet from session. 我如何从jsp将值传递给该servlet,以便它将用户名存储在其列表中……是否有可能从会话中将值放入该servlet。

If you want to Store a Value from a session to a servlet, just attach an attribute to the Session; 如果要将会话中的值存储到servlet中,只需将属性附加到会话中即可;

During Login, get the username and store the value into the Session; 登录期间,获取用户名并将其值存储到会话中;

HttpSession sess = request.getSession();//Create new Session
//Get the username from login input
String username = request.getParameter("name");

//Attach the name to the Session.

sess.setAttribute("username", username);

Get the value anytime as long as the session is active. 只要会话处于活动状态,即可随时获取值。

HttpSession sess = request.getSession(false);//Use the current Session
//Get the value fron the Session 
String username = (String) sess.getAttribute("username");//get the Attribute Username

You need to have Attached the Attribute to the Session before you can get it this way. 您需要先将属性附加到会话,然后才能以这种方式获取它。

in your jsp do something like this: 在您的jsp中执行以下操作:

<form action="/YOURWEBAPPNAME/onlineUsersServlet/getOnlineUsersList" method="get">
    <input type="text" name="test" value="Hello World">
    <input type="submit" value="Send">
</form>

in you doGet method do this: 在您的doGet方法中执行以下操作:

String userInput = request.getParameter("test");

and feel free using that stuff. 并随意使用这些东西。

put that stuff in the session with: 通过以下方式将这些内容放入会话中:

request.getSession(false).setAttribute("input",userInput);

and read it with: 并阅读:

 String lInput = (String) request.getSession(false).getAttribute("input");

There are variables with a different scope that you can, or not, access from different places in your code. 您可以在代码的不同位置访问或不访问具有不同范围的变量。 In JavaEE there are variables with request, session and application scope. 在JavaEE中,变量具有请求,会话和应用程序范围。

The request scope means that you can set it and use it in all your classes for the current request and that's what you seem to need right now. 请求范围意味着您可以设置它并将其用于当前请求的所有类中,而这正是您现在似乎需要的。

I'm sorry I can't help you more right now but with this info Google or the SO search box should be your friend. 很抱歉,我现在无法为您提供更多帮助,但是有了此信息,Google或SO搜索框应该是您的朋友。 I'll add details later. 稍后再添加详细信息。

Edit - 编辑-

Stefan beike's answer has these details I'm talking about. 我正在谈论斯蒂芬·贝克的答案有这些细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM