⑴ 如何實現java 用戶在線實時統計
您好,這樣:
通過SessionListenr可以監聽session的創建和銷毀,所以首先要寫一個類MySessionListener,實現javax.servlet.http.HttpSessionListener介面及其sessionCreated()、sessionDestroyed()方法:
import java.util.HashSet;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class MySessionListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent event) {
HttpSession session = event.getSession();
ServletContext application = session.getServletContext();
// 在application范圍由一個HashSet集保存所有的session
HashSet sessions = (HashSet) application.getAttribute("sessions");
if (sessions == null) {
sessions = new HashSet();
application.setAttribute("sessions", sessions);
}
// 新創建的session均添加到HashSet集中
sessions.add(session);
// 可以在別處從application范圍中取出sessions集合
// 然後使用sessions.size()獲取當前活動的session數,即為「在線人數」
}
public void sessionDestroyed(HttpSessionEvent event) {
HttpSession session = event.getSession();
ServletContext application = session.getServletContext();
HashSet sessions = (HashSet) application.getAttribute("sessions");
// 銷毀的session均從HashSet集中移除
sessions.remove(session);
}
}
然後再在web.xml中分別配置SessionListener和session超時時間(10分鍾):
<listener>
<listener-class>全路徑MySessionListener</listener-class>
</listener>
<session-config>
<session-timeout>10</session-timeout>
</session-config>
最後在Jsp頁面代碼使用以下代碼就可以實現當前在線人數統計輸出:
⑵ 如何用java實現在線功能呀
用戶登陸時, 向標記為在線的好友發探測消息
離線的時候也向在線好友發離去的消息
⑶ javaweb怎麼實現在線視頻播放
<video src="a.mp4" width="320" height="240" controls="controls">
Your browser does not support the audio tag.
</video>
⑷ java在線提問
class manager extends employee{
// 你要寫的
}
⑸ 用java寫,在線
publicclassBook{
privateStringtitle;
privateStringauthor;
private doubleprice;
publicBook(){
}
publicBook(Stringtitle,Stringauthor){
this.title=title;
this.author=author;
}
publicBook(Stringtitle,Stringauthor,doubleprice){
this.title=title;
this.author=author;
this.price=price;
}
publicvoidinfo(){
System.out.println("書名:"+title);
System.out.println("作者:"+author);
System.out.println("價格:"+price);
}
}
⑹ JAVA求在線編程
1.
public class Student {
private String name;
private String number;
private int age;
private boolean sex;
private double score;
public Student(String name ,int age,boolean sex,double score){
this.name=name;
this.age=age;
this.sex=sex;
this.score=score;
}
public String show(){
return name+age+sex+score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
2.
public class TestFood {
public static void main(String[] args) {
Cake c = new Cake(12);
Banana b = new Banana();
c.tast();
b.tast();
}
}
abstract class Food {
public void tast() {
System.out.println("this is food!");
}
public int weight;
}
class Cake extends Food {
public Cake() {
}
public Cake(int weight) {
this.weight = weight;
}
public void tast() {
System.out.println("this is a cake!");
}
}
class Banana extends Food {
public void tast() {
System.out.println("this is a banana!");
}
}
⑺ java在線求助 急
實現了ActionListener介面的話 ,就得實現actionPerformed()方法
jbtn.addActionListener(this);給button加上事件時,當觸發某些事件時,就自動調用actionPerformed()方法。
⑻ JavaWeb開發網站,監測用戶在線狀態
用ajax吧。
網頁裡面加點javascript代碼,在後台每120秒和伺服器端通信一次。失敗的話,表示客戶端退出了
不過這樣,伺服器並發壓力很大。
⑼ 有沒有能夠在線java編程的網站
http://www.codeceo.com/article/top-10-online-compiler.html
這里有10個,按自己喜歡挑吧
⑽ 我想在線預覽視頻,java後台的response如何設置才能直接播放而非下載呢
有video標簽