Request对象中请求参数中文编码问题

来源:互联网 发布:网络歌手虞姬资料 编辑:程序博客网 时间:2024/05/19 23:29

register.html文件:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>学生注册系统</title>  </head>  <body>    <form action="/Request/servlet/RequestDemo6" method="post">        <input type="hidden" value="438" name="id"/>        <table border="1" width="438">            <tr>                <td>姓名</td>                <td>                    <input type="text" name="name"/>                </td>            </tr>            <tr>                <td>密码</td>                <td>                    <input type="password" name="password"/>                </td>            </tr>            <tr>                <td>性别</td>                <td>                    <input type="radio" name="gender" value="male" checked="checked"/>男性                    <input type="radio" name="gender" value="female"/>女性                </td>            </tr>            <tr>                <td>婚否</td>                <td>                    <input type="checkbox" name="married"/>                </td>            </tr>            <tr>                <td>爱好</td>                <td>                    <input type="checkbox" name="hobby" value="eat"/>吃饭                    <input type="checkbox" name="hobby" value="sleep"/>睡觉                    <input type="checkbox" name="hobby" value="java"/>学Java                </td>            </tr>            <tr>                <td>籍贯</td>                <td>                    <select name="province">                        <option value="BJ">北京</option>                        <option value="SD">山东</option>                        <option value="HB">湖北</option>                    </select>                </td>            </tr>            <!--             <tr>                <td>靓照</td>                <td>                    <input type="file" name="image"/>                </td>            </tr>             -->             <tr>                <td>简介</td>                <td>                    <textarea rows="3" cols="38" name="description"></textarea>                </td>            </tr>            <tr>                <td colspan="2"><!--                    <input type="submit" value="注册"/> --><!--                    <input type="image" src="an-0333.gif"/> -->                    <input type="button" value="注册" onclick="toSubmit()"/>                </td>            </tr>        </table>        <script type="text/javascript">            function toSubmit(){                //验证用户的输入                document.forms[0].submit();            }        </script>    </form>  </body></html>

Student.java文件:

package com.neu.domain;import java.util.Arrays;public class Student {    private int id;//BeanUtil对基本类型自动转换    private String name;    private String password;    private String gender;    private boolean married;    private String[] hobby;    private String province;    private String description;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    public String getGender() {        return gender;    }    public void setGender(String gender) {        this.gender = gender;    }    public boolean isMarried() {        return married;    }    public void setMarried(boolean married) {        this.married = married;    }    public String[] getHobby() {        return hobby;    }    public void setHobby(String[] hobby) {        this.hobby = hobby;    }    public String getProvince() {        return province;    }    public void setProvince(String province) {        this.province = province;    }    public String getDescription() {        return description;    }    public void setDescription(String description) {        this.description = description;    }    @Override    public String toString() {        return "Student [id=" + id + ", name=" + name + ", password="                + password + ", gender=" + gender + ", married=" + married                + ", hobby=" + Arrays.toString(hobby) + ", province="                + province + ", description=" + description + "]";    }}

RequestDemo6.java文件:

package com.neu;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.lang.reflect.InvocationTargetException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.beanutils.BeanUtils;import com.neu.domain.Student;public class RequestDemo6 extends HttpServlet {/*获取常用表单输入域的值*/    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        test1(request, response);    }    //获取请求正文的参数乱码问题    private void test1(HttpServletRequest request, HttpServletResponse response)            throws UnsupportedEncodingException, IOException {        request.setCharacterEncoding("UTF-8");//设置请求正文使用的编码,只对POST有效        Student s = new Student();        System.out.println("封装前:"+s);        try {            BeanUtils.populate(s, request.getParameterMap());        } catch (Exception e) {            e.printStackTrace();        }        System.out.println("封装后:"+s);        response.setContentType("text/html;charset=UTF-8");        response.getWriter().write(s.toString());    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doGet(request, response);    }}

运行:
http://localhost:8080/Request/register.html
运行结果:
服务器控制台输出:
封装前:Student [id=0, name=null, password=null, gender=null, married=false, hobby=null, province=null, description=null]
封装后:Student [id=438, name=张三, password=123, gender=male, married=true, hobby=[eat, java], province=BJ, description=我去]
浏览器输出:
Student [id=438, name=张三, password=123, gender=male, married=true, hobby=[eat, java], province=BJ, description=我去]

0 0