Md5 算法java实现类
处理URL中文参数问题,对中文参数进行编码和解码
HTTP字符集编码过滤器
IO 操作的工具类
如Global.asa系统事件监听
☆Md5 算法java实现类!
package com.test
import java.security.*;
import java.math.*;
public class TestMd5
{
public String MD5(String sInput) throws Exception{
String algorithm=””;
//输入不能为空
if(sInput.trim()==null){
return “null”;
}
//指定采用MD5算法
try{
algorithm=System.getProperty(“MD5.algorithm”,”MD5″);
}catch(SecurityException se){
}
//定义MessageDigest对象
MessageDigest md=MessageDigest.getInstance(algorithm);
//按照系统缺省的字符编码方式把sInput 转换成字节,并把结果存到一新的字节数组buffer中
byte buffer[]=sInput.getBytes();
//从指定的字节数组buffer的偏移量0开始,用指定的字节数组修改由sInput生成摘要
//count为从 0 开始用的字节数长度。
for(int count=0;count
<%
TestMd5 my = new TestMd5();
try
{
out.println(my.MD5("9999"));
}catch (Exception e){}
%>
☆处理URL中文参数问题,对中文参数进行编码和解码
import org.apache.commons.codec.base64.Base64;
/**
* 提供对 URL 进行处理的工具类
*/
public class URLUtil
{
/**
* 将 URL 参数据进行安全编码
* @param s
* @return
*/
public final static String safeRequestParameter(String s)
{
byte[] b = Base64.encode(s.getBytes());
StringBuffer sb = new StringBuffer();
for(int i = 0; i < b.length; i++)
{
sb.append("%" + Integer.toHexString(b));
}
return sb.toString();
}
/**
* 对 URL 参数据进行安全解码
* @param s
* @return
*/
public final static String decodeSafeRequestParameter(String s)
{
return new String(Base64.decode(s.getBytes()));
}
}
☆HTTP字符集编码过滤器
import javax.servlet.*;
import java.io.IOException;
/**
* 用于设置 HTTP 请求字符编码的过滤器,通过过滤器参数encoding指明使用何种字符编码,用于处理Html Form请求参数的中文问题
* @version 1.0
* @author 陶建风
*/
public class CharacterEncodingFilter
implements Filter
{
protected FilterConfig filterConfig = null;
protected String encoding = “”;
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
{
if(encoding != null)
servletRequest.setCharacterEncoding(encoding);
filterChain.doFilter(servletRequest, servletResponse);
}
public void destroy()
{
filterConfig = null;
encoding = null;
}
public void init(FilterConfig filterConfig) throws ServletException
{
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter(“encoding”);
}
}
☆IO 操作的工具类
import java.io.*;
public final class StreamUtil
{
/**
* 将输入流复制到输出流
* @param is
* @param os
* @throws IOException
*/
public final static void write(InputStream is, OutputStream os) throws IOException
{
BufferedInputStream bis = new BufferedInputStream(is, 1024);
int c = bis.read();
while(c != -1)
{
os.write(c);
c = bis.read();
}
os.flush();
}
/**
* 将输入流按指定的编码转成字符串
* @param is
* @param encoding
* @return
* @throws IOException
*/
public final static String toString(InputStream is, String encoding) throws IOException
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
write(is, bos);
if(encoding == null) return new String(bos.toByteArray());
else return new String(bos.toByteArray(), encoding);
}
/**
* 将输入流按默认的编码转成字符串
* @param is
* @return
* @throws IOException
*/
public final static String toString(InputStream is) throws IOException
{
return toString(is, null);
}
/**
* 关闭输入流,忽略异常
* @param is
*/
public final static void closeQuitely(InputStream is)
{
try
{
if(is != null) is.close();
}
catch (IOException e)
{
}
}
/**
* 关闭输出流,忽略异常
* @param os
*/
public final static void closeQuitely(OutputStream os)
{
try
{
if(os != null) os.close();
}
catch (IOException e)
{
}
}
/**
* 半闭输入输出流,忽略异常
* @param is
* @param os
*/
public final static void closeQuitely(InputStream is, OutputStream os)
{
closeQuitely(is);
closeQuitely(os);
}
}
☆在JSP上实现类似ASP中的Global.asa 这样具有系统事件监听功能的程序并不难,我想这也是很多JSP朋友所关注的问题,那么本人就将实现过程展示如下:
1.实现监听类
package Bean.Sample;
import java.util.Date;
import java.servlet.http.HttpSessionEvent;
import java.servlet.http.HttpSessionListener;
public class counterListenerExam implements HttpSessionListener{
private static int count = 0;
private static int activeCount = 0;
public void sessionCreate(HttpSessionEvent evt){
long time = evt.getSession().getCreationTime();
System.out.println(“session 建立的时间” + new Date(time));
count ++;
activeCount ++;
}
public void sessionDestroyed(HttpSessionEvent evt){
activeCount —
}
public static String getCounteInfo(){
return “建立总数:” + count + ” ” +
“活动总数:” + activeCount;
}
}
2. 监听器的配置:
通过WEB应用程序的XML部署符WEB.XNL实现,在WEB.XML中添加一个
web.xml
…
..
3. JSP页应用
<%@page import="Bean.Sample"%>
<%
Sample.getCounterInfo();
%>