6,946   วิธีแก้ทุกกรณีเมื่อ java ไม่เป็นภาษาไทย

วิธีที่ 1 แก้ที่ Class Connect Database

DBConnect.java

try {
	String dbName = "dbname";
	String username = "username";
	String password = "password";
	String classForName = "jdbc:mysql://localhost:3306/" + dbName + "?useUnicode=true&characterEncoding=utf-8," + username + "," + password;
	Class.forName("com.mysql.jdbc.Driver");
	connection = DriverManager.getConnection(classForName);
	System.out.println("========in DBConnect method : Connect Success.========");
	statement = connection.createStatement();

} catch(ClassNotFoundException e){
	e.printStackTrace();
}


วิธีที่ 2 แก้ที่ Servlet GET/POST

Code (JAVA)

request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");


วิธีที่ 3 เพิ่มไฟล์ Config ของโปรเจค

web.xml

<filter>
	<description>Character Encoding Filter </description>
	<display-name>encodingFilter </display-name>
	<filter-name>encodingFilter </filter-name>
	<filter-class>com.csbank.base.servlets.filter.EncodingFilter </filter-class>
	<init-param>
		<param-name>encoding </param-name>
		<param-value>UTF-8 </param-value>
	</init-param>
</filter> 

<filter-mapping>
	<filter-name>encodingFilter </filter-name>
	<url-pattern>/encodingFilter </url-pattern>
</filter-mapping>


***โดยต้องใช้ class นี้ด้วย ***

EncodingFilter.java

package com.EncodingFilter;

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;

public class EncodingFilter implements Filter {

	protected String encoding=null;
	protected boolean ignore=true;
	protected FilterConfig filterConfig=null;
	
	@Override
	public void destroy() {
	
		this.encoding = null;
		this.filterConfig = null;
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
	FilterChain chain) throws IOException, ServletException {
		
		// Conditionally select and set the character encoding to be used
		System.out.println("Do Filter ");
		if (ignore || (request.getCharacterEncoding() == null)) {
			String encoding = selectEncoding(request);
		if (encoding != null)
			request.setCharacterEncoding(encoding);
		}
		chain.doFilter(request, response);
	}
	
	@Override
	public void init(FilterConfig 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.equalsIgnoreCase("true"))
			this.ignore = true;
		else if (value.equalsIgnoreCase("yes"))
			this.ignore = true;
		else
			this.ignore = false;
		
		this.filterConfig.getServletContext().log("Initialize Filter: " + this.filterConfig.getFilterName()+ " with encoding="+ encoding);
	}
	
	protected String selectEncoding(ServletRequest request) {
		return (this.encoding);
	}
}


วิธีที่ 4 แก้ที่ JSP File

<@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8">


วิธีที่ 5 แก้ที่ HTML-meta tags

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />

 

^^ลองไปแก้แค่นี้ดูก่อนนะครับ^^

Back to Top