java中的servlet问题
public class onlinecounter{
private static long online=0
public static long getonline(){
return online;
}
public static void raise(){
online++;
}
public static void reduce(){
online--;
}
}
import javax.servlet.http.Httpsessionevent;
import java.servlet.http.HttpsessionLIstener;
public class onlineconuntlistener implements HttpSessionListener{
public void sessionCreated(HttpSessionEvent hse){
onlinecounter.rasie();
}
public void sessionDestroy(HttpsessionEvent hse){
onlinecounter.reduce();
}
jsp:文件
<% page language=java pageEncodeing="gb2312"%>
<%page import="myservlet.onlinecounter%>
<html>
<body>
在线人数<%=onlinecounter.getonline()%>
</body>
</html>
我的问题是这个三个文件时如何联系起来的,在jsp文件时如何得出正确结论的,即servlet是如何实现功能的,这里又没有main方法,那么他是如何实现联系
追问:myservlet.onlinecounter是一个类呀,没跟servlet产生联系呀,者产生联系时什么回事
其他:<%page import="myservlet.onlinecounter%>这个起的作用,每当有一个人登录(即打开jsp页面)时,Web服务器会自动调用Servlet中的sessionCreated()方法,然后再调用类onlinecounter中的rasie()方法改变online的值,再在页面调用了onlinecounter.getonline()方法来获取online的值,最后在客户端Web页面显示online的值。 当你输入一个网址之后,就是对服务器发送了一个请求(http协议)。服务器接受到请求后会解析请求,然后根据请求调用指定的servlet,servlet处理业务逻辑之后会生成结果,然后将结果填充到你写好的jsp模板中,生成一个结果页面,然后返回给浏览器。 在servlet中使用request.setAttribute(“”, ); request.getRequestDispatcher(jsp路径).forward(request, response);
在jsp中使用<%=request.getAttibute("")%>得到
上一个:我是学java的 而且是新手 想做一个小游戏 魔塔 网上那些flash 想找个人教
下一个:我手机是腾信Q3(国产学生机),里面支持java 但是安装后进入游戏时,读取进度条的时候他就不动了,