Jsp 在登录、注销和索引页面之间导航时,由于Servlet,在浏览器的搜索栏中显示错误的URL
我想学习的是通过在页面之间导航来解决URL问题 当我在登录页面中登录时(Jsp 在登录、注销和索引页面之间导航时,由于Servlet,在浏览器的搜索栏中显示错误的URL,jsp,url,servlets,Jsp,Url,Servlets,我想学习的是通过在页面之间导航来解决URL问题 当我在登录页面中登录时(http://localhost:1112/BookStoreWebsiteProject/backend/login.jsp),它打开index.jsp,但其url地址显示为http://localhost:1112/BookStoreWebsiteProject/backend/login 当我在url地址定义为http://localhost:1112/BookStoreWebsiteProject/backend/l
http://localhost:1112/BookStoreWebsiteProject/backend/login.jsp
),它打开index.jsp,但其url地址显示为http://localhost:1112/BookStoreWebsiteProject/backend/login
当我在url地址定义为http://localhost:1112/BookStoreWebsiteProject/backend/login
,打开url地址为http://localhost:1112/BookStoreWebsiteProject/backend/logout
登录Servlet
@WebServlet("/backend/login")
public class AdminLoginServlet extends HttpServlet {
...
String page = "/backend/";
RequestDispatcher requestDispatcher = request.getRequestDispatcher(page);
requestDispatcher.forward(request, response);
注销Servlet
@WebServlet("/backend/logout")
public class AdminLogoutServlet extends HttpServlet {
...
String page = "login.jsp";
RequestDispatcher requestDispatcher = request.getRequestDispatcher(page);
requestDispatcher.forward(request, response);
主Servlet
@WebServlet("/backend/")
public class AdminHomeServlet extends HttpServlet {
...
String page = "index.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(page);
dispatcher.forward(request, response);
如何修复它?来回答您关于设计url部分的问题。通常这是通过使用Java过滤器来完成的。在您的情况下,您可以使用
response.sendRedirect
:
登录Servlet
/后端/登录-->后端/index.jsp
注销Servlet
/后端/注销-->后端/注销.jsp
主Servlet
注意:您应该在web.xml中完成这一部分
index.jsp
让我知道这是否有用 来回答您关于设计url部分的问题。通常这是通过使用Java过滤器来完成的。在您的情况下,您可以使用
response.sendRedirect
:
登录Servlet
/后端/登录-->后端/index.jsp
注销Servlet
/后端/注销-->后端/注销.jsp
主Servlet
注意:您应该在web.xml中完成这一部分
index.jsp
让我知道这是否有用 我的答案
String page = request.getContextPath() + "/backend/login.jsp";
response.sendRedirect(page);
String page = request.getContextPath() + "/backend/logout.jsp";
response.sendRedirect(page);
String page = request.getContextPath() + "/backend/index.jsp";
response.sendRedirect(page);
我的回答
String page = request.getContextPath() + "/backend/login.jsp";
response.sendRedirect(page);
String page = request.getContextPath() + "/backend/logout.jsp";
response.sendRedirect(page);
String page = request.getContextPath() + "/backend/index.jsp";
response.sendRedirect(page);
您到底想做什么?当我在login.jsp之后打开index.jsp时,url显示为/backend/index.jsp。当我从索引jsp注销时,url显示为backend/login.jsp,您想让url显示注销后的内容吗?这是一针见血。如何设计url部分好的,但您需要明确在登录和注销后,您希望url显示什么,然后我将向您展示如何实现它,您希望做什么?当我在login.jsp之后打开index.jsp时,url显示为/backend/index.jsp。当我从索引jsp注销时,url显示为backend/login.jsp,您想让url显示注销后的内容吗?这是一针见血。我如何设计url部分好的,但是你需要明确你想要url在登录和注销后显示什么,然后我将向你展示如何实现它
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
String page = request.getContextPath() + "/backend/login.jsp";
response.sendRedirect(page);
String page = request.getContextPath() + "/backend/logout.jsp";
response.sendRedirect(page);
String page = request.getContextPath() + "/backend/index.jsp";
response.sendRedirect(page);