Skip to content

Commit 5c3d6b7

Browse files
committed
Signed-off-by: chenhaoxiang <[email protected]>
1 parent f1800d9 commit 5c3d6b7

29 files changed

+465
-0
lines changed

myOnlinesWeb/.classpath

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5+
<classpathentry kind="con" path="melibrary.com.genuitec.eclipse.j2eedt.core.MYECLIPSE_JAVAEE_6_CONTAINER"/>
6+
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
7+
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
8+
<classpathentry kind="output" path="WebRoot/WEB-INF/classes"/>
9+
</classpath>

myOnlinesWeb/.mymetadata

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project-module
3+
type="WEB"
4+
name="myOnlinesWeb"
5+
id="myeclipse.1471626831613"
6+
context-root="/myOnlinesWeb"
7+
j2ee-spec="6.0"
8+
archive="myOnlinesWeb.war">
9+
<attributes>
10+
<attribute name="webrootdir" value="WebRoot" />
11+
</attributes>
12+
</project-module>
13+

myOnlinesWeb/.project

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>myOnlinesWeb</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.wst.common.project.facet.core.builder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>com.genuitec.eclipse.j2eedt.core.WebClasspathBuilder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
<buildCommand>
24+
<name>org.eclipse.jdt.core.javabuilder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
28+
<buildCommand>
29+
<name>com.genuitec.eclipse.j2eedt.core.J2EEProjectValidator</name>
30+
<arguments>
31+
</arguments>
32+
</buildCommand>
33+
<buildCommand>
34+
<name>com.genuitec.eclipse.j2eedt.core.DeploymentDescriptorValidator</name>
35+
<arguments>
36+
</arguments>
37+
</buildCommand>
38+
<buildCommand>
39+
<name>org.eclipse.wst.validation.validationbuilder</name>
40+
<arguments>
41+
</arguments>
42+
</buildCommand>
43+
<buildCommand>
44+
<name>com.genuitec.eclipse.ast.deploy.core.DeploymentBuilder</name>
45+
<arguments>
46+
</arguments>
47+
</buildCommand>
48+
</buildSpec>
49+
<natures>
50+
<nature>com.genuitec.eclipse.ast.deploy.core.deploymentnature</nature>
51+
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
52+
<nature>com.genuitec.eclipse.j2eedt.core.webnature</nature>
53+
<nature>org.eclipse.jdt.core.javanature</nature>
54+
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
55+
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
56+
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
57+
</natures>
58+
</projectDescription>

myOnlinesWeb/.settings/.jsdtscope

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="WebRoot"/>
4+
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
5+
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">
6+
<attributes>
7+
<attribute name="hide" value="true"/>
8+
</attributes>
9+
</classpathentry>
10+
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
11+
<classpathentry kind="output" path=""/>
12+
</classpath>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#Sat Aug 20 01:13:51 CST 2016
2+
eclipse.preferences.version=1
3+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
4+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
5+
org.eclipse.jdt.core.compiler.compliance=1.6
6+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
7+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
8+
org.eclipse.jdt.core.compiler.source=1.6
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project-modules id="moduleCoreId" project-version="1.5.0">
3+
<wb-module deploy-name="myOnlinesWeb">
4+
<wb-resource deploy-path="/" source-path="/WebRoot"/>
5+
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src"/>
6+
<property name="context-root" value="/myOnlinesWeb"/>
7+
<property name="java-output-path" value="/myOnlinesWeb/WebRoot/WEB-INF/classes"/>
8+
</wb-module>
9+
</project-modules>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<faceted-project>
3+
<fixed facet="wst.jsdt.web"/>
4+
<installed facet="java" version="1.6"/>
5+
<installed facet="jst.web" version="3.0"/>
6+
<installed facet="wst.jsdt.web" version="1.0"/>
7+
</faceted-project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.eclipse.wst.jsdt.launching.baseBrowserLibrary
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Window

myOnlinesWeb/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
JavaWeb-Servlet技术的监听器-解析与实例-网站在线用户信息与网页点击量
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Class-Path:
3+
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

myOnlinesWeb/WebRoot/WEB-INF/web.xml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app version="3.0"
3+
xmlns="http://java.sun.com/xml/ns/javaee"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
6+
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
7+
<display-name></display-name>
8+
9+
<filter>
10+
<filter-name>charset</filter-name>
11+
<filter-class>cn.hncu.filter.CharsetFilter</filter-class>
12+
</filter>
13+
<filter-mapping>
14+
<filter-name>charset</filter-name>
15+
<url-pattern>/*</url-pattern>
16+
</filter-mapping>
17+
18+
<listener>
19+
<listener-class>cn.hncu.listeners.MyServletContextListener</listener-class>
20+
</listener>
21+
<listener>
22+
<listener-class>cn.hncu.listeners.MyServletRequestListenr</listener-class>
23+
</listener>
24+
<listener>
25+
<listener-class>cn.hncu.listeners.MyHttpSessionAttributeListener</listener-class>
26+
</listener>
27+
<listener>
28+
<listener-class>cn.hncu.listeners.MyHttpSessionListener</listener-class>
29+
</listener>
30+
31+
32+
<servlet>
33+
<servlet-name>LoginServlet</servlet-name>
34+
<servlet-class>cn.hncu.servlets.LoginServlet</servlet-class>
35+
</servlet>
36+
<servlet>
37+
<servlet-name>ShowServlet</servlet-name>
38+
<servlet-class>cn.hncu.servlets.ShowServlet</servlet-class>
39+
</servlet>
40+
41+
42+
<servlet-mapping>
43+
<servlet-name>LoginServlet</servlet-name>
44+
<url-pattern>/servlet/LoginServlet</url-pattern>
45+
</servlet-mapping>
46+
<servlet-mapping>
47+
<servlet-name>ShowServlet</servlet-name>
48+
<url-pattern>/servlet/ShowServlet</url-pattern>
49+
</servlet-mapping>
50+
51+
52+
53+
<welcome-file-list>
54+
<welcome-file>index.jsp</welcome-file>
55+
</welcome-file-list>
56+
</web-app>

myOnlinesWeb/WebRoot/index.jsp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2+
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
3+
4+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
5+
<html>
6+
<head>
7+
<title>在线人信息与网站点击量的实现</title>
8+
</head>
9+
10+
<body>
11+
<h3>在线人信息与网站点击量的实现</h3>
12+
<a href='<c:url value="servlet/ShowServlet"></c:url>'>查看在线人信息</a>
13+
<hr/>
14+
<form action='<c:url value="servlet/LoginServlet"></c:url>' method="post">
15+
姓名:<input type="text" name="name" />
16+
<input type="submit" value="登录">
17+
</form>
18+
19+
<br/>点击量:${count}
20+
</body>
21+
</html>

myOnlinesWeb/myOnlinesWeb.zip

21.8 KB
Binary file not shown.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cn.hncu.filter;
2+
3+
import java.io.IOException;
4+
5+
import javax.servlet.Filter;
6+
import javax.servlet.FilterChain;
7+
import javax.servlet.FilterConfig;
8+
import javax.servlet.ServletException;
9+
import javax.servlet.ServletRequest;
10+
import javax.servlet.ServletResponse;
11+
12+
public class CharsetFilter implements Filter{
13+
@Override
14+
public void init(FilterConfig filterConfig) throws ServletException {
15+
}
16+
@Override
17+
public void doFilter(ServletRequest request, ServletResponse response,
18+
FilterChain chain) throws IOException, ServletException {
19+
request.setCharacterEncoding("utf-8");
20+
response.setCharacterEncoding("utf-8");
21+
chain.doFilter(request, response);
22+
}
23+
@Override
24+
public void destroy() {
25+
}
26+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cn.hncu.listeners;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import javax.servlet.http.HttpSession;
7+
import javax.servlet.http.HttpSessionAttributeListener;
8+
import javax.servlet.http.HttpSessionBindingEvent;
9+
10+
public class MyHttpSessionAttributeListener implements HttpSessionAttributeListener{
11+
12+
//session添加属性了,就会调用下面的attributeAdded方法
13+
@Override
14+
public void attributeAdded(HttpSessionBindingEvent se) {
15+
if(se.getName().equals("name")){//代表添加了name属性
16+
System.out.println("用户"+se.getValue()+"登录了");
17+
List<HttpSession> logins = (List<HttpSession>) se.getSession().getServletContext().getAttribute("logins");
18+
19+
if(logins==null){
20+
logins = new ArrayList<HttpSession>();
21+
se.getSession().getServletContext().setAttribute("logins", logins);
22+
}
23+
logins.add(se.getSession());
24+
}
25+
}
26+
27+
@Override
28+
public void attributeRemoved(HttpSessionBindingEvent se) {
29+
}
30+
31+
@Override
32+
public void attributeReplaced(HttpSessionBindingEvent se) {
33+
}
34+
35+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package cn.hncu.listeners;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import javax.servlet.http.HttpSession;
7+
import javax.servlet.http.HttpSessionEvent;
8+
import javax.servlet.http.HttpSessionListener;
9+
10+
public class MyHttpSessionListener implements HttpSessionListener {
11+
12+
//有 游客/用户 来访问了
13+
@Override
14+
public void sessionCreated(HttpSessionEvent se) {
15+
List<HttpSession> guests = (List<HttpSession>) se.getSession().getServletContext().getAttribute("guests");//在线人的集合
16+
if(guests==null){//第一个访问网站的人--沙发
17+
guests = new ArrayList<HttpSession>();
18+
se.getSession().getServletContext().setAttribute("guests", guests);//设置guests属性
19+
}
20+
guests.add(se.getSession());//将第一个用户的session添加到在线人集合
21+
}
22+
23+
@Override
24+
public void sessionDestroyed(HttpSessionEvent se) {
25+
List<HttpSession> guests = (List<HttpSession>) se.getSession().getServletContext().getAttribute("fuses");
26+
if(guests.contains(se.getSession())){
27+
guests.remove(se.getSession());
28+
}
29+
}
30+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package cn.hncu.listeners;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
import java.io.FileReader;
7+
import java.io.InputStreamReader;
8+
import java.io.PrintWriter;
9+
10+
import javax.servlet.ServletContext;
11+
import javax.servlet.ServletContextEvent;
12+
import javax.servlet.ServletContextListener;
13+
14+
public class MyServletContextListener implements ServletContextListener{
15+
16+
@Override
17+
public void contextInitialized(ServletContextEvent sce) {
18+
//从服务器硬盘把之前存储的点击量数据读取出来
19+
ServletContext sct = sce.getServletContext();
20+
String path = sct.getRealPath("/count.txt");
21+
22+
try {
23+
BufferedReader br = new BufferedReader(new FileReader(path));
24+
String line = br.readLine();
25+
Integer count = Integer.valueOf(line);
26+
sct.setAttribute("count", count);
27+
} catch (Exception e) {
28+
//如果出异常了,我们认为文件还不存在,服务器第一次启动
29+
e.printStackTrace();
30+
sct.setAttribute("count",new Integer(0));
31+
}
32+
33+
}
34+
35+
//关闭服务器时
36+
@Override
37+
public void contextDestroyed(ServletContextEvent sce) {
38+
//把当前servletContext容器中存储的点击量数据 永久化到 服务器硬盘
39+
ServletContext sct = sce.getServletContext();
40+
String path = sct.getRealPath("/count.txt");
41+
42+
try {
43+
PrintWriter pw = new PrintWriter(path);
44+
pw.write(""+sct.getAttribute("count"));
45+
pw.close();
46+
} catch (FileNotFoundException e) {
47+
e.printStackTrace();
48+
}
49+
50+
51+
}
52+
53+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cn.hncu.listeners;
2+
3+
import javax.servlet.ServletContext;
4+
import javax.servlet.ServletRequestEvent;
5+
import javax.servlet.ServletRequestListener;
6+
7+
public class MyServletRequestListenr implements ServletRequestListener{
8+
9+
//有一个请求就会运行这里
10+
@Override
11+
public void requestInitialized(ServletRequestEvent sre) {
12+
ServletContext sct = sre.getServletContext();
13+
new MyThread(sct).start();
14+
}
15+
16+
@Override
17+
public void requestDestroyed(ServletRequestEvent sre) {
18+
}
19+
}
20+
21+
class MyThread extends Thread{
22+
private ServletContext sct = null;
23+
private static Object obj = new Object();
24+
public MyThread(ServletContext sct) {
25+
this.sct=sct;
26+
}
27+
28+
@Override
29+
public void run() {
30+
synchronized (obj) {
31+
sct.setAttribute("count", (Integer) sct.getAttribute("count") + 1);
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)