balu
balu

Reputation: 57

How to set request attributes in JSP and how to get them in a servlet

I am setting request attributes in JSP. But I am not getting that request attributes in servlet it is getting null.

If I set like request.getSession().setAttribute(); it is working fine but request.setAttribute() means it is getting null.

How to set request attributes in JSP without session?

Upvotes: 1

Views: 44265

Answers (3)

Richard Sand
Richard Sand

Reputation: 674

What you want to do is in the JavaServerPages Standard Tag Library (JSTL):

http://docs.oracle.com/javaee/5/tutorial/doc/bnakc.html.

You want to use the c:set element from the core library and set scope to request. Example from the docs:

<c:set var="foo" scope="request" value="..."/>

or from the body of the tag:

<c:set var="foo">
     ...
</c:set>

Don't forget to declare the tag library at the top of your JSP:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

Hope this helps!

Upvotes: 0

Dileep
Dileep

Reputation: 5440

Request Set Attribute

request.setAttribute("message to be saved",variableName);
RequestDispatcher reqDisp = getServletContext().getRequestDispatcher("servletName");
reqDisp.forward(request, response);

By this you will forward the values to the next servlet

Request Get Attribute Example

<html>
<body>
<%
  String message = (String) request.getAttribute("message");
  out.println("Servlet communicated message to JSP: "+ message);

  Vector vecObj = (Vector) request.getAttribute("vecBean");
  out.println("Servlet to JSP communication of an object: "+vecObj.get(0));
%>
</body>
</html>

Upvotes: 2

SpringLearner
SpringLearner

Reputation: 13844

As far As i understand,you need to pass some values from jsp to servlet. I would suggest you to use session.setAttribute() and session .getAttribute();

In the jsp try

session.setAttribute("test","test");

in the servlet

session.getAttribute("test");

you will get test

Upvotes: 2

Related Questions