wood burning stoves 2.0*
The moose likes Servlets and the fly likes request.setAttribute() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "request.setAttribute()" Watch "request.setAttribute()" New topic
Author

request.setAttribute()

vijaya vinayagam
Greenhorn

Joined: Dec 09, 2005
Posts: 21
Hi,
i'am having a jsp "index.jsp" and a servlet "firstservlet.java"
the control of clicking a button in "index.jsp" is perfectly transferred to "firstservlet.java" ( it is working good )

index.jsp
--------------------------------------------------------------------
<HTML>
<TITLE>
Test Servlet
</TITLE>
<HEAD>
<%
getServletContext().setAttribute("namecontext","vijay_ais");
request.setAttribute("name","vijay");
%>
<FORM name="test" action="/vijProject/firstservlet" method="get">
<INPUT type="text" name="txtname" >
<INPUT type="submit" value="SUBMIT">
</FORM>
</HEAD>
</HTML>
-----------------------------------------------------------------------



firstservlet.java
-----------------------------------------------------------------------
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

public class firstservlet extends HttpServlet
{
public void init(ServletConfig conf) throws ServletException
{
super.init(conf);
}
public void service(ServletRequest req, ServletResponse res) throws ServletException, java.io.IOException
{
System.out.println("JSP attribute "+req.getAttribute("name"));
}

}

-----------------------------------------------------------------------

Question

1) when i execute this the value of "req.getAttribute("name")" is returning null but i had set that to a value in the "index.jsp". why it is returning null???

2) when we say "request.setAttribute()" in jsp is it is servlet request (or) HttpServletRequest ( soory i'am new to servlets)


thansk in advance
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

There is a bit of a contextual shift you need to make when moving to HTTP applications, and I remember making taking that step myself.

You need to trace the behaviour from the point at which the client sends the request, ie:
* The client has a HTML page in front of them in the browser, it contains a form that will submit to the 'firstservlet' and include some data.
* the user hits the 'submit button. The data from the HTMNL page is converted to an HTTP request and sent to the server.
* The server receives the request, does its pre-processing, and eventually the ServletRequest gets passed to the 'firstservlet' class.
* Note that at this stage, the request object was created from the HTTP request sent by the browser. It is not the same request object you set the attribute on, hence that value does not exist.
* At this stage you would look at the data sent by the client, choose a response and then send it to the client.
* the client gets a new page to look at, when they perform an action the cycle starts again.

It is imporant to get your head around the concept that all of the 'java stuff' only happens from the point the request is received by the server to the point the response is sent. Any Java code in JSPs occurs on the server before the response is sent to the client, it does not occur on the client.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

2) when we say "request.setAttribute()" in jsp is it is servlet request (or) HttpServletRequest

Depending on the request, it could be both

Think of it this way: the basic behaviour of a servlet is to receive 'requests' nad return 'responses'. In it's most basic form the Servlet does not specify what form the request and response take. In the javax.serv let package you have the Servlet interface which defines this 'common' behaviour, and the 'GenericServlet' which implements some of this behaviour in a protol independent manner.

The most common implementation for Servlets is to accept requests via HTTP and send responses the same way. Now we move to the javax.servlet.http package. The classes and interfaces in this package extend the basic behaviour with HTTP specific behaviour.

Hence (for example) the HttpServet extends the GenericServlet, HttpServletRequest extends ServletRequest and HttpServletResponse extends ServletResponse.

So, in a HttpServlet, the request is a HttpServletRequest, which extends ServletRequest, so therefore the request is both.

Dave
vijaya vinayagam
Greenhorn

Joined: Dec 09, 2005
Posts: 21
Thankyou sir,
i got a clear answer, one more question.
Actually once the control is transferred to "firstservlet.java" it responses to the request by
"getServletContext().getRequestDispatcher("/result.jsp").forwardreq,res);" i had added a Dateheader to "firstservlet.java" as "res.addDateHeader("If",455555L);" when i tried to retrieve that in the "result.jsp" it by "request.getDateHeader("If")" it gives me "null" why so ???
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

No 'sirs' around here, we all work for a living

Again you're mixing your client and server parts.
The request has information sent by the client, the response has information sent to the client.

res.getHeader(...) will only return a value if it was sent by the client, which it was not. If effect you're sending a header value to the client and then (in the same breath) asking if they sent it to you, which they didn't.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
Response headers go from the server to the browser and request headers go from the browser to the server.

I suggest you to download LiveHttpHeaders - a mozilla Firefox plug-in with which you can have a better understanding of how request/response cycle work s with all the headers.

Have a look at this thread.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
vijaya vinayagam
Greenhorn

Joined: Dec 09, 2005
Posts: 21
Thanks to David O'Meara & Vishnu Prakash,
for spending time and explaining to me
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
After going through all the discussion, I came up with this conclusion:

It is useless to call setAttribute() method at client side because this request will not go again to the server, a new request will go to the server so doing this will not make any difference.

But one question came in my mind:

What if I want to send something from client to server in request. So how to do so?

1] Through 'form hidden parameters'...

But what if I need to send objects???

I don't want to get it through session or other objects...

Please reply.

Thanks.

Manesh Kumar
Ranch Hand

Joined: Mar 21, 2006
Posts: 94
Originally posted by rathi ji:
After going through all the discussion, I came up with this conclusion:

It is useless to call setAttribute() method at client side because this request will not go again to the server, a new request will go to the server so doing this will not make any difference.

But one question came in my mind:

What if I want to send something from client to server in request. So how to do so?

1] Through 'form hidden parameters'...

But what if I need to send objects???

I don't want to get it through session or other objects...

Please reply.

Thanks.



You need to use session.setAttribute(key,value) to share the objects between the requests.


Manesh
geeta lalchandani
Ranch Hand

Joined: Jun 07, 2005
Posts: 118
dilip..
i think you got rathiji wrong...

he means to say. how can he set attributes in a request..(attributes that are objects) and doesnt talk to sharing the attributes between requests.
Manesh Kumar
Ranch Hand

Joined: Mar 21, 2006
Posts: 94
Originally posted by geetu lalchandani:
dilip..
i think you got rathiji wrong...

he means to say. how can he set attributes in a request..(attributes that are objects) and doesnt talk to sharing the attributes between requests.


Thanks Geetu.

There is one method which you can try to send the objects and retrieve it back ( I dont know whether it works), though I dont recommend. This method will just send the object as a String to client and reconstruct them again. You cant manipulate the objects in the client side.

You need to serialize the object to ByteArrayOutputStream and and then calling toString() method on the ByteArrayOutputStream object. Then printing the String value directly to the hidden form variable's value attribute.

Like this.


While again constructing again.
<%
String objAsString = request.getParameter("objectStream");
ByteArrayInputStream bais = new ByteArrayInputStream(objAsString.getBytes());
ObjectInputStream ois = new ObjectInputStream(bais);
SerializableObject objectConstructed = (SerializableObject) ois.readObject();
bais.close();
%>

Hope this works and this is what you had expected.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Thanks Dilip.

I got the logic.

geeta lalchandani
Ranch Hand

Joined: Jun 07, 2005
Posts: 118
There is one method which you can try to send the objects and retrieve it back ( I dont know whether it works), though I dont recommend.


as you said, its not recommended. I am not convienced that this is the only way out to set attributes in request.Please do let me know if there is an alternative to this
Manesh Kumar
Ranch Hand

Joined: Mar 21, 2006
Posts: 94
Originally posted by geetu lalchandani:


as you said, its not recommended. I am not convienced that this is the only way out to set attributes in request.Please do let me know if there is an alternative to this


Yes. You can serialize the object to the database as BLOB when request is going to be completed.
And deserializing the object again from the database as sson as the request comes. You need to use sesion id as key. This is will not use session.

Of course, this adds overhead of using JDBC but this is more optimized than hidden variables since usually the servers are placed in the LAN and communication is faster between them.

This might also add database cost in terms of extra size required for storing the objects.
 
jQuery in Action, 2nd edition
 
subject: request.setAttribute()
 
Similar Threads
Retaining drop down values on page submit
Need help with a servlet problem
Html Input to oracle through servlet and JSP output
passing parameters from JSP to a Servlet
Getting the name of the requesting page