转发不会更改浏览器地址栏中的 URL
我刚刚开始使用 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);
}
但有些东西不起作用,因为我想要这样的东西:
- 如果用户有活动会话并登录到系统“zalogowany”应该显示
- 否则,无论我做什么,记录
问题都是问题,这些转发不会将我带到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:
- if user had active session and was logged to system "zalogowany" should show
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果这确实是您唯一的问题
然后我必须令您失望的是:这完全符合规范。转发基本上告诉服务器使用给定的 JSP 来呈现结果。它不会告诉客户端在给定的 JSP 上发送新的 HTTP 请求。如果您期望客户端的地址栏发生变化,那么您必须告诉客户端发送新的 HTTP 请求。您可以通过发送重定向而不是转发来做到这一点。
因此,除了
do
之外,另一种方法是完全删除
/index.jsp
URL 并始终使用/Hai
URL。您可以通过将 JSP 隐藏在/WEB-INF
文件夹中(这样最终用户永远无法直接打开它并被迫为此使用 servlet 的 URL)并实现doGet 来实现此目的()
的 servlet 以及显示 JSP:这样,您只需打开 http://localhost:8080/Project/Hai 并查看 JSP 页面的输出,表单将提交到完全相同的 URL,因此浏览器地址栏中的 URL 基本上将不改变。我可能只会将
/Hai
更改为更合理的内容,例如/login
。另请参阅:
If that is really your only problem
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
do
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 thedoGet()
of the servlet as well to display the JSP: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: