Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes How i will display error details in the same jsp page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "How i will display error details in the same jsp page" Watch "How i will display error details in the same jsp page" New topic
Author

How i will display error details in the same jsp page

sumanta panda
Ranch Hand

Joined: Jun 23, 2008
Posts: 224
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

Joined: Apr 02, 2008
Posts: 823

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 ]

Regards, Sud.
SCJP 5 ScjpFAQ JLS
sumanta panda
Ranch Hand

Joined: Jun 23, 2008
Posts: 224
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

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

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

Joined: Jun 23, 2008
Posts: 224
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

Joined: Apr 02, 2008
Posts: 823

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

Joined: Jun 23, 2008
Posts: 224
Thanks Sudipt.I am waiting for your response.Hope you will better to guide me.

Thanks a lot in Advance.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410






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

Joined: Dec 05, 2008
Posts: 8
Same as above except you can also try

Srikanth Nutigattu
Ranch Hand

Joined: Oct 30, 2007
Posts: 114
<%=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

Joined: Apr 02, 2008
Posts: 823

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

Joined: Jun 23, 2008
Posts: 224
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Jun 23, 2008
Posts: 224
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Apr 02, 2008
Posts: 823

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

Joined: Oct 30, 2007
Posts: 114
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

Joined: Jun 23, 2008
Posts: 224
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

Joined: Oct 30, 2007
Posts: 114
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

Joined: Apr 02, 2008
Posts: 823

You are always welcome.


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61299
    
  66

Suman Bhatt,
Your post was moved to a new topic.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
GeeCON Prague 2014
 
subject: How i will display error details in the same jsp page