Redirect servlet to jsp

Mustafa Berkan Demir :

I've some issue to redirect servlets to jsp pages. My servlet codes like this

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);
}

And my html codes like this

<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">&times;</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>

The problem is here redirecting to admin.jsp page. The list is not empty but it's not redirect to admin.jsp. What's wrong here? My list results is here

Total query Rows: 1
1 - admin - abcd
0.93 sec

And my project has not web.xml

lucumt :

For your code,you have used the wrong way to compare string value,you need to use equals() instead of ==

Change

list.get(0).getFieldValue("userName")==uname 
&& list.get(0).getFieldValue("password")==pword

to

list.get(0).getFieldValue("userName").equals(uname) 
&& list.get(0).getFieldValue("password").equals(pword)

Also,in order to avoid NullPointerException,you had better to check if list is null,so change you code to below:

    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");
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=151212&siteId=1