Java-Web 编码和路径

一、响应编码

        当使用response.getWriter()来向客户端发送字符数据时,如果在之前没有设置编码,那么默认使用iso-8859-1,因为iso-8859-1不支持中文,一定乱码。
        在使用response.getWriter()之前可以使用response.setCharaceterEncoding()来设置字符流的编码为gbk或utf-8,当然我们通常会选择utf-8。这样使用response.getWriter()发送的字符就是使用utf-8编码的。但还是会出现乱码!因为浏览器并不知道服务器发送过来的是什么编码的数据!这时浏览器会使用gbk来解码,所以乱码!
        在使用response.getWriter()之前可以使用response.setHeader("Content-type","text/html;charset=utf-8")来设置响应头,通知浏览器服务器这边使用的是utf-8编码,而且在调用setHeader()后,还会自动执行setCharacterEncding()方法。这样浏览器会使用utf-8解码,所以就不会乱码了!
        在静态页面中,使用<meta>来设置content-type响应头,例如:

        还有一种简写方式  setContentType("text/html;charset=utf-8);   推荐使用这种方法

            <meta http-equiv="content-type" content="text/html; charset=UTF-8">


二、请求编码

        * 客户端发送给服务器的请求参数是什么编码:

    客户端首先要打开一个页面,然后在页面中提交表单或点击超链接!在请求这个页面时,服务器响应的编码是什么,那么客户端发送请求时的编码就是什么。
    * 服务器端默认使用什么编码来解码参数:
    服务器端默认使用ISO-8859-1来解码!所以这一定会出现乱码的!因为iso不支持中文!
    * 请求编码处理分为两种:GET和POST:GET请求参数不在请求体中,而POST请求参数在请求体中,所以它们的处理方式是不同的!
        * GET请求编码处理:

        > 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");
  * URL解码:String username = URLDecoder.decode(username, "utf-8");
  最后我们需要把链接中的中文参数,使用url来编码!


四、路径

    1.客户端路径
        超链接、表单、重定向都是客户端路径,客户端路径可以分为三种方式:
                绝对路径;
                以“/”开头的相对路径;

                不以“/”开头的相对路径;

例如:http://localhost:8080/hello1/pages/a.html中的超链接和表单如下:

绝对路径:<a href="http://localhost:8080/hello2/index.html">链接1</a>
客户端路径:<a href="/hello3/pages/index.html">链接2</a>
相对路径:<a href="index.html">链接3</a>
<hr/>
绝对路径:
<form action="http://localhost:8080/hello2/index.html">
  <input type="submit" value="表单1"/>
</form>
客户端路径:
<form action="/hello2/index.html">
  <input type="submit" value="表单2"/>
</form>
相对路径:
<form action="index.html">
  <input type="submit" value="表单3"/>
</form>

          链接1和表单1:没什么可说的,绝对路径;

      链接2和表单2:以“/”开头,相对主机,与当前a.html的主机相同,即最终访问的页面为http://localhost:8080/hello2/index.html;

        链接3和表单3:不以“/”开头,相对当前页面的路径,即a.html所有路径,即最终访问的路径为:http://localhost:8080/hello1/pages/index.html;

重定向1:

public class AServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.sendRedirect("/hello/index.html");
	}
}

        假设访问AServlet的路径为:http://localhost:8080/hello/servlet/AServlet

    因为路径以“/”开头,所以相对当前主机,即 http://localhost:8080/hello/index.html。

重定向2:

public class AServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.sendRedirect("index.html");
	}
}

        假设访问AServlet的路径为:http://localhost:8080/hello/servlet/AServlet

        因为路径不以“/”开头,所以相对当前路径,即http://localhost:8080/hello/servlet/index.html

    2.服务器端路径

        服务器端路径必须是相对路径,不能是绝对路径。但相对路径有两种形式:

            以“/”开头;

            不以“/”开头;

        其中请求转发、请求包含都是服务器端路径,服务器端路径与客户端路径的区别是:

            客户端路径以“/”开头:相对当前主机;

            服务器端路径以“/”开头:相对当前应用;

请求转发1:

public class AServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getRequestDispatcher("/BServlet").forward(request, response);
	}
}

        假设访问AServlet的路径为:http://localhost:8080/hello/servlet/AServlet

        因为路径以“/”开头,所以相对当前应用,即http://localhost:8080/hello/BServlet。

请求转发2:

public class AServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getRequestDispatcher("BServlet").forward(request, response);
	}
}

        假设访问AServlet的路径为:http://localhost:8080/hello/servlet/AServlet

        因为路径不以“/”开头,所以相对当前应用,即http://localhost:8080/hello/servlet/BServlet。

    3.<url-pattern>路径

        <url-pattern>必须使用“/”开头,并且相对的是当前应用。

    4.ServletContext获取资源

        必须是相对路径,可以“/”开头,也可以不使用“/”开头,但无论是否使用“/”开头都是相对当前应用路径。

        例如在AServlet中获取资源,AServlet的路径路径为:http://localhost:8080/hello/servlet/AServlet:

public class AServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String path1 = this.getServletContext().getRealPath("a.txt");
		String path2 = this.getServletContext().getRealPath("/a.txt");
		System.out.println(path1);
		System.out.println(path2);
	}
}

        path1和path2是相同的结果:http://localhost:8080/hello/a.txt

    5.Class获取资源

        Class获取资源也必须是相对路径,可以“/”开头,也可以不使用“/”开头。

package cn.yfy;

import java.io.InputStream;

public class Demo {
	public void fun1() {
		InputStream in = Demo.class.getResourceAsStream("/a.txt");
	}
	
	public void fun2() {
		InputStream in = Demo.class.getResourceAsStream("a.txt");
	}
}

        fun1()方法获取资源时以“/”开头,那么相对的是当前类路径,即/hello/WEB-INF/classes/a.txt文件;

        fun2()方法获取资源时没有以“/”开头,那么相对当前Demo.class所在路径,因为Demo类在cn.yfy包下,所以资源路径为:/hello/WEB-INF/classes/cn/yfy/a.txt。

    6.ClassLoader获取资源

        ClassLoader获取资源也必须是相对路径,可以“/”开头,也可以不使用“/”开头。但无论是否以“/”开头,资源都是相对当前类路径。

public class Demo {
	public void fun1() {
		InputStream in = Demo.class.getClassLoader().getResourceAsStream("/a.txt");
	}
	
	public void fun2() {
		InputStream in = Demo.class.getClassLoader().getResourceAsStream("a.txt");
	}
}

        fun1()和fun2()方法的资源都是相对类路径,即classes目录,即/hello/WEB-INF/classes/a.txt

猜你喜欢

转载自blog.csdn.net/fy_java1995/article/details/80143747