[英]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: 但有些东西不起作用,因为我想要这样的东西:
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
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.