• 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

Jsp code doubt

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
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

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.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sahana,
Try this,
login.jsp


Then ,
Welcome.jsp



After try, what you will see.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 97
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic