• Post Reply Bookmark Topic Watch Topic
  • New Topic

transferring an object from my jsp page to a java class

 
guillaume ciutar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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,
 
Paul Clapham
Sheriff
Posts: 21963
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
Balagopal Kannampallil
Ranch Hand
Posts: 136
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65661
129
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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 Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 21963
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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:


 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!