• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Passing Vector Object

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Nilesh Srivastava
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Nilesh Srivastava
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Nilesh Srivastava
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ben, this is one more misconception that we forgot.
 
reply
    Bookmark Topic Watch Topic
  • New Topic