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 Jsp code doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Jsp code doubt" Watch "Jsp code doubt" New topic
Author

Jsp code doubt

sahana mithra
Ranch Hand

Joined: Oct 26, 2010
Posts: 72
I have created a login page.
<html>
<body>
<%
String name=request.getParameter("name");
String pwd=request.getParameter("password");
if(name.equals(pwd))
{
response.sendRedirect("Welcome.jsp");
}
else
{
out.println("Invalid Username/password");
}
%>
</body>
</html>



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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61769
    
  67

sahana mithra wrote:Please help me to do this in jsp.

No. A JSP is the worst possible place to try and do this. Not only should your JSP contain no Java code, you'd have to put this code into each and every JSP in your application.

A servlet filter is a much better place to check authentication.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18992
    
    8

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.
Eswar Nec
Ranch Hand

Joined: May 02, 2010
Posts: 105
Hi sahana,
Try this,
login.jsp


Then ,
Welcome.jsp



After try, what you will see.

Wake up! Don't let your smile be snatched away by anybody!
Regards, Eswar
Alex Hurtt
Ranch Hand

Joined: Oct 26, 2010
Posts: 98
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.
Gaurav Sagar
Ranch Hand

Joined: Sep 08, 2010
Posts: 97

If you want to display the Welcome.jsp in a new browser window/tab, then this small tweak into the HTML code would work. Its just the user has to click onto the link generated.



The Welcome.jsp is :-


As a matter of advise, its kinda lame to use scriplets in a JSP. Tags are most commonly used in a JSP nowadays.

SCJP 1.6(91%), SCWCD 1.5(100%), SCBCD in progress
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Jsp code doubt