• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How i will display error details in the same jsp page

 
sumanta panda
Ranch Hand
Posts: 224
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,
I have a jsp page let say login.jsp and a servlet class Login Servlet.java.

My doubt is when request goes to servlet and servlet class do all database
validation.if let say login id is invalid how can i display same message
in jsp.Example once submitting of userid and password if user is wrong
then it will in same jsp page "Invalid User id".

Could you please guide me how i will write the code in servlet as well as jsp page.


My servlet class is

[BPSouther: Added code tags]
[ December 08, 2008: Message edited by: Ben Souther ]
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think including a jsp page at the desired position using the <c:if> tag is one of the option, when you don't want any other jsp. Correct me if i am wrong.

Thank you.
[ December 08, 2008: Message edited by: Sudipto Shekhar ]
 
sumanta panda
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sudipto,
when user input userid and password then it will go to server class do all database validation and check if userid and password is correct go to success page else same server side error will diplay in the jsp page.
Please advise me for the needful.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the validation fails, bind an error message to request scope and forward back to the JSP with the form on it.
In that JSP, have an EL expression that writes the message to the page if it isn't empty.
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sumanta panda:

when user input userid and password then it will go to server class.....else same server side error will diplay in the jsp page.



Sorry i did not get you...

 
sumanta panda
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sudipt,
Ben is right to explain my doubt.
If the validation fails, bind an error message to request scope and forward back to the JSP with the form on it.
In that JSP, have an EL expression that writes the message to the page if it isn't empty.

But Sudipt,
I dont have any idea about EL expression.Could you please
advise me how to do it simple JSP and Servlet.
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sumanta panda:
Hi Sudipt,
Ben is right to explain my doubt.
If the validation fails, bind an error message to request scope and forward back to the JSP with the form on it.
In that JSP, have an EL expression that writes the message to the page if it isn't empty.

But
I dont have any idea about EL expression.



If you don't have any idea at all about EL then how do you know he is right (though the brief explanation is the exact answer....)??
I'll surely help you but give me some time....

Thanks. Bye.
[ December 08, 2008: Message edited by: Sudipto Shekhar ]
 
sumanta panda
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sudipt.I am waiting for your response.Hope you will better to guide me.

Thanks a lot in Advance.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator





Here's a scriptlet that shows the basic idea.
Note that commented code on line 7 would forward the user to a welcome page
after a successful login; if there was one.
[ December 08, 2008: Message edited by: Ben Souther ]
 
Udayaditya Barua
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Same as above except you can also try

 
Srikanth Nutigattu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

<%=request.getAttribute("error_message")%>



It is not advisable to use scripting inside JSP's, so better stick to the
EL version instead

${message} if message is present it will be printed, else if message is null or not found nothing will happen.

Thanks.
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Understanding EL properly is very important.... They are sleek,easy and the most important is that it removes scripting from JSP. So Sushanta don't try to skip a bit of it.

Thank you.
 
sumanta panda
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sudipt/All,
I have trying a lot but still it is not working.errormessage value is comming null.Kindly help me for the needful.

Servlet class
if(errorType.equals("S")) {
pw.println("Welcome");
}
if(errorType.equals("E")) {
request.setAttribute ("errorMessage", "Invalid username or password");
}

-------------------------------------
x.jsp


if(request.getAttribute("errorMessage") != null)
{


<%=request.getAttribute("errorMessage")%>



}
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sumanta panda:

if(request.getAttribute("errorMessage") != null)
{


<%=request.getAttribute("errorMessage")%>



}



Why are you putting this mess in your JSP page?
Did you try running the example page that I gave you?

If so, what part of it are you having trouble understanding?
 
sumanta panda
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Sir,
According to your advice i try it.But its not working.Could you please tell me for express language should we include any thing or set up anything in the tool.

Servlet Class

if(null != request.getParameter("Submit")){
if(errorType.equals("S")) {
pw.println("Welcome");
}
else {
request.setAttribute("errorMessage", "Invalid username or password");
}
}

-----------------------------------------------------

x.jsp

<div style="color:red">${errorMessage}</div>
<input size="20" type="text" maxlength="30" name="txtUserId" value="${txtUserId}" />
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sumanta panda:
Dear Sir,
According to your advice i try it.But its not working.Could you please tell me for express language should we include any thing or set up anything in the tool.


All you need is a container that supports JSP 2.0 or higher

Why do you keep posting this?



Lastly,
"It doesn't work" really gives us nothing to work with.
How did you test the JSP I posted?
What happened?
What were you expecting to happen?
If there were errors dumped to the browser or the server logs, post them so we can see what went wrong.
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See Sumanta please try to first understand the code that is given to you by Ben and then implement it in your own way.
Ben took the effort for you to write that code so please do it.
And secondly please post the code you are trying out with and if there are any error messages then post it to along with your code.

Thirdly your post must be very clear so that the person who reads it understands it with very less effort, so use UBB code while you post. The person who can solve your problem would just skip it as
it is not so clear.
Follow what I said, the people here are very helpful and would surely help you.

I am sure this will help you.

Thank you.



[ December 10, 2008: Message edited by: Sudipto Shekhar ]
 
Srikanth Nutigattu
Ranch Hand
Posts: 114
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Sumanta,

Don't be scared on hearing EL. Its really simple scripting language. Let me tell you in detail what Ben is trying to explain.

The steps are:

1. Create a JSP (login.jsp) with the form which submits values to MyServlet.



2. Create a servlet which accepts the "form parameters" and validates them. If the id, password are positive it will forward the request to the Success.jsp else, it will set the value for the errorMessage and it will forward the request back to the initial login.jsp.

public class MyServlet extends HttpServlet{


Now, if the login and pwd fail, you are setting the request attribute "errorMessage" this time as it is not null it will be printed.

NOTE: 1. In either cases you need to forward to appropriate pages as given above.

2. No need to include any thing for EL to work because it's part of the specification.

3. The above given code is not complete.

I think now it is clear!!

Cheers


[ December 10, 2008: Message edited by: Srikanth Nutigattu ]
[ December 10, 2008: Message edited by: Srikanth Nutigattu ]
 
sumanta panda
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,
Thanks a lot for your valuable advise helped me to achieve my milestone.

I am Beginner as a web programmer.But the way you all have explained my doubt i finally done and implement in my project.

Special thanks to Ben Sir,Sudipt Sir and Srikant Sir.

Regards
Sumanta Panda
 
Srikanth Nutigattu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad you could do it happy ranching, and get hold of a good book like "Head First Servlets and JSP", You can learn a lot.

Happy coding
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are always welcome.


 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suman Bhatt,
Your post was moved to a new topic.
 
Aakash Gautam
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the answer. It helped
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic