Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

query using jsp & beans

 
sidharth johar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question in regards to the JSP and Java beans. I have a form which takes the input parameters (in a JSP) and does the buisness logic using the beans class where in I have the approppriate getter,setter methods for the parameters within my form. The issue that I am having is, the moment I call my validation function in the JSP (using the usebeans tag ofcourse), at first it is taking the input values as null and I am getting the inappropriate validation output.
All I am saying is I am not able to get the validated correct output immeadiately after submitted the form. What I am getting is a false output for the null parameters automaticlaly supplied and then when I supply my input, I am getting the appropriate output. How do I make sure that I do not get the invalid output at first.
Any example would be appreaciated. Is that got to do with the usebean tag parameters or exactly what do I need to change so that I get the appropriate response. Please note, there is only one form here and I am doing the validation using a bean class.

Thank you.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you doing the validation in the same JSP as the form? If so that's the issue. The validation is running before the form even gets sent to the browser!

That's not an appropriate or best practice way to structure a web app. Your form should be submitting to a servlet, not a JSP, for validation.

Perhaps this article could help bring you up-to-speed on modern web app structure.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

could you post the involved code snippets from both the file that contains the form and the validating JSP page ?
 
sidharth johar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry I cannot post the exactly code snippet but its some thing like this...

(a) form.jsp page...


In the above form, I have two parameters which are input1 and input2 and onsubmitting, I am posting that to the same page....I donot know how far that is correct...

myBean.java class has a verify method which takes the input parameters by saying getInput1() and getInput2() respectively and validate by manipulating those parameters and return true / false based on the business logic.

In the scriptlet of the JSP, I am printing out the output based on the boolean obtained as a result of verify funtion obtained.
I am sorry, I am not authorzed to put up the snippet, but I guess this should be good enough to have a glimpse of my template.


Thanks.

 
sidharth johar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no bear...like I said, I am doing the validation using a bean class which does all the business logic for me using a function which returns true/false....
and this result is fetched in my jsp(which has the form..) and I have set of println statements based on those results of the function...
It will be great if you could let me know how to proceed smoothly in this issue...

Thanks.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that JSP the direct action of the previous form?
Also, does your bean have the appropriate setter methods?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear is right, basically you should almost never directly trigger a jsp file.

The general procedure is: Servlet -> Validation & Business Methods -> JSP's for display
 
sidharth johar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I had in mind....actually in my jsp, I have the input form which takes two values...as I said....and the action is done on the same page....that is..I am actually posting it to the same page...I suppose that is a mistake...I am not sure....I want somebody to correct me....Do I need to post that to some other page and call the function that I want to ??? Cant I just have a single jsp that does all of this for me? I am neophyte in this. So please bear with me.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could happily organize a whole web application in a single JSP, but that's stupid and won't lead to success.

You always strive for a separation of concerns. That means, separating business logic from the presentation. And that means, a form gets submitted to a servlet, the servlet does it's validation and business logic, and then forwards to the JSP (which could be the same JSP you used for the form, but it doesn't make much sense to me, depends on the case) which displays the results generated by the servlet.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sidharth johar wrote:and the action is done on the same page

Which is the problem.

I suppose that is a mistake

Yes. It is.

I want somebody to correct me

Did you read the article I linked to? It will correct you.

Do I need to post that to some other page and call the function that I want to

You should never post to pages. Post to servlets.

Cant I just have a single jsp that does all of this for me? I am neophyte in this. So please bear with me.

Then it's important to learn proper habits at the outset. If you are a novice to JSP, I'd recommend this article as well.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic