1.用filter解决汉字编码及禁止页面缓存 (不用再在每个页面里去加那几行代码了):
package beanservlettest;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class servfilter extends HttpServlet implements Filter {
private FilterConfig filterConfig;
//Handle the passed-in FilterConfig
public void init(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}
//Process the request/response pair
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) {
try {
request.setCharacterEncoding("GB2312");//设置编码
((HttpServletResponse)response).setHeader("Pragma","No-cache");
((HttpServletResponse)response).setHeader("Cache-Control","no-cache");
((HttpServletResponse)response).setHeader("Expires","0");//禁止缓存
//自己添加的代码就这上面四行,其它的都是Jbuilder自动生成的:)
filterChain.doFilter(request, response);
}
catch(ServletException sx) {
filterConfig.getServletContext().log(sx.getMessage());
}
catch(IOException iox) {
filterConfig.getServletContext().log(iox.getMessage());
}
}
//Clean up resources
public void destroy() {
}
}
下面是在web.xml里的配置:
(我的是Jbuilder自动生成的)
<filter>
<filter-name>servfilter</filter-name>
<filter-class>beanservlettest.servfilter</filter-class>
</filter>
<filter-mapping>
<filter-name>servfilter</filter-name>
<url-pattern>/*</url-pattern> ///这里表示对整个网站都有效
</filter-mapping>
2.用listener实现在线人数统计.
package beanservlettest;//除了其中三行代码,其余均由Jbuilder自动生成
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class sessioncount extends HttpServlet implements ServletContextListener, ServletContextAttributeListener, HttpSessionListener, HttpSessionAttributeListener {
private static int activeSessions = 0;
public void contextInitialized(ServletContextEvent sce) {
}
public void contextDestroyed(ServletContextEvent sce) {
}
public void attributeAdded(ServletContextAttributeEvent scab) {
}
public void attributeRemoved(ServletContextAttributeEvent scab) {
}
public void attributeReplaced(ServletContextAttributeEvent scab) {
}
public void sessionCreated(HttpSessionEvent se) {
activeSessions++;//每创建一个新的session就 加1
}
public void sessionDestroyed(HttpSessionEvent se) {
if(activeSessions > 0) //每销毁一个新的session就 减1
activeSessions--;
}
public void attributeAdded(HttpSessionBindingEvent se) {
}
public void attributeRemoved(HttpSessionBindingEvent se) {
}
public void attributeReplaced(HttpSessionBindingEvent se) {
}
public static int getActiveSessions() {
return activeSessions;
}
}
web.xml里的配置如下:
<listener>
<listener-class>beanservlettest.sessioncount</listener-class>
</listener>
jsp测试页面:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="beanservlettest.sessioncount" %>
<html>
<head>
<title>
sc
</title>
</head>
<body bgcolor="#ffffff">
<h1>
JBuilder Generated JSP
</h1>
当前在线人数有<%=sessioncount.getActiveSessions()%>人。
</body>
</html>
<a href= "logout.jsp">注销</a>
网页连接:
http://expert.csdn.net/Expert/topic/1788/1788525.xml?temp=.2403833
http://www.chinaunix.net/bbsjh/14/397.html
2003-7-23 12:44:00
查看评语 ?
2003-7-23 16:45:15 刚才找到了其它程序的一个错误,总算解决了。在xslt文件里:
把<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
写成了:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/TR/WD-xsl">
//这是当时从 http://expert.csdn.net/expert/Xsl/2.xsl里copy的,
//我 晕:(
IE6里能够正常解析,
而java的JAXP却老是出错:
"javax.xml.transform.TransformerException: stylesheet 要求属性:version"
幸好 我突然想到找另外个xsl文件来对照了一下。
把这句话一修改,程序马上执行就成功了。
程序代码:
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
第一种:
/*
try{
File xmlFile = new File("C:\\xmltest\\tiezi.xml");
File xsltFile = new File("C:\\xmltest\\tiezi.xsl");
javax.xml.transform.Source xmlSource =
new javax.xml.transform.stream.StreamSource(xmlFile);
javax.xml.transform.Source xsltSource =
new javax.xml.transform.stream.StreamSource(xsltFile);
javax.xml.transform.Result result =
new javax.xml.transform.stream.StreamResult(System.out);
//创建一个TransformerFactory 实例
javax.xml.transform.TransformerFactory transFact =
javax.xml.transform.TransformerFactory.newInstance();
javax.xml.transform.Transformer trans =
transFact.newTransformer(xsltSource);
trans.transform(xmlSource, result);
return "转换成功了!!!";
}
catch(Exception e){
System.out.println("出错");
return e.toString()+"出错了:(";
}
*/
第二种:
/* try {
TransformerFactory transformerFactory =
TransformerFactory.newInstance();
StreamSource source = new StreamSource("C:\\xmltest\\tiezi.xml");
StreamResult result = new StreamResult("C:\\xmltest\\result.xml");
StreamSource style = new StreamSource("C:\\xmltest\\tiezi.xsl");
Transformer transformer =
transformerFactory.newTransformer(style);
transformer.transform(source, result);
return "OK";
} catch (Exception e) {
e.printStackTrace();
return "Failed";
}
*/