wood burning stoves 2.0*
The moose likes JSP and the fly likes Pass value and Validate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Pass value and Validate" Watch "Pass value and Validate" New topic
Author

Pass value and Validate

Vycter Vens
Greenhorn

Joined: Feb 15, 2013
Posts: 7
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vycter Vens
Greenhorn

Joined: Feb 15, 2013
Posts: 7
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

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

Joined: Feb 15, 2013
Posts: 7
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

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

Joined: Feb 15, 2013
Posts: 7
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

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

Joined: Feb 15, 2013
Posts: 7
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

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

Joined: Feb 15, 2013
Posts: 7
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

Joined: Feb 15, 2013
Posts: 7
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Good plan. You know where to come with questions!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pass value and Validate
 
Similar Threads
Http Status Error 405 : get Method Not Supported.
Unexpected tag <TABLE> found ,expecting one of <TD> ,<TH>
help with validation/ 2 functions on submit.
Problem in using date in WHERE clause in SELECT query in Servlet.
Two actions in a single JSP