Blowman Mihawk
Blowman Mihawk

Reputation: 13

How could i change the url when forwarding to a page

I am trying to forward to a .jsp, but the url does not change. I clearly know the difference between redirect and forward, but I need to get the error information from servlet which will be displayed in jsp, for example: login.jsp

 <c:choose>
    <c:when test="${error=='username'}">
       <div class="msg error" style="display:block">invalid username</div>
    </c:when>
    <c:when test="${error=='password'}">
       <div class="msg error" style="display:block">incorrect password</div>
    </c:when>
    <c:otherwise>
       <div class="msg error" style="display:block"></div>
    </c:otherwise>
 </c:choose>
 <form id="login-form" action="LoginAction" method="post" autocomplete="off" name="loginform" onsubmit="Check()">
    <ul class="scr-log-form-list">
       <li class="scr-form-item">
           <label for="username" class="prefix">username</label> 
           <input id="username" class="ipt" name="passport" type="text" placeholder="用户名" />
       </li>
       <li class="scr-form-item"><label for="password" class="prefix">password</label>
           <input id="password" class="pwd" name="password" type="password" /> 
           <input id="remember" class="remPwd" name="remember" value="true" type="checkbox" />
           <label for="remember" class="remPwdLabel">remember me</label>
       </li>
       <li class="scr-log-item">
           <a id="login" class="log-btn" onclick="javascript:document.loginform.submit();" style="cursor: hand" href="#"></a> 
           <span class="log-ing-btn"></span>
        </li>
    </ul>
 </form> 

here is LoginServlet:

        String username = (String) request.getParameter("passport");
        String password = (String) request.getParameter("password");
        AjaxRequest ar = new AjaxRequest();
        User u = ar.getUserByName(username.trim());
        if (u == null) {
            //response.sendRedirect("index1.jsp?error=usename");
            request.setAttribute("error", "username");
            request.getRequestDispatcher("index1.jsp").forward(request,
                    response);
        } else if (!u.getPasswd().equals(password.trim())) {
//          response.sendRedirect("index1.jsp?error=password");
            request.setAttribute("error", "password");
            request.getRequestDispatcher("index1.jsp").forward(request,
                    response);
        } else {
            request.getRequestDispatcher("/WEB-INF/mapviewer.jsp").forward(
                    request, response);
        }    

I don't know how to change the url when using forward, but it is quite essential. for example if user types localhost:8080/project/LoginServlet, it will be 404 error, how could solve this problem

Upvotes: 1

Views: 1952

Answers (2)

Anurag Anand
Anurag Anand

Reputation: 498

You can use javascript for it.

<script>
 //get The current URL
 var currURL = ${requestScope['javax.servlet.forward.request_uri']}
 //Push the current URL to window history
 window.history.pushState("object or string", "mapviewer", currURL );
</script>

Put it at the top of JSP inside <body>, so that it executes first OR you can put it anywhere in the <body> .

Upvotes: 2

Sachin Patil
Sachin Patil

Reputation: 1

request.getRequestDispatcher("/WEB-INF/mapviewer.jsp").forward(

                request, response); ####

Replace "/WEB-INF/mapviewer.jsp" with "mapviewer.jsp" it will resolve your problem Thanks SP

Upvotes: 0

Related Questions