JSP + MySQL + Tomcat + Apache garbled Solution

Source: Internet
Author: User

JSP + MySQL + Tomcat + Apache garbled Solution
Author: posting date: 2006.06.05 Source: jspcn
[Java area] [security area] [network management area] [Linux area] [access forum] [It blog]


 

 

 

 

 

 

 

 

This article takes the most common JSP + MySQL + Tomcat + Apache garbled solution as an example. We hope this will be an effective solution for your environment configuration!

Garbled code has been around for a long time. In an open-source environment, garbled Code is even worse for programmers. I have experienced Garbled text in Unix (FreeBSD), so I wrote this article to help you!

I classify all the garbled characters in this case into three types:

1. garbled page characters

2. garbled characters in records

3. Request transmission garbled characters

The above three types of garbled characters will be parsed as follows:

I. garbled page characters:

1. Case Insensitive:

Org. apache. jasper. jasperexception:/top. JSP (1, 1) page directive: illegal to have multiple occurrences of contenttype with different values (old: text/html; charset = gb2312, new: text/html; charset = gb2312)

2. Inconsistent intervals:

Org. apache. jasper. jasperexception:/top. JSP (1, 1) page directive: illegal to have multiple occurrences of contenttype with different values (old: text/html; charset = gb2312, new: text/html; charset = gb2312)

* Solution:

First, add adddefaultcharset gb2312 or adddefacharcharset GBK to Apache.

Second, the unified use of the page encoding definition, such as:, OK, is indeed a mess. Now there are two solutions.

* Solution:

1. Add this statement: request. setcharacterencoding ("GBK ");

It can be performed on one or two pages, but this method is not a benefit.

2. register the setcharacterencodingfilter class:

First, write the setcharacterencodingfilter. Java file. The Code is as follows:

Package cn.com. jsp;

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;

Import javax. servlet. unavailableexception;

Public class setcharacterencodingfilter implements filter {

Protected string encoding = NULL;

Protected filterconfig = NULL;

Protected Boolean ignore = true;

Public void destroy (){

This. Encoding = NULL;

This. filterconfig = NULL;

}

Public void dofilter (servletrequest request, servletresponse response,

Filterchain chain) throws ioexception,

Servletexception {

// Conditionally select and set the character encoding to be used

If (ignore | (request. getcharacterencoding () = NULL )){

String encoding = selectencoding (request );

If (encoding! = NULL ){

Request. setcharacterencoding (encoding );

}

}

// Pass control on to the next Filter

Chain. dofilter (request, response );

}

Public void Init (filterconfig) throws servletexception {

This. filterconfig = filterconfig;

This. Encoding = filterconfig. getinitparameter ("encoding ");

String value = filterconfig. getinitparameter ("Ignore ");

If (value = NULL ){

This. Ignore = true;

} Else if (value. inclusignorecase ("true ")){

This. Ignore = true;

} Else if (value. inclusignorecase ("yes ")){

This. Ignore = true;

} Else {

This. Ignore = false;

}

}

Protected string selectencoding (servletrequest request ){

Return (this. Encoding );

}

}

This file is a request filtering class and must be registered before global compilation.

The registration file is:/WEB-INF/Web. xml.

Add the following code to this file:

Wwwroot

MySQL test app

Setcharacterencodingfilter

Setcharacterencodingfilter

Setcharacterencodingfilter

Cn.com. jsp. setcharacterencodingfilter

Encoding

GBK

Setcharacterencodingfilter

/*

......

OK. Now you can compile your setcharacterencodingfilter. Java file!

At this point, Garbled text will be out of place with you!

Saidi network Java zone, Java developer Park!

SCID it blog, IT people's online home
[Comment] [recommended] [large, medium, small] [print] [closed] 496

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.