2

I am trying to make a simple login page to deal with database using JSP, servlet with MVC concept and Data Access Object(DAO) and I am new in this. My problem now that I need to alert a box message in servlet if the user enters invalid name and password and sendRedirect to login.jsp page again. I set flag to be 1 if the user valid then do this if-check

if(validUserFlage == 1)
    response.sendRedirect("User_Manipulation_Interface.jsp");
else {   
    //Here i want to alert message cause user invalid ??
    response.sendRedirect("Admin_And_User_Login_Form.jsp");
}

searching for this i find this answer but i can`t understand how can i do it the answer i found: (( With send redirect you cant display the message where you want in the code. So as per me there might be two approaches. Display a message here and use include of requestdispatcher instead of send redirect or else pass some message to admin.jsp and display the message there. ))

home
  • 12,468
  • 5
  • 46
  • 54
Eman Hassan
  • 23
  • 1
  • 1
  • 4
  • In our servlets wiki page you can find a hello world example with validation: http://stackoverflow.com/tags/servlets/info – BalusC Feb 24 '12 at 13:55

3 Answers3

3

Set a flag parameter like this,

 response.sendRedirect("Admin_And_User_Login_Form.jsp?invalid=true");

on jsp

<c:if test=${invalid eq 'true'}">invalid credentials</c:if>
jmj
  • 237,923
  • 42
  • 401
  • 438
  • ok, but in jsp page i trying to get the value send in URL invalid = request.getParameter("invalid"); for now i want to check if the invalid is true or false and if it false then alert message with javascript for example to tell the user that his input invalid Really i am new in jsp and i can not understand the last line from your answer Could you help me in more simple way ! – Eman Hassan Feb 24 '12 at 08:12
  • set that flag to some hidden field on your jsp from java script after load check if that field contains true then alert – jmj Feb 24 '12 at 08:29
2

You can set the Parameters like errormsg in the servlet page and add it to the ri-direct object. Then you can check that variable errormsg and if it is null then the username is correct else the username is incorrect..

In Servlet Code:
if(username.equal(databaseusername))

{
RequestDispatcher rd = request.getRequestDispatcher("NextPage.jsp");

req.setAttribute("errormsg", "");

rd.forward(request, response);  

}

else

{

RequestDispatcher rd = request.getRequestDispatcher("login.jsp");

req.setAttribute("errormsg", "Wrong Username or Password");

rd.forward(request, response);  

}

In JSP code:

<%

String msg=req.getAttribute("errormsg").toString();

if(!msg.equals(""))

{

// Print here the value of Msg.

}


%>
Haresh Chaudhary
  • 4,390
  • 1
  • 34
  • 57
0
In servlet
    String strExpired = (String) session.getAttribute("Login_Expired");
response.sendRedirect("jsp/page.jsp");

In jsp 
<%
String strExpired = (String) session.getAttribute("Login_Expired");
out.print("alert('Password expired, please update your password..');");

%>