javawebday25(编码方式 获取资源路径)

来源:互联网 发布:无效的json字符串 编辑:程序博客网 时间:2024/05/22 03:04
/* *编码 * 常见字符编码 iso-8859-1(拉丁文不支持中文)、gb2312、gbk、gb18030(系统默认编码 中国的国标码)、utf-8(万国码,支持全世界的编码,一般都用这个) *响应编码  * 当使用response.getWriter()来向客户端发送字符数据时,如果在之前没有设置编码 那么默认使用iso 因为iso不支持中文 一定乱码 * 当使用response.getWriter()之前可以使用response.setCharacterEncoding()来设置字符流的编码为gbk或utf-8 当然通常会选择utf-8         * 在使用response.getWriter()之前可以使用response.setHeader("Content-Type","text/html;charset=utf-8")来设置响应头 通知浏览器这边使用的是utf-8 * setHeader("Content-Type","text/html;charset=utf-8")的快捷方式是setContentType("text/html;charset=utf-8") *请求编码 * 客户端发送给服务器的请求参数是什么编码 *  客户端首先要打开一个页面 然后在页面中提交表单或点击超链接,在请求这个页面时,服务器响应的编码是什么,那么客户端发送请求时的编码就是什么  * 服务器默认使用什么编码来解析参数 *  服务器默认使用ISO-8859-1来解码 一定会出现乱码 因为iso不支持中文 * 请求编码处理分为两种 GET和POST GET请求参数不在请求体中 而POST请求参数在请求体中 所以他们的处理方式是不同的 * GET请求编码处理  tomcat8默认utf-8编码 *  String username = new String(request.getParameter("iso-8859-1"),"utf-8") *  在server.xml中配置URIEncoding=utf-8 * POST请求编码处理 *  String username = new String(request.getParameter("iso-8859-1"),"utf-8") *  在获取参数之前调用request.setCharacterEncoding("utf-8")  *URL编码  * 表单的类型 Content-Type application/x-www-form-urlencoded 把中文转换成%后面跟随两位的16进制 * 在客户端和服务器之间传递中文 需要转换成网络适合的方式 *  不是字符编码  *  用来在客户端与服务器之间传递参数的一种方式 *  URL编码需要先指定一种字符编码 把字符串解码后 得到BYTE[] 然后把小于0的字节+256 在转换成16进制 前面再添加一个% *  POST请求默认使用URL编码 tomcat会自动使用URL解码 *  URL编码 String username = URLEncoder.encode(username,"utf-8"); 将名字转成 16进制类型 *  URL解码 String username = URLDecoder.decode(username,"utf-8"); 将16进制解码回名字 *  最后 需要把链接中的中文参数 使用url来编码 html中不能给出java代码 学了jsp就行 *   *web.xml中<url-pattern>路径 (Servlet路径) * 要么以*开头 要么用"/"开头 *转发和包含路径 * (绝对路径 一般都用这个)以 "/"开头 相对当前项目路径例如 http://localhost:8080/项目名/ request.getRequestdispatcher("/BServlet").for..(); * 不以"/"开头 相对当前Servlet路径  request.getRequestdispatcher("/BServlet").for..(); 假如当前Servlet是  http://localhost:8080/项目名/servlet/AServlet  *就是http://localhost:8080/项目名/servlet/BServlet *重定向路径(客户端路径) * 以"/"开头 相对当前主机 例如http://localhost:8080/ 所以需要自己手动添加项目名 例如response.sendRedirect("/day05/BServlet"); *页面中超链接和表单路径 *  与重定向相同,都是 客户端路径 需要添加项目名 *  <form action="day05/AServlet"> *  <a href="day05/AServlet"> *  <a href="AServlet"> 如果不以"/"开头 那么相对当前页面所在路径 如果是http://localhost:8080/day05/html/form.html  即http://localhost:8080/day05/html/AServlet *  ***建议使用以"/"开头的路径 即绝对路径 *ServletContext获取资源路径 *  相对当前项目目录 即当前index.jsp所在目录   *ClassLoader获取资源路径 *  相对classes目录 *Class获取资源路径 *  以"/"开头相对classes目录   *  不以"/"开头相对当前.class文件所在目录  */
    /*     * Test中有主函数的话测试结果不会显示     * [-27, -68, -96, -28, -72, -119]      * [%E5, -68, -96, -28, -72, -119]      * 256+x 在转成16进制     */    @Test    public void fun() throws UnsupportedEncodingException {        String name = "张三";        // byte[] bytes = name.getBytes("utf-8");        // System.out.println(Arrays.toString(bytes));        String s = URLEncoder.encode(name, "utf-8");        System.out.println(s);        s = URLDecoder.decode(s, "utf-8");        System.out.println(s);    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        /*         *1、先获取来自iso的错误字符串          *2、回退(使用utf-8)重编         */        String name = request.getParameter("username");        byte[] b = name.getBytes("iso-8859-1");        name = new String(b,"utf-8");        System.out.println(name);    }    protected void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        /*         *1、在获取参数之前 需要先调用request.setCharacterEncoding("utf-8")         *2、使用getParameter()来获取参数 %E5%8F%B2%E8%AF%97          */        request.setCharacterEncoding("utf-8");        String username = request.getParameter("username");        System.out.println(username);    }

form.html

<a href="/day05/AServlet?username=史诗">get</a><form action="/day05/AServlet" method="post">    用户名<input type="text" name="username" value="史诗"/>    <input type="submit" value="提交"/></form>
    @Test    public void fun2(){        //ClassLoader获取资源时不能以"/"开头        ClassLoader c = Test_02.class.getClassLoader();//得到类加载器        c.getResourceAsStream("a.txt");//让类加载器去类路径下查找资源        System.out.println();    }
    @Test    public void fun2() throws IOException{        //ClassLoader获取资源时不能以"/"开头        ClassLoader c = Test_02.class.getClassLoader();//得到类加载器        InputStream in = c.getResourceAsStream("a.txt");//让类加载器去类路径下查找资源        System.out.println(IOUtils.toString(in));    }    //使用class来加载类路径下的资源    @Test    public void fun3() throws IOException {        Class c = Test_02.class;        InputStream in = c.getResourceAsStream("/a.txt");//与类加载器的效果相同        System.out.println(IOUtils.toString(in));    }    @Test    public void fun4() {        Class c = Test_02.class;        InputStream in = c.getResourceAsStream("a.txt");//到当前Class对应的.class文件所在目录    }
原创粉丝点击