java - Displaying error message in login page after login failure - If condition is not working -
i quite new web development. have login.jsp , filter servlet . in case of unsuccessful login, message has displayed on same page.
login.jsp
<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>login page</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="css/home.css" rel="stylesheet"> </head> <body class="home"> <header class="heading"> <label class="headfont">digital market place</label> </header> <form action="./servlet1" method="post"> <div class="form-group"> <div class="login"> <label class="loginmsg">log in seller account</label> </div> <div class="input"> username: <input type="text" name="name" /><br> <br> </div> <div class="input"> password: <input type="password" name="password" /><br> <br> </div> <button type="submit" id="loginbtn">log in</button> <% out.println(request.getattribute("loginresult")); if(request.getattribute("loginresult") != null && "true".equals(request.getattribute("loginresult"))) { out.println("inide if"); %> <p style="color: red">login failed. please try again.</p> <% } %> <div class="signup"> <label class="signmsg">don't have seller account?</label> <span style="white-space:nowrap;padding-left: 20px;"><a title="register" href="registration.jsp"> sign now</a></span> </div> </div> </form> </body>
and filter servlet given below:
import java.io.ioexception; import java.io.printwriter; import java.util.properties; import javax.servlet.*; public class myfilter implements filter{ filterconfig filterconfig; public void init(filterconfig arg0) throws servletexception { setfilterconfig(arg0); } public void setfilterconfig(filterconfig config) { this.filterconfig = config; } public filterconfig getfilterconfig() { return filterconfig; } public void dofilter(servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception { printwriter out = response.getwriter(); string username = request.getparameter("name"); string password = request.getparameter("password"); properties properties = new properties(); properties.load(getfilterconfig().getservletcontext().getresourceasstream("/conf/user.conf")); if(username.equals(properties.getproperty("username")) && password.equals(properties.getproperty("password"))){ chain.dofilter(request, response);//sends request next resource } else { request.setattribute("loginresult", true); requestdispatcher requestdispatcher = request.getrequestdispatcher("/login.jsp"); requestdispatcher.forward(request, response); } } public void destroy() { }
}
the request.getattribute("loginresult") prints "true" on unsuccesful login. seems control not going inisde if condition.
can 1 please help
i think problem because request.getattribute(string arg)
return object type
value. , calling equals
method compare string
object return false
.
you need use tostring()
method change value string
change code as
if(request.getattribute("loginresult") != null && "true".equals(request.getattribute("loginresult").tostring())) { out.println("inide if");
Comments
Post a Comment