我在将servlet重定向到jsp页面时遇到一些问题。我的servlet代码如下
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String uname=request.getParameter("userName");
String pword=request.getParameter("password");
SolrJava SJ = new SolrJava();
SolrDocumentList list;
try {
list = SJ.getSolrList(uname);
if(list.get(0).getFieldValue("userName")==uname && list.get(0).getFieldValue("password")==pword)
{
HttpSession session=request.getSession(true);
session.setAttribute("userName", uname);
session.setAttribute("password", pword);
response.getWriter().append("Login SucessFully");
response.sendRedirect("admin.jsp");
}
else
{
response.sendRedirect("index.jsp");
}
} catch (SolrServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//doGet(request, response);
}
我的html代码是这样的
<input type="button" value="ADMIN LOGIN" class="btn btn-sm" onclick="document.getElementById('id01').style.display='block'"></p></div>
<div id="id01" class="modal">
<span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">×</span>
<form class="modal-content animate" action="LoginServlet" method="POST">
<div class="container" class="text-center" align="center">
<label for="uName"><b>User Name</b></label>
<input id="userName" type="text" placeHolder="Enter Username" name="userName" required>
<br>
<label for="psw"><b>Password</b></label>
<input id="password" type="password" placeHolder="Enter Password" name="password" required>
<br>
<button type="submit" class="btn btn-sm" onclick="login()">Login</button>
<button type="button" onclick="document.getElementById('id01').style.display='none'" class="btn btn-sm">Cancel</button>
<br>
</div>
</form>
</div>
问题在这里重定向到 admin.jsp 页面。该列表不为空,但不会重定向到 admin.jsp 。怎么了我的列表结果在这里
Total query Rows: 1
1 - admin - abcd
0.93 sec
我的项目没有web.xml
答案 0 :(得分:2)
对于您的代码,您使用了错误的方法来比较字符串值,需要使用equals()
而不是==
更改
list.get(0).getFieldValue("userName")==uname
&& list.get(0).getFieldValue("password")==pword
到
list.get(0).getFieldValue("userName").equals(uname)
&& list.get(0).getFieldValue("password").equals(pword)
此外,为了避免 NullPointerException ,您最好检查list
是否为空,因此将代码更改为以下内容:
if(list!=null &&
list.get(0).getFieldValue("userName").equals(uname)
&& list.get(0).getFieldValue("password").equals(pword)){
HttpSession session=request.getSession(true);
session.setAttribute("userName", uname);
session.setAttribute("password", pword);
response.getWriter().append("Login SucessFully");
response.sendRedirect("admin.jsp");
} else {
response.sendRedirect("index.jsp");
}