首页 > 代码库 > 域对象的属性和请求的转发重定向
域对象的属性和请求的转发重定向
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=http://www.mamicode.com/"TestServlet">to servlet
>
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); }
域对象的属性和请求的转发重定向
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。