jsp 表单里面的内容只要是中文的到servlet里面获取的是乱码

例如1.jsp

  <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<select type="text" name="type" size="1" multiple="1">
   <option>纸盒,快递盒,白纸等</option>
   <option>塑料盒,塑料瓶等</option>
   <option>植物幼苗,小植物等,植物种子等</option>
   <option>易拉罐等</option>
   <option>小小钱财</option>
   </select>
 这里面的内容
 到2.Servlet里面
 String jztype=new String(request.getParameter("type"));
    System.out.println(jztype);

    jztype打印出来是????????????é????????????????? 该怎么解决

6个回答

request.setCharacterEncoding("utf-8"),不行就写个filter

表单的请求方式是post 的话,直接在代码里request.setCharacterEncoding("utf-8");
如果是get方式提交的,那么可以jztype = new String(jztype.getBytes('iso-8859-1'),'utf-8');然后再输出。

或者配置一个过滤器解决所有请求 的乱码问题

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Set;

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.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

public class EnCodingFilter implements Filter {
// FilterConfig fc;
@Override
public void destroy() {
// TODO Auto-generated method stub

}

@Override
public void doFilter(ServletRequest req, ServletResponse res,
        FilterChain chain) throws IOException, ServletException {
    // TODO Auto-generated method stub
    HttpServletRequest request = (HttpServletRequest)req;
    HttpServletResponse response = (HttpServletResponse)res;
    response.setContentType("text/html;charset=utf-8");
    MyRequest myreq = new MyRequest(request);
    chain.doFilter(myreq, response);

}

@Override
public void init(FilterConfig fc) throws ServletException {
    // TODO Auto-generated method stub
//  this.fc = fc;
}

}

class MyRequest extends HttpServletRequestWrapper{
HttpServletRequest request;
Boolean flag = true;
public MyRequest(HttpServletRequest request) {
super(request);
// TODO Auto-generated constructor stub
this.request = request;
}
@Override
public String getParameter(String name) {
// TODO Auto-generated method stub
Map map = getParameterMap();
if(map!=null){
String[] values = map.get(name);
if(values!=null){
return values[0];
}else{
return null;
}
}
return super.getParameter(name);
}
@Override
public String[] getParameterValues(String name) {
// TODO Auto-generated method stub
Map map = getParameterMap();
if(map!=null){
String[] values = map.get(name);
if(values!=null){
return values;
}else{
return null;
}
}
return super.getParameterValues(name);
}
@Override
public Map getParameterMap() {
// TODO Auto-generated method stub

    String method = request.getMethod();

    if("POST".equals(method)){
        try {

            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }else if("GET".equals(method) && flag){
        Map<String,String[]> map = request.getParameterMap();
        Set<String> keys = map.keySet();
        for(String key:keys){
            String[] values = map.get(key); 
            for(int i = 0;i<values.length;i++){  
                try {
                    values[i] = new String(values[i].getBytes("iso-8859-1"),"utf-8");
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        flag = false;
        return map;
    }
    return super.getParameterMap();
}

}

表单的请求方式是post 的话,直接在代码里request.setCharacterEncoding("utf-8");
如果是get方式提交的,那么可以jztype = new String(jztype.getBytes('iso-8859-1'),'utf-8');然后再输出。

或者配置一个过滤器解决所有请求 的乱码问题

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Set;

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.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

public class EnCodingFilter implements Filter {
// FilterConfig fc;
@Override
public void destroy() {
// TODO Auto-generated method stub

}

@Override
public void doFilter(ServletRequest req, ServletResponse res,
        FilterChain chain) throws IOException, ServletException {
    // TODO Auto-generated method stub
    HttpServletRequest request = (HttpServletRequest)req;
    HttpServletResponse response = (HttpServletResponse)res;
    response.setContentType("text/html;charset=utf-8");
    MyRequest myreq = new MyRequest(request);
    chain.doFilter(myreq, response);

}

@Override
public void init(FilterConfig fc) throws ServletException {
    // TODO Auto-generated method stub
//  this.fc = fc;
}

}

class MyRequest extends HttpServletRequestWrapper{
HttpServletRequest request;
Boolean flag = true;
public MyRequest(HttpServletRequest request) {
super(request);
// TODO Auto-generated constructor stub
this.request = request;
}
@Override
public String getParameter(String name) {
// TODO Auto-generated method stub
Map map = getParameterMap();
if(map!=null){
String[] values = map.get(name);
if(values!=null){
return values[0];
}else{
return null;
}
}
return super.getParameter(name);
}
@Override
public String[] getParameterValues(String name) {
// TODO Auto-generated method stub
Map map = getParameterMap();
if(map!=null){
String[] values = map.get(name);
if(values!=null){
return values;
}else{
return null;
}
}
return super.getParameterValues(name);
}
@Override
public Map getParameterMap() {
// TODO Auto-generated method stub

    String method = request.getMethod();

    if("POST".equals(method)){
        try {

            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }else if("GET".equals(method) && flag){
        Map<String,String[]> map = request.getParameterMap();
        Set<String> keys = map.keySet();
        for(String key:keys){
            String[] values = map.get(key); 
            for(int i = 0;i<values.length;i++){  
                try {
                    values[i] = new String(values[i].getBytes("iso-8859-1"),"utf-8");
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        flag = false;
        return map;
    }
    return super.getParameterMap();
}

}

首先你的提交方式是什么 还有就是你的servlet也要设置编码

你的编程软件是GBK的编码格式 在拷贝时编码格式别乱改。

 String jztype=new String(request.getParameter("type"));

为什么new String这么写?

  String jztype=request.getParameter("type");

直接用字符串接不行吗?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问