简体   繁体   English

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

[英]A forward does not change the URL in browser address bar

I am just starting with Servlets/JSP/JSTL and I have something like this: 我刚开始使用Servlets / 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>

and in my doPost method 并在我的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);
}

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 如果用户有活动会话并且已登录到系统“zalogowany”应该显示
  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. 问题是我做的事情,那些转发没有把我放到我的项目的根文件夹中的index.jsp,我仍然在我的地址栏Projekt / Hai。

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. 问题是我做的事情,那些转发没有把我放到我的项目的根文件夹中的index.jsp,我仍然在我的地址栏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. 转发基本上告诉服务器使用给定的JSP来呈现结果。 It does not tell the client to send a new HTTP request on the given JSP. 它不会告诉客户端在给定的JSP上发送新的HTTP请求。 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. 如果您希望更改客户端的地址栏,则必须告诉客户端发送新的HTTP请求。 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. 另一种方法是完全删除/index.jsp URL并始终使用/Hai URL。 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: 您可以通过将JSP隐藏在/WEB-INF文件夹中来实现这一点(以便最终用户永远不能直接打开它并强制使用servlet的URL)并实现servlet的doGet()以显示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. 这样,您只需打开http:// localhost:8080 / Project / Hai并查看JSP页面的输出,表单将只提交到相同的URL,因此浏览器地址栏中的URL基本不会更改。 I would maybe only change the /Hai to something more sensible, such as /login . 我可能只会将/Hai更改为更合理的内容,例如/login

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 与RequestDispatcher混淆,浏览器地址栏未更改 - Confusion with RequestDispatcher saying the browser address bar does not change 在网络浏览器地址栏上显示编码的URL - Show encoded url on web browser address bar 泽西岛:如何在地址栏中不更改URL的情况下重定向/转发 - Jersey: How to redirect/forward WITHOUT URL changing in address bar 如何阅读在浏览器的地址栏中输入的URL? - How can I read the URL entered in a browser's address bar? 如何获取JAVA中浏览器地址栏中显示的URL? - How to get the URL as shown in browser's address bar in JAVA? 发送重定向不会更改浏览器中的 URL - send redirect does not change URL in browser 如何在地址栏中隐藏网址? - How to hide the Url in the address Bar? 前进时如何在浏览器中显示原始URL - How to display the original URL in the browser in case of forward 有没有办法在Selenium(java)的chrome浏览器中输入非URL值(没有协议http/https的URL)到地址栏? - Is there a way to type a non-URL value (URL without protocol http/https) to address bar in chrome browser in Selenium(java)? Android浏览器:隐藏URL栏和进度栏 - Android Browser: Hiding URL bar and progress bar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM