如何在转发到页面时更改URL

时间:2015-01-05 01:19:04

标签: java jsp url servlets forward

我正在尝试转发.jsp,但网址不会改变。我清楚地知道重定向和转发之间的区别,但我需要从servlet获取将在jsp中显示的错误信息,例如: 的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> 

这里是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);
        }    

我不知道在使用前锋时如何更改网址,但这非常重要。例如,如果用户键入localhost:8080 / project / LoginServlet,那将是404错误,怎么能解决这个问题

2 个答案:

答案 0 :(得分:2)

您可以使用javascript。

<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>

将它放在<body>内的JSP顶部,以便它首先执行,或者将它放在<body>中的任何位置。

答案 1 :(得分:0)

的request.getRequestDispatcher(&#34; /WEB-INF/mapviewer.jsp")。正向(

                request, response); ####

替换&#34; /WEB-INF/mapviewer.jsp"用&#34; mapviewer.jsp&#34; 它会解决你的问题 谢谢 SP