Redirect to new page

1.8k views Asked by At

I I have a form in my "login.html" and after I click the "login" button, this form will be submitted to my java back-end code. However, I don't know how to redirect my login page to the new page.

In the following code:

<script>
        $(function() {
           $('#ff').form({
               url: "LoginServlet",
               success:function(data){
                   $.messager.alert(data);
               }
           });
        });
    </script>

In the success part, my redirected page will show in this message window. But I want it jump to the page returned by the servlet.

enter image description here

This is my login.html code:

<!DOCTYPE html>
<html lang="en">
<head>
    <script>
        $(function() {
           $('#ff').form({
               url: "LoginServlet",
               success:function(data){
                   $.messager.alert(data);
               }
           });
        });
    </script>
</head>
<body>
    <form id="ff" role="form" method="post">
        <div>
            <h1>User Login</h1>
        </div>
        <div>
            <input id="username" name="username" class="easyui-textbox" data-options="iconCls:'icon-man',iconWidth:30,iconAlign:'left',prompt:'Username'" style="width:100%;height:35px;" />
        </div>
        <div>
            <input id="password" name="password" class="easyui-passwordbox" data-options="iconWidth:30,iconAlign:'left',prompt:'Password'" style="width:100%;height:35px;" />
        </div>
        <div>
            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="$('#ff').submit()" style="width:100%;height:35px;">Submit</a>
        </div>
        <div>
            <div style="display:inline;">
                <a href="javascript:void(0)">Register</a>
            </div>
        </div>
    </form>
</body>
</html>

This is my java servlet code:

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        IUserService userService = new UserServiceImpl();
        User user = userService.login(username, password).get(0);

        if (user != null) {
            response.sendRedirect(request.getContextPath() + "/index.jsp");
        } else {
        }
    }
}
1

There are 1 answers

2
Panagiotis Bougioukos On

You could try removing the Javascript function and calling directly the jsp using the form action.

So I would remove the following part

<script>
        $(function() {
           $('#ff').form({
               url: "LoginServlet",
               success:function(data){
                   $.messager.alert(data);
               }
           });
        });
    </script>

This part here needs to be removed because of the structure that you have. Most projects with JSP have this structure that you have.

You want in first step to take all the form parameters and to visit the url .../LoginServlet.

Then as you have structured your servlet it makes the authentication and if successful it sends a message to your browser and says: Please browser keep these headers in the http message but please visit another URL (index.jsp) to see what you wait for.

As you can see it's up to the client (aka browser) to move between different URL in order to be served.

That is why that simple Javascript function does not work as expected.

So a simple solution would be to make the call directly in the form action

<body>
    <form id="ff" role="form" method="post" action="/LoginServlet">
    ....

If the authentication was not successful then I would serve an error page from my servlet.

if (user != null) {
            response.sendRedirect(request.getContextPath() + "/index.jsp");
        } else {
            response.sendRedirect(request.getContextPath() + "/error.html");
}