• Post Reply Bookmark Topic Watch Topic
  • New Topic

Carrying wrong ID from JSP to Servlet  RSS feed

Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys.

I am currently working on a webshop where a customer can select a pre-defined carport and purchase it.
On the page all carports are displayed by using a loop that receives an array with all carport information (I am using MYSQL)
It's for a school project, so I have to use JSP.

The page correctly shows all the carports along with their respective ID's, but when a customer selects a specifik carport and presses "buy" (køb)
no matter what carport you choose, it always chooses ID 1.



Best regards
Ranch Hand
Posts: 30
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- Your code in JSP have multiple problem about put the tag in wrong position.
- You can not put a form direct inside a table row like you did. Refer to this
- A sample edit for your code to submit the value you want is like this:

- But you should not do like this because it will create a blank cell in your table, let's try to think about non-table presentation? or try to hide the last column?
Posts: 1844
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a few suggestions.  I've tried to separate them out. 
Implement one or all of them within your constraints.

First thing.

You have written this whole thing using <% scriptlet tags %>.
Scriptlet tags are bad ok?

Lets introduce you to the JSP Expression tag.  <%= expression %>
The expression tag effectively does an out.print of the expression you place between the tags.

Rather than write:

It would be preferable to have

There.  We've gotten rid of a whole lot of unnecessary boilerplate code.


The other thing I don't like is your continued use of carports[i].
At the least, declare a local variable for it and assign it at the start of your loop:


However we can even do better than that using JSTL and EL

It isn't exactly the same because we need an instance of the userServlet rather than what looks like a static import, but that is the way I would want it to look.

Ideally then we could use an EL expression rather than a scriptlet expression
That would look something like:

By using these tools, you can get rid of practically every scriptlet on your page.

Rather than putting the customerId into an input type="hidden" populated from the session, remove that field from the form, and in your servlet retrieve it from the session directly.
int customerID = Integer.parseInt(session.getAttribute("customerID"));

I don't know what customerID is.  If it is an Integer already in the session, then you don't need to parse it.

hope this helps,

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!