我有一个登录jsp页面。我正在使用java servlet对该登录页面进行服务器端验证。当我点击登录按钮而不输入值时,它显示错误消息。之后,即使仍然显示错误消息,我只是刷新登录页面。
我的要求是当我刷新时,当我从转发的页面返回时,我的登录页面不应显示包含错误消息。
这是我的代码
if(username==null || username.isEmpty()){
errors.put("username", "Please Enter User Name");
}
if(password==null || password.isEmpty()){
errors.put("password", "Please Enter Password");
}
if(type==null || type.isEmpty()){
errors.put("type", "Please Select Type");
}
if(errors.size()>0)
{
request.setAttribute("map",errors);
RequestDispatcher rd=request.getRequestDispatcher("login.jsp");
rd.forward(request, response);
}
JSP
<%!
Map<String,String> errors=null;
%>
<%
if(request.getAttribute("map")!=null){
errors=(Map<String,String>)request.getAttribute("map");
request.removeAttribute("map");
}
%>
<div class="textbox">
<%if(request.getParameter("username")== null || "".equals(request.getParameter("username"))){ %>
<input name="username" type="text"/>
<span style="color:red"><%=(errors!=null?(errors.get("username")!=null?errors.get("username"):""):"")%></span>
<%} else{ %>
<input name="username" type="text" value="<%=request.getParameter("username")%>"/>
<%} %>
</div>
<div class="Password">Password</div>
<div class="textbox">
<%if(request.getParameter("password")== null || "".equals(request.getParameter("password"))){ %>
<input name="password" type="password" />
<span style="color:red"><%=(errors!=null?(errors.get("password")!=null?errors.get("password"):""):"")%></span>
<%} else{ %>
<input name="password" type="password" value="<%=request.getParameter("password")%>"/>
<%} %>
</div>
<div class="Password">Type</div>
<div class="type">
<%if(request.getParameter("type")== null || "".equals(request.getParameter("type"))){ %>
<select name="type" id="type">
<option value="">---Select---</option>
<option value="Administrator">Administrator</option>
<option value="User">User</option>
<!-- <option></option>
<option></option> -->
</select>
<span style="color:red"><%=(errors!=null?(errors.get("type")!=null?errors.get("type"):""):"")%></span>
<%} else{ %>
<select name="type" id="type">
<option value="<%=request.getParameter("type")%>"><%=request.getParameter("type")%></option>
<option value="Administrator">Administrator</option>
<option value="User">User</option>
<!-- <option></option>
<option></option> -->
</select>
<%} %>
</div>