服务器发给浏览器
response.getWriter();之前使用response.setCharceterEncoding()来设置字符流的编码为utf-8
response.getWriter();之前使用 response.setHeader("Conntent-type","text/html;charset=uft-8")设置响应头 同时也通知浏览器服务器两边使用utf-8;
举例
public class FServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().print("呵呵");
}
}
乱码了…再看看我们添加上两行代码的效果
public class FServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type","text/html;charset=utf-8");
//相当于上面这句response.setContentType("text/html;charset=utf-8");
response.getWriter().print("呵呵");
}
}
解决请求乱码方式有很多,其中这样也可以解决
去Tomcat安装目录下conf文件查看server.xml添加如下红框
String username = new String(request.getParameter("xxx").getByts("iso-8859-1"),"utf-8");
注意:在server.xml中配置URIEncoding=utf-8这种方式不能使用
String username = request.getParameter("xxx");
//在获取参数之前调用request.setCharacterEncoding("utf-8");
举例
public class GServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username=request.getParameter("username");
System.out.println(username);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String username=request.getParameter("username");
System.out.println(username);
}
}
URL编码: String username=URLEncoder.encode(username,"utf-8");
URL解码: String username=URLDecoder.decode(username,"utf-8");
public class Temp {
//[-25, -77, -106, -25, -77, -106]
//%E7 %B3 %96 %E7 %B3 %96
//糖糖
@Test
public void fun() throws UnsupportedEncodingException {
String user="糖糖";
//byte [] bys=user.getBytes("UTF-8");
//System.out.println(Arrays.toString(bys));
String user2=URLEncoder.encode(user,"UTF-8");
System.out.println(user2);
String user3=URLDecoder.decode(user2,"UTF-8");
System.out.println(user3);
}
}
打印
%E7%B3%96%E7%B3%96
糖糖
总结:今后我们需要把链接中的中文参数,使用URL来编码
路径Servlet路径,要么以*开头 要么为/开头
request.getRequestdispacher("/BServlet").for...()
response.sendRedirect("/day10/Bservlet");
与重定向相同,都是客户端路径,需要添加项目名
<form action="/day10/ASerlvet">
<a href="/day10/AServlet">
<a href="AServlet"> 如果不以"/"开头 那么相对当前页面所在路径
http://localhost:8080/day10/html/form.html
http://localhost:8080/day10/html/AServlet
//建议使用以/开头的路径 即绝对路径!
public class Temp {
/*
* ClassLoader获取资源时,不能以"/"开头
*/
@Test
public void fun1() throws IOException {
ClassLoader classLoader=Temp.class.getClassLoader();//得到类加载器
InputStream io=classLoader.getResourceAsStream("a.html");//让类加载器去类路径下查找资源
System.out.println(IOUtils.toString(io));
}
@Test
public void fun2() throws IOException {
Class c=Temp.class;
InputStream io=c.getResourceAsStream("/a.html");//与类加载器的效果相同
System.out.println(IOUtils.toString(io));
}
@Test
public void fun3() throws IOException {
Class c=Temp.class;
InputStream io=c.getResourceAsStream("a.html");//与类加载器的效果相同
System.out.println(IOUtils.toString(io));
}
}
END!!!!!!!