• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

transferring an object from my jsp page to a java class

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have a servlet that generate a list of objects named "Alerte".
I display this list in my jsp and I want the user to be able to delete one of them by clicking on it. Data are stored on Google Datastore.
My problem is that I don't know how to pass the current object from the loop of my JSP page to my java class.
Anyone have an idea?






Thanks,
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi gui, welcome to the Ranch!

The most important thing to remember about JSP is that it's producing HTML. In your case it's producing some HTML which displays a list of alerts in an HTML table.

So your idea that you're going to pass data from the HTML (which is displayed in the client's browser, remember) to a Java class is a bit off-course. What your HTML code needs to do is to send a request to a URL where a servlet will call that method to delete the specified alert.

You're part way there, you have a URL which will request whatever servlet the "delete" URL is mapped to. What's missing is that you have to pass the alert number as part of the request. So your task is to write some HTML which will do that. Don't think of JSP now, think of what HTML you need in the browser. Then the second step is to make your JSP generate that HTML.
 
Ranch Hand
Posts: 136
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
gui, welcome to the Ranch :D

As Paul suggested, once you have written the jsp which talks to the server side component. The functionality you want can be achieved by

1.Talking to your sever side simultaneously as you delete each row (call the server side url using javascript or jQuery and ajax). In this case, the page refresh won't happen and you can handle the row deletes from page and the actual data deletes from datastore asynchronously.
OR
2. You can delete the rows from the page using javascript (only row deletes from page). Fool the user to believe that he/she is actually deleting stuffs from the datastore. Instead, you will be storing the deleted ids in some page variable. Finally, request the user to click the save button which when clicked will call the server side url and pass this deleted ids as a bulk. The server side component will have the logic to delete the obtained ids it from the datastore and return back a refreshed page.
 
Sheriff
Posts: 67747
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
I would not recommend approach #2. Not at all.

And, why is there Java code in the JSP? :shock: It's 2014. Java code in a JSP has been obsolete and discredited for 13 years.
 
Balagopal Kannampallil
Ranch Hand
Posts: 136
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote: It's 2014. Java code in a JSP has been obsolete and discredited for 13 years.



Agree with you Bear.. :)

Sometimes it becomes too overwhelming for beginners to mix up different things at once.
 
guillaume ciutar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your fast reply paul !

[strike]In fact, my first idea was to add the key of my object alert with:

Google is using objects as storage key, it means I can't directly send the key of my object but i didnt' thought about sending the position of my object from the list.
I think I will try to send my request with a dopost method to the same servlet so I won't have to retrieve my list from the datastore again.[/strike]

SorryI'm just starting learning JEE, I know I should'nt put java in my jsp but don't know how to avoid if for the moment.
=> Trying #1
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I recommend that you send the alert's key as the request parameter. Clearly you can't send the alert object itself because HTML is only text. And then instead of this:



which you can't use because you don't have the "alerte" object, do this:



Also I don't understand why the choice between GET and POST, or requesting the same servlet or a different servlet, has anything to do with having to retrieve the list from the datastore.
 
guillaume ciutar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your advices, it work now :)
I redifined the getKey method in Alerte in order to return the string version of the key.

My servlet:


My jsp:


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic