您的位置:首页 > 其它

网页编码

2015-08-11 19:58 399 查看
serverlet的网页编码

Serverlet的编码思想:网页的编码都是iso-8859-1而一般我们所用的编程平台都是utf-8这样之间的传递就会出现乱码,就得需要编码转换,转换的思想将的到的编码,转换为最小单位编码,byte,然后在 转换为自己想要的编码。举个列子,首先得到的是网页的数据,iso-8859-1的编码,我要得到utf-8,然后得到后在给网页传输一个数据。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

String username=request.getParameter("username");//得到的网页的uername,
String password=request.getParameter("password");
username=Encoding.encoding(username);//通过编写的方法,进行转换
System.out.println("用户名"+username+"\n"+"密码"+password);
/**
* 然后在给浏览器数据
*/
response.setHeader("content-type","text/html;charset=utf-8");//让浏览器以utf-8格式解析
response.getWriter().append("用户名:"+username);
response.getWriter().println("<br>");
response.getWriter().append("密码    :"+password);
}
编码转换的代码
public class Encoding {
public static String encoding(String s) {

try {
byte[] array = s.getBytes("ISO-8859-1");
s = new String(array, "utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return s;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  乱码 编码