How to put an object on the request in a servlet

Many times when you‘re working with Java servlets and JSP‘s, you‘ll want to forward some piece of information from your servlet to your JSP without having to put that piece of information into thesession. For instance, in many applications you may not have a user session, and in other cases where you do have a session, you may just not want to put a bunch of junk in there.

In either of these cases, it‘s very simple to put a piece of information onto the request (an instance ofHttpServletRequest). Here‘s some sample Java servlet code where I demonstrate this technique:

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
  // do whatever you need to here ...

  // my jsp needs access to the uri the user specified, so i‘ll get it here
  // in the servlet, then put it on the request, where the jsp can access it.
  String uri = request.getRequestURI();
  request.setAttribute("URI", uri);

  // now forward on to the jsp page
  RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(pageURL);
  dispatcher.forward(request,response);
}

  

As you can see from that sample servlet code, it‘s very easy to put something on the request. You just use the setAttribute method, like this:

request.setAttribute("YOUR_KEY", yourVariable);

  

Accessing the request attribute in the JSP

Next, in your JSP code, it‘s very simple to access the attribute you just set on the request. Here‘s some sample JSP code that demonstrates this:

<%
  String theUri = (String)request.getAttribute("URI");
%>

  

As you can see, all you have to do in your JSP is use the getAttribute method of the implicit requestobject, and then cast your object back to its original value. In my case, my object was a String, so I cast it back to a String here in the JSP.

That‘s all you have to do to put an object on the request from a servlet, and then access the object from within your JSP. As a final note, remember that the "request" doesn‘t live very long, so if you need an object to last over many subsequent pages, you‘ll probably want to put that object in the user‘s sessioninstead.

时间: 2024-10-26 02:14:16

How to put an object on the request in a servlet的相关文章

Serlect的笔记二(request 、 ersponse)

Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象. 下面这两个对象的基本理解: 一.HttpServletRequest 1.请求行 Get http://localhost:8080/day09/servlet/req1?username=zs http/1.1 getMethod(); 获得请求方式 * getRequestURL(); 返回客户端发出请求时的完整URL. * getRequestURI()

request.getInputStream为空的改进办法

最近公司的一个小项目进行模块优化,需求是:跟踪记录每个IP请求的源数据和响应数据.当源数据包括requestBody的时候,从HttpServletRequest里面获取requestBody我们采用的是以下代码: @RequestMapping(value = "/{user}/welcome", method = RequestMethod.POST) public ModelAndView queryUpdateStatus(@PathVariable String user,

Action的搭建及application、request、Session的运用 多种方法

1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&g

Servlet &amp; JSP - getParameter 与 request.getAttribute 的区别

getParameter 方法获取的是表单或 URL 的请求参数.参数是从 Web 客户端传递至 Web 服务端.例如有如下的 servlet: @WebServlet(name = "helloServlet", urlPatterns = {"/hello"}) public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req

spring mvc DispatcherServlet详解之三---request通过ModelAndView中获取View实例的过程

整个spring mvc的架构如下图所示: 上篇文件讲解了DispatcherServlet第二步:通过request从Controller获取ModelAndView.现在来讲解第三步:request 从ModelAndView中获取view对象. 获取view对象一般是通过viewResolver来解析view name来完成的.若ModelAndView中view 不存在或者ModelAndView本身为null则填充默认值.代码如下: ModelAndView中view 不存在或者Mod

servlet下的request&amp;&amp;response

request的方法 *获取请求方式: request.getMethod(); * 获取ip地址的方法 request.getRemoteAddr(); * 获得用户清气的路径; * request.getRequestURL().tostring();    --- url * request.getRequestURI()                --- uri *获得工程名; * request.getContextPath(); *在接受数据前设置编码 * post请求 * re

request.setAttribute()、session.setAttribute()和request.getParameter()的联系与区别(记录)

1.session.setAttribute()和session.getAttribute()配对使用,作用域是整个会话期间,在所有的页面都使用这些数据的时候使用. 2.request.setAttribute()和request.getAttribute()配对使用,作用域是请求和被请求页面之间.request.setAttribute()是只在此action的下一个forward需要使用的时候使用:request.getAttribute()表示从request范围取得设置的属性,必须要先s

【转】于request.getSession(true/false/null)的区别

http://blog.csdn.net/gaolinwu/article/details/7285783 关于request.getSession(true/false/null)的区别 一.需求原因 现实中我们经常会遇到以下3中用法: HttpSession session = request.getSession(); HttpSession session = request.getSession(true); HttpSession session = request.getSessi

The process of container handles the servlet request

1. User clicks a link that has a URL of Servlet. Client Browse Servlet URL 2. Container (Apache Tomcat is one of the example) sees that the request is for servlet , so create two objects :HttpServletRequestHttpServletResponse HttpServletRequest and H