您的位置:首页 > 其它

转发和重定向之详解

2016-09-24 08:38 267 查看
下面是使用request获取请求参数的API:

l String getParameter(String name):通过指定名称获取参数值;

 

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String v1 = request.getParameter("p1");

String v2 = request.getParameter("p2");

System.out.println("p1=" + v1);

System.out.println("p2=" + v2);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String v1 = request.getParameter("p1");

String v2 = request.getParameter("p2");

System.out.println("p1=" + v1);

System.out.println("p2=" + v2);


}

 

l String[] getParameterValues(String name):当多个参数名称相同时,可以使用方法来获取;

<a href="/hello/ParamServlet?name=zhangSan&name=liSi">超链接</a>

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String[] names = request.getParameterValues("name");

System.out.println(Arrays.toString(names));

}

 

l Enumeration getParameterNames():获取所有参数的名字;

    <form action="/hello/ParamServlet" method="post">

     参数1:<input type="text" name="p1"/><br/>

     参数2:<input type="text" name="p2"/><br/>

     <input type="submit" value="提交"/>

    </form>

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

Enumeration names = request.getParameterNames();

while(names.hasMoreElements()) {

System.out.println(names.nextElement());

}

}

 

l Map getParameterMap():获取所有参数封装到Map中,其中key为参数名,value为参数值,因为一个参数名称可能有多个值,所以参数值是String[],而不是String。

<a href="/day05_1/ParamServlet?p1=v1&p1=vv1&p2=v2&p2=vv2">超链接</a>

Map<String,String[]> paramMap = request.getParameterMap();

for(String name : paramMap.keySet()) {

String[] values = paramMap.get(name);

System.out.println(name +
": " + Arrays.toString(values));


}

p2: [v2, vv2]

p1: [v1, vv1]

 

6 请求转发和请求包含

无论是请求转发还是请求包含,都表示由多个Servlet共同来处理一个请求。例如Servlet1来处理请求,然后Servlet1又转发给Servlet2来继续处理这个请求。

 

6.1 请求转发

在AServlet中,把请求转发到BServlet:

public class AServlet
extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

System.out.println("AServlet");

RequestDispatcher rd = request.getRequestDispatcher("/BServlet");

rd.forward(request, response);

}

}

public class BServlet
extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

System.out.println("BServlet");

}

}

Aservlet

BServlet

 

6.2 请求包含

在AServlet中,把请求包含到BServlet:

public class AServlet
extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

System.out.println("AServlet");

RequestDispatcher rd = request.getRequestDispatcher("/BServlet");

rd.include(request, response);

}

}

public class BServlet
extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

System.out.println("BServlet");

}

}

Aservlet

BServlet

 

6.3 请求转发与请求包含比较

l 如果在AServlet中请求转发到BServlet,那么在AServlet中就不允许再输出响应体,即不能再使用response.getWriter()和response.getOutputStream()向客户端输出,这一工作应该由BServlet来完成;如果是使用请求包含,那么没有这个限制;

l 请求转发虽然不能输出响应体,但还是可以设置响应头的,例如:response.setContentType(”text/html;charset=utf-8”);

l 请求包含大多是应用在JSP页面中,完成多页面的合并;

l 请求请求大多是应用在Servlet中,转发目标大多是JSP页面;






6.4 请求转发与重定向比较

l 请求转发是一个请求,而重定向是两个请求;

l 请求转发后浏览器地址栏不会有变化,而重定向会有变化,因为重定向是两个请求;

l 请求转发的目标只能是本应用中的资源,重定向的目标可以是其他应用;

l 请求转发对AServlet和BServlet的请求方法是相同的,即要么都是GET,要么都是POST,因为请求转发是一个请求;

l 重定向的第二个请求一定是GET;

 

路径

 

1 与路径相关的操作
l 超链接

l 表单

l 转发

l 包含

l 重定向

l <url-pattern>

l ServletContext获取资源

l Class获取资源

l ClassLoader获取资源

 

2 客户端路径
超链接、表单、重定向都是客户端路径,客户端路径可以分为三种方式:

l 绝对路径;

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

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

例如: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>

 

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

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

l 链接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 建议使用“/”

强烈建议使用“/”开头的路径,这说明在页面中的超链接和表单都要以“/”开头,后面是当前应用的名称,再是访问路径:

<form action="/hello/servlet/AServlet">

</form>

<a href="/hello/b.html">链接</a>

 

其中/hello是当前应用名称,这也说明如果将来修改了应用名称,那么页面中的所有路径也要修改,这一点确实是个问题。这一问题的处理方案会在学习了JSP之后讲解!

 

在Servlet中的重定向也建议使用“/”开头。同理,也要给出应用的名称!例如:

response.sendRedirect("/hello/BServlet");

 

其中/hello是当前应用名,如果将来修改了应用名称,那么也要修改所有重定向的路径,这一问题的处理方案是使用request.getContextPath()来获取应用名称。

response.sendRedirect(request.getContextPath() +
"/BServlet");


 

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

l 以“/”开头;

l 不以“/”开头;

 

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

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

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

 

转发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。

 

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

 

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

例如在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

 

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

package cn.itcast;

 

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.itcast包下,所以资源路径为:/hello/WEB-INF/classes/cn/itcast/a.txt。

 

7 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



编码

 

1 请求编码
1.1 直接在地址栏中给出中文

请求数据是由客户端浏览器发送服务器的,请求数据的编码是由浏览器决定的。例如在浏览器地址栏中给出:http://localhost:8080/hello/AServlet?name=传智,那么其中“传智”是什么编码的呢?不同浏览器使用不同的编码,所以这是不确定的!

l IE:使用GB2312;

l FireFox:使用GB2312;

l Chrome:使用UTF-8;

 

  通常没有哪个应用要求用户在浏览器地址栏中输入请求数据的,所以大家只需了解一下即可。

 

1.2 在页面中发出请求

通常向服务器发送请求数据都需要先请求一个页面,然后用户在页面中输入数据。页面中有超链接和表单,通过超链接和表单就可以向服务器发送数据了。

因为页面是服务器发送到客户端浏览器的,所以这个页面本身的编码由服务器决定。而用户在页面中输入的数据也是由页面本身的编码决定的。

index.html

<!DOCTYPE html>

<html>

  <head>

    <title>index.html</title>

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

  </head>

  

  <body>

<form action="/hello/servlet/AServlet">

  名称:<input type="text" name="name"/>

  <input type="submit" value="提交"/>

</form>

<a href="/hello/servlet/AServlet?name=传智">链接</a>

  </body>

</html>

 

当用户在index.html页面中输入数据时,都是UTF-8列表的。因为这个页面本身就是UTF-8编码的!

页面的编译就是页面中输入数据的编码。

 

1.3GET请求解读编码

当客户端通过GET请求发送数据给服务器时,使用request.getParameter()获取的数据是被服务器误认为ISO-8859-1编码的,也就是说客户端发送过来的数据无论是UTF-8还是GBK,服务器都认为是ISO-8859-1,这就说明我们需要在使用request.getParameter()获取数据后,再转发成正确的编码。

例如客户端以UTF-8发送的数据,使用如下转码方式:

String name = request.getParameter(“name”);

name = new String(name.getBytes(“iso-8859-1”), “utf-8”);

 

1.4POST请求解读编码

  当客户端通过POST请求发送数据给服务器时,可以在使用request.getParameter()获取请求参数之前先通过request.setCharacterEncoding()来指定编码,然后再使用reuqest.getParameter()方法来获取请求参数,那么就是用指定的编码来读取了。

也就是说,如果是POST请求,服务器可以指定编码!但如果没有指定编码,那么默认还是使用ISO-8859-1来解读。

request.setCharacterEncoding(“utf-8”);

String name = request.getParameter(“name”);

 

2 响应编码
响应:服务器发送给客户端数据!响应是由response对象来完成,如果响应的数据不是字符数据,那么就无需去考虑编码问题。当然,如果响应的数据是字符数据,那么就一定要考虑编码的问题了。

response.getWriter().print(“传智”);

上面代码因为没有设置repsonse.getWriter()字符流的编码,所以服务器使用默认的编码(ISO-8859-1)来处理,因为ISO-8859-1不支持中文,所以一定会出现编码的。

所以在使用response.getWriter()发送数据之前,一定要设置response.getWriter()的编码,这需要使用response.setCharacterEncoding()方法:

response.setCharacterEncoding(“utf-8”);

response.getWriter().print(“传智”);

上面代码因为在使用response.getWriter()输出之前已经设置了编码,所以输出的数据为utf-8编码。但是,因为没有告诉浏览器使用什么编码来读取响应数据,所以很可能浏览器会出现错误的解读,那么还是会出现乱码的。当然,通常浏览器都支持来设置当前页面的编码,如果用户在看到编码时,去设置浏览器的编码,如果设置的正确那么乱码就会消失。但是我们不能让用户总去自己设置编码,而且应该直接通知浏览器,服务器发送过来的数据是什么编码,这样浏览器就直接使用服务器告诉他的编码来解读!这需要使用content-type响应头。

response.setContentType(“text/html;charset=utf-8”);

response.getWriter().print(“传智”);

  上面代码使用setContentType()方法设置了响应头content-type编码为utf-8,这不只是在响应中添加了响应头,还等于调用了一次response.setCharacterEncoding(“utf-8”),也就是说,通过我们只需要调用一次response.setContentType(“text/html;charset=utf-8”)即可,而无需再去调用response.setCharacterEncoding(“utf-8”)了。

 

在静态页面中,使用<meta>来设置content-type响应头,例如:

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

 

3URL编码
通过页面传输数据给服务器时,如果包含了一些特殊字符是无法发送的。这时就需要先把要发送的数据转换成URL编码格式,再发送给服务器。

其实需要我们自己动手给数据转换成URL编码的只有GET超链接,因为表单发送数据会默认使用URL编码,也就是说,不用我们自己来编码。

例如:“传智”这两个字通过URL编码后得到的是:“%E4%BC%A0%E6%99%BA”。URL编码是先需要把“传智”转换成字节,例如我们现在使用UTF-8把“传智”转换成字符,得到的结果是:“[-28,
-68, -96, -26, -103, -70]”,然后再把所有负数加上256,得到[228, 188, 160, 230, 153, 186],再把每个int值转换成16进制,得到[E4,
BC, A0, E6, 99, BA],最后再每个16进制的整数前面加上“%”。

通过URL编码,把“传智”转换成了“%E4%BC%A0%E6%99%BA”,然后发送给服务器!服务器会自动识别出数据是使用URL编码过的,然后会自动把数据转换回来。

 

当然,在页面中我们不需要自己去通过上面的过程把“传智”转换成“%E4%BC%A0%E6%99%BA”,而是使用Javascript来完成即可。当后面我们学习了JSP后,就不用再使用Javascript了。

  <script type="text/javascript">

  function
_go() {

   location = "/day05_2/AServlet?name=" + encodeURIComponent("传智+播客");

   }

  </script>

<a href="javascript:_go();">链接</a>

 

因为URL默认只支持ISO-8859-1,这说明在URL中出现中文和一些特殊字符可能无法发送到服务器。所以我们需要对包含中文或特殊字符的URL进行URL编码。

服务器会自动识别数据是否使用了URL编码,如果使用了服务器会自动把数据解码,无需我们自己动手解码。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  转发 重定向