This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JSP and the fly likes Access jstl variable in Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Access jstl variable in Servlet" Watch "Access jstl variable in Servlet" New topic
Author

Access jstl variable in Servlet

Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
Im populating customer data by iterating a customer list as following in the jsp.



Now in the action either edit or delete each record i want to track which customer needs to be edit/delete. So in servlet how can i access the customer object which is in var listItems. If its not possible do i need to have hidden input variables with values and access them using getParameter in servlet?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Yes, only form elements are submitted as part of a form.

Remember that once the response leaves the server that it's just an HTML page. So there's no way that a JSTL value (or any other server-side value) can be submitted with a form that only exists on the client.

Please read this article to understand how JSP operates.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
Bear Bibeault wrote:Yes, only form elements are submitted as part of a form.


Yes but defining hidden input variable does not gives a solution for identify which customer detail like fname to either edit or delete that record. Because it will eventually have a list of hidden input variables with same name and different values.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

How is the user choosing which of the displayed items is to be submitted?
Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
Bear Bibeault wrote:How is the user choosing which of the displayed items is to be submitted?


Thank you for the answer. Its like a data grid where each customer records are displayed with edit/delete button next to it. So once the user click on delete for example that cutomer information should be delete. So for that need to send the fname for the servlet to process know. That's where i need to know how to track it.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

fname? What's that? Is it the primary key for the database record?

In any case, you need to send the primary key for the record to identify which one was chosen.

This is not a JSP issue, it's an HTML issue. How would you construct the HTML, and perhaps JavaScript, of this page to be able to submit the primary key of the record?

Once you've answered that question, then what you need to do in the JSP becomes clear.
ronney singh
Ranch Hand

Joined: Feb 10, 2012
Posts: 40

I also got this kind of problem few days back I can tell you what I did.
Instead of using 2 buttons for edit and delete, I used 2 link like this-

<a href="welcomeC.do?mode=e&img=${LoginCBean.image}">Edit</a>

here you can send your primary key and variable(here it is mode) which tells servlet that edit is clicked or delete and you will be able to do coding according to that variable.


Live Life King Size
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

I would not recommend this approach. Links are a poor substitute for buttons in a UI.

I'm still waiting for answers to my questions before I can make any recommendations.
Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
Bear Bibeault wrote:I'm still waiting for answers to my questions before I can make any recommendations.


Yes Bear Your right. Using javascript i have set the action clicked customers primary key to a hidden variable.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access jstl variable in Servlet