本文共 3966 字,大约阅读时间需要 13 分钟。
Request不仅可以获取一系列数据,还可以通过属性传递数据。在ServletRequest接口中,定义了一系列操作属性的方法,具体如下:
public void setAttribute(String name, Object o);
需要注意的是,如果ServeltRequest对象中已经存在指定名称的属性,setAttribute()方法将会先删除原来的属性,然后再添加新的属性。如果传递给setAttribute()方法的属性值对象为null,则删除指定名称的属性,这时的效果等同于removeAttribute()方法。
public Object getAttribute(String name);
public Enumeration getAttributeNames();
注 意 : 只 有 属 于 同 一 个 请 求 中 的 数 据 才 可 以 通 过 S e r v l e t R e q u e s t 对 象 传 递 数 据 。 \color{red}{注意:只有属于同一个请求中的数据才可以通过ServletRequest对象传递数据。} 注意:只有属于同一个请求中的数据才可以通过ServletRequest对象传递数据。
示例:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //向request中存入数据 request.setAttribute("str1","helloWorld1"); request.setAttribute("str2","helloWorld2"); request.setAttribute("str3","helloWorld3"); //从request对象中获取数据 String str1 = (String) request.getAttribute("str1"); System.out.println(str1); //从request对象中删除数据 request.removeAttribute("str1"); //返回所有属性名,并遍历 Enumeration attributeNames = request.getAttributeNames(); while (attributeNames.hasMoreElements()){ String str = (String) request.getAttribute((String) attributeNames.nextElement()); System.out.println(str); } }
浏览器访问,控制台输出:
helloWorld1helloWorld3helloWorld2
在Servlet中,如果当前Web资源不想处理请求时,可以通过forward()方法将当前请求转发给其它的Web资源进行处理,这种方式称为请求转发。为了更好的理解使用forward()方法实现请求转发的工作原理,下面通过一张图来描述:
示例:表单提交到AttributeServlet,AttributeServlet转发到RequestServlet,RequestServlet获取表单数据然后打印 :
表单:
AttributeServlet:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //转发到RequestServlet request.getRequestDispatcher("/request").forward(request, response); }
RequestServlet:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); username = new String(username.getBytes("iso8859-1"), "utf-8"); String password = request.getParameter("password"); System.out.println("用户名:" + username); System.out.println("密码:" + password); //获取参数名为hobby的值 String[] hobbys = request.getParameterValues("hobby"); System.out.println("爱好:"); for (int i = 0; i < hobbys.length; i++) { System.out.println(hobbys[i] + ", "); } }
通过浏览器访问表单页面填写数据并提交:
用户名:tom密码:123456爱好:sing, dance, football,
当文档移动到新的位置,我们需要向客户端发送这个新位置时,我们需要用到网页重定向。当然,也可能是为了负载均衡,或者只是为了简单的随机,这些情况都有可能用到网页重定向。
重定向请求到另一个网页的最简单的方式是使用 response 对象的 sendRedirect() 方法。下面是该方法的定义:public void HttpServletResponse.sendRedirect(String location)throws IOException
该方法把响应连同状态码和新的网页位置发送回浏览器。您也可以通过把 setStatus() 和 setHeader() 方法一起使用来达到同样的效果:
....String site = "http://www.runoob.com" ;response.setStatus(response.SC_MOVED_TEMPORARILY);response.setHeader("Location", site); ....
示例:访问AttributeServlet然后重定向到表单页面
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //重定向到form表单页面 response.sendRedirect(getServletContext().getContextPath() + "/form.jsp"); }
通过浏览器访问AttributeServlet,页面显示:
转载地址:http://mnsqb.baihongyu.com/