JavaWeb学习五(编码和路径)

来源:互联网 发布:python电子书 编辑:程序博客网 时间:2024/06/05 18:30

一.编码

1.常见字符编码

  • iso-8859-1(不支持中文)
  • gb2312 gbk gb18030(系统默认 中国国标码)
  • utf-8(万国码 我们要用的)

2.响应编码

服务器发给浏览器

这里写图片描述

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("呵呵");    }}

这里写图片描述

3.请求编码

  • 浏览器发给服务器
  • 请求编码处理分为两种:GET和POST,GET请求参数不再请求体中,而POST请求参数在请求体中 所以它们的处理方式是不同的

这里写图片描述

解决请求乱码方式有很多,其中这样也可以解决
去Tomcat安装目录下conf文件查看server.xml添加如下红框

这里写图片描述
注意:但是这种方式并不建议使用

(1).GET请求编码处理
String username = new String(request.getParameter("xxx").getByts("iso-8859-1"),"utf-8"); 

注意:在server.xml中配置URIEncoding=utf-8这种方式不能使用

(2).POST请求编码处理
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);    }}

4.URL编码

这里写图片描述

  • 表单的类型:Content-Type:application/X-www-form-urlencoded 就是把中文转换成%后面跟随两位的16进制
  • 在客户端和服务器之间传递非英文时需要把它转换成网络适合的方式
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来编码

二.路径

1.web.xml中<url-pattern>路径

Servlet路径,要么以*开头 要么为/开头

2.转发和包含路径

  • 以/开头:相对当前项目路径 例如: http://localhost:8080/项目名/
request.getRequestdispacher("/BServlet").for...()
  • 不以/开头:相对当前Servlet路径

3.重定向路径

  • 以/开头 相对于当前主机 http://localhost:8080/
response.sendRedirect("/day10/Bservlet");

4.页面中超链接和表单路径

与重定向相同,都是客户端路径,需要添加项目名

<form action="/day10/ASerlvet"> <a href="/day10/AServlet">
<a href="AServlet"> 如果不以"/"开头 那么相对当前页面所在路径http://localhost:8080/day10/html/form.html http://localhost:8080/day10/html/AServlet//建议使用以/开头的路径 即绝对路径!

5.ServletContext获取资源路径

  • 相当于当前项目目录,即当前index.jsp所在目录

6.ClassLoader获取资源路径

  • 相对classes目录

7.Class获取资源路径

  • 以”/”开头相对classes目录
  • 不以”/”开头相对当前.class文件所在目录

这里写图片描述

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!!!!!!!

1 0
原创粉丝点击