Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Login Authentication & sendRedirect

 
carina caoor
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i designed a html page with input fields for username and password, on clicking the logon button the javascript calls a servlet which checks wether the credentials provided are proper or not and then redirect the page to another jsp, but i am facing a problem in this redirect.


[code]
if(password.equals(pword))
{
request.getSession().setAttribute("UserName",username);
response.sendRedirect("src/jsp/second.jsp");


}

else
{
System.out.println("inside else");

out.println("<html><script langauge=\"javascript\">function raiseexception(){alert(\"The Username and the Password did not match please ensure that you enter the correct username and password \");}</script> <body onloud=\"raiseexception();\"></body></html>");

response.sendRedirect("src/jsp/home.html");


}

The problem is when username and password are matched it redirects to the second.jsp page but when the username and password do not match it does not display the alert saying the username and password did not match it just redirects to the same page home.html, I want the alert message along with the redirect can anybody help me in this.

Also when user log's in the session is to be maintained through out the application, can anybody suggest me how to do that..
[ September 23, 2008: Message edited by: ruquia tabassum ]
 
Anadi Misra
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Check this part



this has to be onload. Regarding session management there are two common ways

1) Write a cookie with a unique ID and check it in later requests
2) Store user specific data in the HttpSession Object since it is shared.

there is a good article that can help you out

http://www.developer.com/java/other/article.php/616831
[ September 23, 2008: Message edited by: Anadi Misra ]
 
carina caoor
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its onload in my code but here i modified it because the post was not getting posted it was asking me to modify the html tags with lt; and remove the function "onload".
 
Ola Daniel
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do a search on Form Based Authentication. Trust me, that would make your life easier.
 
Ola Daniel
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if you insist on doing it this way:


response.sendRedirect("src/jsp/second.jsp");


That line looks wrong, but I can't know for sure because I don't know what your web archive directory structure looks like.
 
carina caoor
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my web directory sturcture is

webapps----MyPlots----src---jsp---second.jsp (all jsp here home.jsp etc.) also

webapps----MyPlots----src---PlotManagement---UserValidation.java

The path i specified is correct its directing to second.jsp but on error i want the alert to be displayed and then direct back to home.html, which is not happening with my code it is just directing back to home page with out the alert being displayed.
[ September 23, 2008: Message edited by: ruquia tabassum ]
 
Ola Daniel
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Nothing wrong with your redirect line. A servlet cannot output to two pages at the same time, the out.println(...) is going to be ignored and the page will only get redirected to home.html which is what you say is happening.

There are many ways of doing this, One way is to comment out the response.sendRedirect line and send your output to the browser and include a javascript (window.location.href='src/jsp/home.html') to redirect to the home page after the alert shows.

Personal note: I'm not fond of constructing HTML in servlets (its a nightmare).
 
carina caoor
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help daniel , but as you said its a nightmare constructing html inside servlet, can you suggest me how would i alert user of the error that is raised in my stored procedure which i am calling from servlet
my code says
[code]
try
{

CallableStatement cstmt = d.connection.prepareCall("{call dbo.sp_rs_AreaCodeMasterRemovePlots(?,?,?,?,?,?)}");
cstmt.setString(1,apid.toString());
cstmt.setString(2, acode);
cstmt.setString(3,mareacode);
cstmt.setString(4,username);
cstmt.setInt(5,Integer.parseInt(comment));
cstmt.setString(6,userdefcomment);


cstmt.execute();

d.connection.close();
}

catch(Exception e)
{

out.println("<html><script langauge=\"javascript\">function raiseexception(){var error=\"" +e+"\";alert(error);}</script>" +
"<body onloud=\"raiseexception();\">" +
"</body></html>");
e.printStackTrace();
}


the exception that is raised in my stored procedure i want to display that in the alert that's the reason i am constructing html in servlet can you please suggest me any other optimum solution for this.
 
Ola Daniel
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks to me like you need to learn how to use the MVC Model. I could give you a quick fix and tell you exactly what to do but I don't think that is what Java Ranch is all about. Do some search on these patterns: FrontControllers Pattern, The Model-View-Controller Pattern. While your at it pay attention to the RequestDispatcher
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic