转发不会更改浏览器地址栏中的 URL

发布于 2025-01-04 09:08:24 字数 1543 浏览 0 评论 0原文

我刚刚开始使用 Servlet/JSP/JSTL,我有这样的东西:

<html>
<body>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<jsp:directive.page contentType="text/html; charset=UTF-8" />

<c:choose>
  <c:when test='${!empty login}'>
    zalogowany
  </c:when>
<c:otherwise>
   <c:if test='${showWarning == "yes"}'>
        <b>Wrong user/password</b>
    </c:if>
    <form action="Hai" method="post">
    login<br/>
     <input type="text" name="login"/><br/>
     password<br/>
     <input type="password" name="password"/>
     <input type="submit"/>
     </form>
  </c:otherwise>
</c:choose>
</body>
</html>

在我的 doPost 方法中,

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException 
{
    HttpSession session=request.getSession();
    try
    {
        logUser(request);
    }
    catch(EmptyFieldException e)
    {
        session.setAttribute("showWarning", "yes");
    } catch (WrongUserException e) 
    {
        session.setAttribute("showWarning", "yes");
    }
    RequestDispatcher d=request.getRequestDispatcher("/index.jsp");
    System.out.println("z");
    d.forward(request, response);
}

但有些东西不起作用,因为我想要这样的东西:

  1. 如果用户有活动会话并登录到系统“zalogowany”应该显示
  2. 否则,无论我做什么,记录

问题都是问题,这些转发不会将我带到index.jsp,它位于我的项目的根文件夹中,我的地址栏中仍然有Projekt/Hai。

I am just starting with Servlets/JSP/JSTL and I have something like this:

<html>
<body>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<jsp:directive.page contentType="text/html; charset=UTF-8" />

<c:choose>
  <c:when test='${!empty login}'>
    zalogowany
  </c:when>
<c:otherwise>
   <c:if test='${showWarning == "yes"}'>
        <b>Wrong user/password</b>
    </c:if>
    <form action="Hai" method="post">
    login<br/>
     <input type="text" name="login"/><br/>
     password<br/>
     <input type="password" name="password"/>
     <input type="submit"/>
     </form>
  </c:otherwise>
</c:choose>
</body>
</html>

and in my doPost method

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException 
{
    HttpSession session=request.getSession();
    try
    {
        logUser(request);
    }
    catch(EmptyFieldException e)
    {
        session.setAttribute("showWarning", "yes");
    } catch (WrongUserException e) 
    {
        session.setAttribute("showWarning", "yes");
    }
    RequestDispatcher d=request.getRequestDispatcher("/index.jsp");
    System.out.println("z");
    d.forward(request, response);
}

but something is not working, because I wanted something like this:

  1. if user had active session and was logged to system "zalogowany" should show
  2. otherwise logging form

the problem is whatever I do, those forwards don't put me to index.jsp, which is in root folder of my Project, I still have in my address bar Projekt/Hai.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只想待在家 2025-01-11 09:08:24

如果这确实是您唯一的问题

问题是无论我做什么,这些转发都不会将我转到位于我的项目根文件夹中的index.jsp,但我的地址栏中仍然有 Projekt/Hai。< /p>

然后我必须令您失望的是:这完全符合规范。转发基本上告诉服务器使用给定的 JSP 来呈现结果。它不会告诉客户端在给定的 JSP 上发送新的 HTTP 请求。如果您期望客户端的地址栏发生变化,那么您必须告诉客户端发送新的 HTTP 请求。您可以通过发送重定向而不是转发来做到这一点。

因此,除了

RequestDispatcher d=request.getRequestDispatcher("/index.jsp");
System.out.println("z");
d.forward(request, response);

do

response.sendRedirect(request.getContextPath() + "/index.jsp");

之外,另一种方法是完全删除 /index.jsp URL 并始终使用 /Hai URL。您可以通过将 JSP 隐藏在 /WEB-INF 文件夹中(这样最终用户永远无法直接打开它并被迫为此使用 servlet 的 URL)并实现 doGet 来实现此目的() 的 servlet 以及显示 JSP:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.getRequestDispatcher("/WEB-INF/index.jsp").forward(request, response);
}

这样,您只需打开 http://localhost:8080/Project/Hai 并查看 JSP 页面的输出,表单将提交到完全相同的 URL,因此浏览器地址栏中的 URL 基本上将不改变。我可能只会将 /Hai 更改为更合理的内容,例如 /login

另请参阅:

If that is really your only problem

the problem is whatever I do, those forwards don't put me to index.jsp, which is in root folder of my Project, I still have in my address bar Projekt/Hai.

then I have to disappoint you: that's fully by specification. A forward basically tells the server to use the given JSP to present the results. It does not tell the client to send a new HTTP request on the given JSP. If you expect a change in the address bar of the client, then you have to tell the client to send a new HTTP request. You can do that by sending a redirect instead of a forward.

So, instead of

RequestDispatcher d=request.getRequestDispatcher("/index.jsp");
System.out.println("z");
d.forward(request, response);

do

response.sendRedirect(request.getContextPath() + "/index.jsp");

An alternative is to get rid of the /index.jsp URL altogether and use /Hai URL all the time. You can achieve this by hiding the JSP away in /WEB-INF folder (so that the enduser can never open it directly and is forced to use the servlet's URL for this) and implement the doGet() of the servlet as well to display the JSP:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.getRequestDispatcher("/WEB-INF/index.jsp").forward(request, response);
}

This way, you can just open http://localhost:8080/Project/Hai and see the output of the JSP page and the form will just submit to the very same URL, so the URL in browser address bar will basically not change. I would maybe only change the /Hai to something more sensible, such as /login.

See also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文