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.
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).
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.
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:
IF not ALL, then new statement 'null'
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?
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.)
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.
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.
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.
Thanks for the helpful hint. I will test out and let you know what happens.
posted 5 years ago
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.