all gone

all gone

#

JSP 处理MySQL数据库时的中文问题

以前遇到JSP 处理MySQL数据库时的中文问题时,采取的是很笨的一种方法,直接用字符串编码转换函数进行转换,这次从网上搜了一下,找到了一个使用Filter的可行方法。在Tomcat 5.5+ MySQL4.0.16下通过。

filter类源码是从网上找的,如下
/**
 *
 */
package com.lzy;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * @author lzy
 *
 */
public class SetCharacterEncodingFilter implements Filter {
 
 protected String encoding = null;
    protected FilterConfig filterConfig = null;
    protected boolean ignore = true;


 /* (non-Javadoc)
  * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
  */
 public void init(FilterConfig filterConfig) throws ServletException {
  // TODO Auto-generated method stub
  this.filterConfig = filterConfig;
     this.encoding = filterConfig.getInitParameter("encoding");
     String value = filterConfig.getInitParameter("ignore");
     if (value == null)
      this.ignore = true;
     else if (value.equalsIgnoreCase("true"))
      this.ignore = true;
     else if (value.equalsIgnoreCase("yes"))
      this.ignore = true;
     else
      this.ignore = false;


 }

 /* (non-Javadoc)
  * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
  */
 public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {
  
  // TODO Auto-generated method stub
  if (ignore || (request.getCharacterEncoding() == null)) {
   String encoding = selectEncoding(request);
   if (encoding != null)
    request.setCharacterEncoding(encoding);
  }
  chain.doFilter(request, response);
 }

 /* (non-Javadoc)
  * @see javax.servlet.Filter#destroy()
  */
 public void destroy() {
  // TODO Auto-generated method stub
  
  this.encoding = null;
     this.filterConfig = null;


 }
 
  protected String selectEncoding(ServletRequest request) {

         return (this.encoding);

     }


}

在web.xml 文件中作如下设置:(我使用的是Struts框架)
<filter>
<filter-name>Encoding</filter-name>
<filter-class>com.lzy.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>Encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>Encoding</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>


最后,连接数据库时,使用下面的url:
jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=GBK

posted @ 2005-12-18 22:57 all gone 阅读(476) | 评论 (0)编辑 收藏

Spring之Hello World

1.下载Spring相关的开发包
下载地址:http://sourceforge.net/project/showfiles.php?group_id=73357
有spring-framework-1.2.6-with-dependencies.zip,一個是spring-framework-1.2.6.zip,最好下载with-dependencies的,里面有另外一些附加包,下载后解压缩,dist目录下是spring自身的jar,lib目录下是附加的jar。
2.新建Java Project,将spring.jar(dist目录下)和commons-logging.jar(lib目录下)添加到project的build path中。
3.新建POJO Bean类:HelloBean
//HelloBean.java
/**
 *
 */
package com.lzy;

/**
 * @author lzy
 *
 */
public class HelloBean{
 
 private String hello;
 
 public void sayHello(){
  System.out.println(this.getHello());
 }

 /**
  * @return Returns the hello.
  */
 public String getHello() {
  return hello;
 }

 /**
  * @param hello The hello to set.
  */
 public void setHello(String hello) {
  this.hello = hello;
 }
 

}


4.新建文件bean.xml,将在这个XML文件中为一个HelloBean的实例的hello属性赋值。
//bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<description>Spring Quick Start</description>
<bean id="helloBean" class="com.lzy.HelloBean">
  <property name="hello">
   <value>hello world</value>
  </property>
</bean>
</beans>

5.新建测试类:TestSpring
//TestSpring.java
/**
 *
 */
package com.lzy;

import java.util.Locale;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * @author lzy
 *
 */
public class TestSpring {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  
  ApplicationContext ctx=new FileSystemXmlApplicationContext("bean.xml");
    HelloBean hello=(HelloBean)ctx.getBean("helloBean");
  hello.sayHello();
 }

}


6.运行测试类:
    如果没有出错,输出中将会看到“hello world”。

posted @ 2005-12-14 22:07 all gone 阅读(1037) | 评论 (0)编辑 收藏

仅列出标题
共17页: First 上一页 5 6 7 8 9 10 11 12 13 下一页 Last