码迷,mamicode.com
首页 > 其他好文 > 详细

域对象的属性和请求的转发重定向

时间:2015-01-02 17:27:58      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

1 在HttpServlet中有操作属性的方法,在pageContext,request,session,application四个域对象中也有操作属性的方法。

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> 页面2

  </head>
  
  <body>
     request: <% request.setAttribute("requestValue",123); %>
     session: <% session.setAttribute("sessionValue",123); %>
     application: <% application.setAttribute("applicationValue",123); %>
     <a href="TestServlet">to servlet</a> <br/>
     <a href="ForwardServlet">to servlet2</a> <br/>
     <a href="RedirectServlet">to servlet3</a> <br/>
  </body>
</html>
package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public TestServlet() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        
        Object requestVal = request.getAttribute("requestValue");
        out.println("requestVal:"+requestVal);
        out.println("<br><br>");
        
        Object sessionVal = request.getSession().getAttribute("sessionValue");
        out.println("sessionVal:"+sessionVal);
        out.println("<br><br>");
        
        Object applicationVal = getServletContext().getAttribute("applicationValue");
        out.println("applicationVal:"+applicationVal);
        out.println("<br><br>");
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}

 

2 请求的转发和重定向本质区别:前者只发了一次请求,而重定向发出了两次请求

  请求的转发:地址栏是初次发出请求的地址;在最终的Servlet中,request对象和中转的request是同一个对象;只能转发给当前WEB应用的资源

  请求的重定向:地址栏不再是初次发出的请求地址,地址栏为最后响应的那个地址,request对象和中转的request不是同一个对象。可以重定向到任何资源

public class ForwardServlet extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public ForwardServlet() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        
        String path = "TestServlet";
        RequestDispatcher rd = request.getRequestDispatcher(path);
        rd.forward(request, response);
        
    }
public class RedirectServlet extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public RedirectServlet() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String path = "TestServlet";
        response.sendRedirect(path);
    }

域对象的属性和请求的转发重定向

标签:

原文地址:http://www.cnblogs.com/yjtm53/p/4198600.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!