This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have created a login page.
Now the problem is IF the username matches the password, It should display Welcome username in another page. If it doesn't match it should print invalid password in the same form. Please help me to do this in jsp.
Bear Bibeault wrote:Using a title such as "need help" isn't going to attract the attention of those who can best help you.
So you changed it to "Jsp code doubt"? It's in the JSP forum, so one would expect it to be a question about JSP, and therefore that title is worth nothing.
Your question is about a login page, right? And not every question about JSP is going to be about login pages. So mentioning that fact in your post title might be a good idea, to tell people what your question is about.
I am in agreement with Bear, what you are doing is generally not best practice so I hope you don't plan on using something like this in a production capacity. However there is the possibility you are simply doing a learning exercise of some kind so I will do the favor of imparting some knowledge as to why you are having this problem. Eswara has provided a solution which should work but has in effect "given you a fish" without "teaching you how to fish for yourself" so to speak. In other words, I feel he hasn't explained why his solution works. It seems to me that maybe you lack the understanding of the difference between forward and redirect. The underlying mechanisms between the two are very different and this is not the first time I have seen new web developers tripped up by this.
When you send a redirect to a client browser, you are sending a response telling the client browser that the originally requested resource has moved to a new URL. Specifically there is an HTTP response status code 302 which signifies a moved resource. When a redirect is issued the server will actually issue a response to the client that originated the request. The 302 response will contain the new URL for the resource that was moved. In your case "Welcome.jsp". This causes the client browser to issue a NEW REQUEST to the new URL. The request parameters from your original 1st request are lost unless you have taken steps to preserve them and pass them along in the new request such as including them in the URL query string (or have stored them in the session or otherwise persisted them somehow). As you probably realize it must be this way because HTTP is a request/response based protocol. You get 1 response per 1 request.
Forward, on the other hand, does NOT send any response back to the client and therefore no new request is issued. Whatever incoming request parameters were there in the original request will still be available to the servlet/jsp to which you forward. Forward merely takes the original request and response objects from the original request and "forwards" them on, or delegates them if you will, to another servlet/jsp. Any posted data is preserved for the resource to which you are forwarding.
Neither redirect nor forward cause the currently executing servlet/jsp to return. Therefore, IF your jsp includes more reachable code blocks after your redirect or forward call and you don't want them executed, you should issue a return statement.
There is another key difference between a forward and a redirect as well. A forward can be issued at pretty much any time. A redirect cannot be issued once any response output to the current request has started. If you try you will see errors to the effect of "response already committed" or some such thing.