aspose file tools*
The moose likes JSP and the fly likes Passing Vector Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Passing Vector Object" Watch "Passing Vector Object" New topic
Author

Passing Vector Object

Nilesh Srivastava
Ranch Hand

Joined: Aug 29, 2003
Posts: 70
Hi All,

Can anyone tell me how to pass a vector Object from a JSP page to Servlet.

I dont want to use session for storing my Vector Object.

I stored the Vector Object in request as

request.setAttribute(VectorObject);

and when I post my form, the vector is not available in servlets.
(I dont know why !!!)

Now my aim is to pass a Collection Object from JSP to Servlet.

Plz Help !!!

Regards,
Nilesh
Vijay Kiran
Greenhorn

Joined: Nov 18, 2004
Posts: 21
May be you should put your object in a bean using usebean and then access it from the servlet.

What's the error you are getting in the servlet when you are trying to access the vectorobj?

Can you give some more details regarding the jsp->servlet call?


Vijay<br />I blog, therefore I am<br /><a href="http://www.vijaykiran.com" target="_blank" rel="nofollow">www.VijayKiran.com</a>
Sandeep Saha
Greenhorn

Joined: Mar 28, 2005
Posts: 5
Its quite tough to say without looking the code but what u can do is just cast the variable to vector in the servlet and you will get the values. you have to cast as getAttribute() will return String(Object).

Hope casting will solve the problem.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12789
    
    5
Any reference stored in the request will only be valid during that one request-response cycle - that is the whole idea. A response to a form constitutes a new request-response cycle.

Objects that need to be available during a user session belong in the session. Exactly why don't you want to use the convention?
Bill
kalyan kumarG
Greenhorn

Joined: Mar 28, 2005
Posts: 7
store the object in the request object of the jsp and dispatch the control to the servlet. now retrieve the object from the HttpServletRequest object of the servet.

if this is what you have done then, it should work.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61232
    
  66

William has the correct solution. No amount of casting or any other shenanigans with the request is going to allow you to retrive an object that has gone out of scope. Any scoped variables placed on the request are gone once the page is sent to the client. Period.

The only real choices you have are to: 1) use the session, 2) use hidden form elements on the page to pass the info onto the next request. The former is a far, far better solution.

So I repeat William's question: what's your beef with the session?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nilesh Srivastava
Ranch Hand

Joined: Aug 29, 2003
Posts: 70
Hi All,

Thanks for ur valuable suggestions. I am posting a snippet from my code and the flow of my program.

<form name = "mapColumns" method="post" action="ADPPort?task=portData">

.............(Performed some task)
.............

Vector vecHeaders = (Vector)request.getAttribute("vecHeaderName");
request.setAttribute("vecHeaders",vecHeaders);
Vector vecColumnNames = (Vector)request.getAttribute("vecColumnName");
(These are the two vectors which I passed from servlets to JSP using REQUEST object)
(Also I am setting one of the vectors in REQUEST object.)
...............
...............

<input type = "submit" name="port">
(Here I am posting the form to ADPPort which is mapped to a servlet.)

Now on Posting the form I am trying to retrieve the value in Servlet whose snippet is :

if (task.equals("portData"))
{
Vector vecHeaders = (Vector)request.getAttribute("vecHeaders");
System.out.println(vecHeaders); // Here I get null.
}

I cannot forward my request from JSP to this servlet untill post my form.

I dont want to use session objects because later on in my application, I ahve lots of such vectors to be passed from JSP to vectors... This way my session will get bulky.
Also, I had read somewhere that excessive use of session Objects results in lazy applications.

Once again thanks to u all for ur valuable suggestions.

Please provide some code snippet with ur suggestions.

Thanks in advance.

Due Regards,
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Once the HTML is written the the user's browser, the request is gone.
Anything in it is gone.

When the user posts the form a new request is started.
If you want that vector to survive multiple request/response cycles, you will need to put it in session.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Nilesh Srivastava
Ranch Hand

Joined: Aug 29, 2003
Posts: 70
Can't I pass the vector object from JSp to Servlet like I do the String object. ie through URL, hidden fields or any other way.

Any other mean thru which I can pass VEctor Object from JSP to Servlet WITHOUT using sessions. I am not against storing values in session , but I know that way I can do it.. I just want to know if there are any other ways to do the same.

Thanks in advance,
Sarath Mohan
Ranch Hand

Joined: Mar 17, 2001
Posts: 213
Nilesh,

As soon as user submits the form that will be new Request which doesn't contain your Attribute.

You can't put anything except StringType in URL or hidden fields.

1. You can put the attribute in session and remove it as soon as its use is over.

2. Re think about logic behind this attribute. How are you getting this? Can't you do this at the servlet side according to the condition you require.

Regards


Sarath Mohan
Nilesh Srivastava
Ranch Hand

Joined: Aug 29, 2003
Posts: 70
Sarath,

Can we kill a particular session object. I think we have to kill the entire session itself. which results in lost of other objects stored in session.

Can you please tell me how to go about your second point
(
2. Re think about logic behind this attribute. How are you getting this? Can't you do this at the servlet side according to the condition you require.
)

How will you do this from servlet side..??

Thanks
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12789
    
    5
Can we kill a particular session object. I think we have to kill the entire session itself. which results in lost of other objects stored in session

I can't imagine where you got that idea. Just read the JavaDocs for HttpSession where you will find the removeAttribute() method. You will also see that you can setAttribute with a null reference to get the same effect.

It is senseless to try to program servlets without having the documentation on hand and depending on "I read somewhere."
There are all sorts of classes and methods in the standard API for management of sessions - we talk about them all the time here.
Bill
Ed Wallen
Ranch Hand

Joined: Feb 11, 2002
Posts: 34
Create an intermediary between your application code and the HttpSession (meaning a class that manages all of your getAttribute/setAttribute calls). This intermediary will be the only object in your web app that has knowledge of where your Vectors (or any other object) are stored within the session..............so if you wanted to make sure that you are only keeping X number of Vectors in your session at a time, your intermediary can manage this using several different strategies. You could always have the intermediary set the Vector in the session using the same key, ensuring that only one Vector is maintained. Another way is to have the intermediary actually store a java.util.Map in the session and use the Map to house your Vectors. When you wanted to get rid of all of your vectors, you could just retrieve the Map from the session and call clear() on it. Hope this gives you some useful ideas.

-Ed
Heonkoo Lee
Ranch Hand

Joined: Feb 10, 2005
Posts: 85
When a JSP page is forwarded by calling requestDispatcher.forward(request, response) from some servlet/JSP, it is translated into servlet code and converted to servlet class file first time it is requested. So, all the code processing of the JSP(1) is done at the server side and then the resulting output stream is sent to the browser (response committed) unless you forward it to another servlet or JSP page (in this case the buffer for the JSP(1) is discarded).

Originally posted by Nilesh Srivastava:

.............(Performed some task)
.............

Vector vecHeaders = (Vector)request.getAttribute("vecHeaderName");
request.setAttribute("vecHeaders",vecHeaders);
Vector vecColumnNames = (Vector)request.getAttribute("vecColumnName");
(These are the two vectors which I passed from servlets to JSP using REQUEST object)
(Also I am setting one of the vectors in REQUEST object.)
...............
...............

<input type = "submit" name="port">
(Here I am posting the form to ADPPort which is mapped to a servlet.)

The request object here is the same as the one in the forwarding servlet. And the request dies in this page.


Originally posted by Nilesh Srivastava:

Now on Posting the form I am trying to retrieve the value in Servlet whose snippet is :

if (task.equals("portData"))
{
Vector vecHeaders = (Vector)request.getAttribute("vecHeaders");
System.out.println(vecHeaders); // Here I get null.
}

When the form is posted by clicking submit button, a new request that does not know about "vecHeaders" attribute is sent to the server. So, the servlet should print null.


Originally posted by Nilesh Srivastava:

I cannot forward my request from JSP to this servlet untill post my form.

You cannot forward request to another resource such as html, jsp or servlet while printing your html form to the browser at the same time. You must either commit the response to the browser or forward to another resource.


Originally posted by Nilesh Srivastava:

I dont want to use session objects because later on in my application, I ahve lots of such vectors to be passed from JSP to vectors... This way my session will get bulky.
Also, I had read somewhere that excessive use of session Objects results in lazy applications.

That's true. No one wants to clutter up memory resources with tons of session objects. Rome was not built in a day. Neither are non-lazy developers.

If you don't mind, I would like to recommend a book about servlet/jsp. Core Servlets and JSP by Marty Hall.

Regards,

Heonkoo

[ March 29, 2005: Message edited by: Heonkoo Lee ]

[ March 29, 2005: Message edited by: Heonkoo Lee ]
[ March 29, 2005: Message edited by: Heonkoo Lee ]
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Ben, this is one more misconception that we forgot.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing Vector Object