File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes query using jsp & beans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "query using jsp & beans" Watch "query using jsp & beans" New topic
Author

query using jsp & beans

sidharth johar
Greenhorn

Joined: Jul 08, 2009
Posts: 5
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

Joined: Jan 10, 2002
Posts: 61611
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Hey,

could you post the involved code snippets from both the file that contains the form and the validating JSP page ?


JDBCSupport - An easy to use, light-weight JDBC framework -
sidharth johar
Greenhorn

Joined: Jul 08, 2009
Posts: 5
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

Joined: Jul 08, 2009
Posts: 5
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

Joined: Feb 23, 2009
Posts: 1183
Is that JSP the direct action of the previous form?
Also, does your bean have the appropriate setter methods?
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
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

Joined: Jul 08, 2009
Posts: 5
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

Joined: Feb 23, 2009
Posts: 1183
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

Joined: Jan 10, 2002
Posts: 61611
    
  67

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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: query using jsp & beans