• 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

Pass value and Validate

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the sake of simplicity, I created a shortened version of my original code. Basically, I have a static drop down list and once the user submits the form, it will pass the value to the page and display it. The problem I have is when I try to validate the value. If value = this, then do this, else do that. The code is below.

t.jsp


Please let me know why I cannot validate it correctly. Thanks.
 
Sheriff
Posts: 67746
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
First of all, what is Java code doing in the JSP? That's an obsolete technology from 12 years ago. You should be writing JSPs using the JSTL and EL, not Java scriptlets.

Secondly, JSP is a server-side templating mechanism. The JSP runs on the server in order to create the HTML page to send to the browser. So you've got the cart ahead of the horse here -- the validation code is running before the page is even sent to the browser.

Also it appears that the JSP is submitting to itself. Validation code should be in the servlet to which the form is submitted. Forms should never be submitted to a JSP.

It amy help to understand how JSPs operate, by reading this article.

Modern web app structure is the subject of this article.
 
Vycter Vens
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply and sent a request to have the name changed. I do have the JSP running on my server, but I just wanted to keep the post simple. In the original, I establish a database connection and I am able to pass the value to my SQL query, but I just cannot get it to validate the value that is passed. My application server that I am using is WebSphere. Based on the value that is selected from the Drop Down List, it gets passed to my JSP page, then I try to validate the value that was passed and based on the value, it will do either 1 thing or another (my if statement).
 
Bear Bibeault
Sheriff
Posts: 67746
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
The first refactor should be to move the code out of the JSP and into a separate servlet.

Then, change the form action to submit to the servlet.

Use a debugger or logging in the servlet to see what values are being submitted and if they are the values that you expect. You can also use the browser's debygger to see what HTTP values are being passed even before they get to the servlet.
 
Vycter Vens
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.

Although it may not be the best practice, would it even be possible to do it the way I have now? When I first hit the page my output is:

null 1selectedValue
1yearq
IF not ALL, then new statement 'null'
null 2selectedValue
new statement 'null' 2yearq

This makes sense because my value from request.getParameter("year") is null. However, once I change the values and submit, the values do change, but it still displays the "IF not ALL" even when I choose the "All" from drop down, whereas it should display the "If All, then ..." right?
 
Bear Bibeault
Sheriff
Posts: 67746
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
Possible, yes. But highly, and I do mean highly, undesirable. Why the unwillingness to try and do things the right way? (A frequent answer is "I'm just leaning" and that just doesn't cut it. Learning to do things the wrong way is never a good first step.)
 
Vycter Vens
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is true that I am learning. Definitely not a expert developer. For me, it is to understand why this will not validate and then try to get it to a state that it does work (the undesirable way and probably a bit less efficient than today's standards). Then I can rebuild it with better development practices and understanding and use it to compare the difference between the desirable way of building something versus the old and undesirable way of how things used to be built.
 
Bear Bibeault
Sheriff
Posts: 67746
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 will repeat once again that I do not feel there is any value in pursuing this course of action, and then I'll shut up about it.

Are you expecting the "validation" to take place the first time that the form is displayed? Or after submission? The first time through, there are no parameters.
 
Vycter Vens
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do appreciate your responses and understand what you are trying to convey, but me personally, i learn through compare and contrast. when the form first loads, the value is null, which is okay. I am expecting the validation (my IF statement) to perform after the Submit is clicked by the user. For some reason that I do not understand, I can see the value through my debugging of using the out.println, but it does not work correctly. when i choose, "All" the value is passed as all, but it does not work with the If-Else.
 
Bear Bibeault
Sheriff
Posts: 67746
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
Stop a moment and think. Just because the Java is in a JSP (which makes me cringe -- sorry, I said I'd shut up) doesn't change how Java operates. Look carefully at your string comparison.
 
Vycter Vens
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am glad that you did decide to reply. I've been reading the method to perform a string comparison and it seems that I was somewhat right. as == (the comparison that i've always used to compare) compares reference equality and .equal() compares for value equality, which is the one that I believe I will need in my case. i didn't even know about the .equal() method, but will have to try it when I get to my desktop. i think this might solve my problem.

if(selectedValue.equal("All"))

Thanks for the helpful hint. I will test out and let you know what happens.
 
Vycter Vens
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again. It did fix the issue. Now I can concentrate on how to rewrite this to more of today's standards. I am not sure how to do JSTL or EL, so I will need to do some research.
 
Bear Bibeault
Sheriff
Posts: 67746
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
Good plan. You know where to come with questions!
 
Their achilles heel is the noogie! Give them noogies tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic