各位大侠 小弟在JSP页面上面有一个TEXT
然后我在TEXT里面写入数据
点确定后 提交到ACTION里面
然后在ACTION里面处理添加
从FORM里面获得插入的值但是我页面获取TEXT里面的值的时候是正常的
但是在ACTION里面我在后台通过SYSTEM.OUT.PRINTLN打印出TEXT里面的值就是乱码
而且数据库里面也是乱码请问这是怎么回事啊???
先谢谢了
解决方案 »
- 为什么用myeclipse创建在src下的servlet始终不能在工程的classes下创建.class文件??
- 一个连接池,和大家分享下
- 用java可以编写WEB800吗
- 请教关于jsp 模板的用法
- 是只有当form框里面的method为post时,执行doPost方法
- 关于JAVA异常处理的问题!!顶者有分啊!!
- 求教!!
- 高分求解jsp启动本地application的方法--在线等待ing
- 关于session的一个问题?
- http://www.jspsmart.com/的jspSmartUpload使用求助!
- 返回含bean的list 为什么bean属性取值是 object object?????谢谢啦
- jsp页面中java调用js问题
String s2 = new String(s1.getBytes(“ISO-8859-1”),”GBK”);utf-8解决JSP中文乱码问题,在页面的开始处加:
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <%request.setCharacterEncoding("UTF-8");%>如果仍不能解决问题,还需要这样处理一下:
String msg = request.getParameter("message");
String str=new String(msg.getBytes("ISO-8859-1"),"UTF-8");out.println(st);
from 用method='post'的方式提交
在servlet中
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
如果是用get方式提交的话要单独进行处理
麻烦在问一下
就是我用了
String s2 = new String(s1.getBytes(“ISO-8859-1”),”GBK”);
但是报了一个错Unhandled exception type UnsupportedEncodingException好像不支持~这个应该怎么搞啊
这样写是可以的。需要放入try,catch中
以下是过滤器源码:package com.maca.servlet;
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 shobos
*
*/
public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null; protected FilterConfig 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 { if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
} chain.doFilter(request, response); }
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; }
protected String selectEncoding(ServletRequest request) { return (this.encoding); }
}web.xml的配置文件如下:<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>Encoding</filter-name>
<filter-class>
com.maca.servlet.SetCharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value> <!-- utf-8 设置编码方式 -->
</init-param>
</filter>
<filter-mapping>
<filter-name>Encoding</filter-name>
<url-pattern>/*</url-pattern> <!-- /*表示过滤所有请求 -->
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>看看以上对楼主有没有帮助。