Hello, I am new to JSP. I need to develop a web system using JSP. I have a few doubts that I couldn't figure out.

I understand how to write jsp page by embedding the jsp coding in the same page as the html form. I know this is not a good practice. I want to separate the logic and the form in different page but I don't know how to display the error message on the html form page after validate using the jsp logic in another page.

Beside that, I have try to use java bean to store the logic in java coding and called it using <jsp:useBean>. However, it doesn't work after I insert the <jsp:useBean> tag. It works when I try the example program but somehow it didn't work when I do it myself.

Can anyone give me any advice? Is there any rules in using the <jsp:useBean> so that it doesn't cause any error?

Thank you.

Recommended Answers

All 4 Replies

I think you need a better undestanding first of the JSP programming model.

What exactly do you think will happen when you include a useBean tag for example?

Normally you'd put your business logic in a set of servlets and the display logic in JSPs.
The JSPs produce html which contains forms and links which call servlets.
The servlets process the data and forward to JSPs.

By using a framework like Spring-MVC you get a lot of that almost for free, hidden as it were from you so you can concentrate on just the business and display logic.
It will also give you ways to nicely show errors automagically without a lot of fumbling with stuff on your end.

Can you provide more specific details about what you have done, are trying to do?

If you separate the logic and presentation then you need to pass state between the logic component and the presentation component. this would include a flag to indicate an error condition and/or the error message... though it is common to have the logic forward you to an error page
i.e. you business logic could be like this...

if there is an error
forward to /WEB-INF/error.jps?error=invalid-input
otherwise
forward to /WEB-INF/results.jsp

writing out your logic in this human readable format prior to coding is a big help in ensuring the business logic is, well, logical...

you can use a bean, save the error message in the bean, and have it "display" on the page everytime.... by default the message can be blank so unless an error just occured there is nothing to show on the screen... this is a simple method...
if the bean has scope greater than request, you should be sure to clear the message just after reading it each time...

in the example code you have, the bean is saved to a scope... request, session or application... the default is page which means that it doesn't exist once you forward the request on to the JSP page... be sure you are storingthe bean into a scope that persists long enough to be available to your JSP and include the scope paratmeter in your useBean tag...

<jsp:useBean id="beanName" class="package.className" scope="request"/>

<span class="errorMessage"><jsp:getProperty name="beanName" property="message"/></span>

<jsp:setProperty name="beanName" property="message" value=""/>


Hope that helps... otherwise, just send me more information, I will see what I can do...

Thank you Jwenting and rgtaylor. I have solved my doubt already. Thank you very much :-)

Glad to hear it, it would be great if you could share a short message about what the problem turned out to be, so others who read this, who perhaps have similar issues, will learn from your experience too...

Thanks & Good Luck,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.