這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)怎么在java中使用servlet監(jiān)聽(tīng)器顯示在線人數(shù),文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
1.創(chuàng)建一個(gè)監(jiān)聽(tīng)器
package com.listener; import javax.servlet.ServletContext; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; //使用監(jiān)聽(tīng)器實(shí)現(xiàn)顯示在線人數(shù) public class MyServletSessionListener implements HttpSessionAttributeListener { @Override public void attributeAdded(HttpSessionBindingEvent event) { // TODO 自動(dòng)生成的方法存根 ServletContext cx = event.getSession().getServletContext();//根據(jù)session對(duì)象獲取當(dāng)前容器的ServletContext對(duì)象 Object objectlogincount = cx.getAttribute("logincount");//獲取容器里面名字為logincount的對(duì)象 String name = event.getName(); if("is".equals(name)){//如果session增加的屬性名字為is,表示成功登陸一個(gè)用戶 //System.out.println("登陸的用戶名是:"+event.getValue()); if(objectlogincount==null){//如果logincount為空,表示是第一個(gè)登陸 cx.setAttribute("logincount", 1); }else{//表示已經(jīng)有人登陸了 int a = Integer.parseInt(objectlogincount.toString());//轉(zhuǎn)換已經(jīng)登陸的人數(shù) a++; cx.setAttribute("logincount", a); } } System.out.println("當(dāng)前登陸的人數(shù)為:"+cx.getAttribute("logincount")); } @Override public void attributeRemoved(HttpSessionBindingEvent event) { // TODO 自動(dòng)生成的方法存根 } @Override public void attributeReplaced(HttpSessionBindingEvent event) { // TODO 自動(dòng)生成的方法存根 } }
2.在web.xml中配置監(jiān)聽(tīng)器
<listener> <listener-class>com.listener.MyServletSessionListener</listener-class> </listener>
3.用LoginServ(servlet)進(jìn)行測(cè)試
package com.serv; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @WebServlet(urlPatterns={"/LoginServ"}) public class LoginServ extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO 自動(dòng)生成的方法存根 String name = req.getParameter("user"); String pwd = req.getParameter("pwd"); if(true){//假設(shè)用get方式提交,所有用戶名密碼都是正確的 HttpSession session = req.getSession(); session.setAttribute("is", name);//setAttribute() 方法添加指定的屬性,并為其賦指定的值。如果這個(gè)指定的屬性已存在,則僅設(shè)置/更改值。 } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO 自動(dòng)生成的方法存根 doGet(req, resp); } }
上述就是小編為大家分享的怎么在java中使用servlet監(jiān)聽(tīng)器顯示在線人數(shù)了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.chinadenli.net,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)站欄目:怎么在java中使用servlet監(jiān)聽(tīng)器顯示在線人數(shù)-創(chuàng)新互聯(lián)
本文網(wǎng)址:http://www.chinadenli.net/article14/dhehge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、軟件開(kāi)發(fā)、自適應(yīng)網(wǎng)站、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、Google、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容