Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

trouble using if decision statement in a jsp page  RSS feed

 
Armand Deluvak
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My html form page has a number of text fields of data to submit to a database. It also has five buttons which will determine what action is to be taken on that data. The html page uses <method=post action=decision.jsp>. The decision.jsp page is only used to determine which other .jsp page would redisplay the form with its processed data.
I am frustrated with making the decision.jsp page work. My latest effort uses the following "if" statement construction:

<% if( request.getParameter("Add") == "Add" ) { %>

<jsp:forward page="add.jsp"/>
<% } %>
<% else if( request.getParameter("Update") == "Update" ) { %>

<jsp:forward page="update.jsp"/>
<% } %>
...and so on...

Is this the proper way to make the choice and also to get another .jps page to display?
I have tried to test this page by having it display an ordinary html page that says "I am the add.jsp page" or "I am the update.jsp page" but it won't even do that.
Thank you,
Armand
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing I can see that's the possible source of your problem is that you are not testing the same request parameter in your if's. For this strategy to work, you should have a single request parameter (somthing like "action" perhaps) that you can test for "add", "update" and so on.
But if it were me, I'd back up a bit and re-evaluate how you are doing this. You really don't need the extra overhead of a "decision page" to direct the submission of your form to different targets.
You can use simple Javascript to achieve the effect you are after. In a nutshell:
1) Your buttons can call an onClick handler with a parameter identifying the action to be performed.
2) In the onClick handler function replace the action attribute of the form with the URL of the target page that is appropriate to the passed action.
3) The function can then submit the form.
hth,
bear
[ May 04, 2002: Message edited by: Bear Bibeault ]
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I'm doing this sort of thing, I always have a hidden field on the page, called "cmd" (command) and it's what my servlet uses to determine what logic to perform, and also the 'page' to forward to.

So I agree with Bear, use a single parameter with different values, not different parameters.
 
Armand Deluvak
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear,
Thank you for your reply. Problem is that this is for a class project that is requiring it be done using jsp and javabeans. As I don't know javascript anyway, I'm going to keep plugging away at a jsp solution.
The logical thing to do, it seems to me, would be have each different button target a different jsp page, but as forms can't be nested in an html page, it doesn't look like that could be done. If you know a way to do that, I'd be interested.
Thanks,
Armand
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Armand, you may also want to use the equals() method in your comparison instead of "==".
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!