我想验证有关用户的信息。如果已输入用户并尝试进入/login
页面,请转到用户页面。
到目前为止,我有这个:
StartServlet
public class StartServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
UserDTO userDTO = (UserDTO) req.getSession().getAttribute("userDTO");
if (userDTO == null) {
req.getRequestDispatcher(req.getContextPath() + "/WEB-INF/pages/login.jspx").forward(req, resp);
} else {
req.getRequestDispatcher(req.getContextPath() + "/checklogin").forward(req, resp);
}
}
}
ValidateServlet
public class ValidateServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String email = req.getParameter("email");
String password = req.getParameter("password");
UserDTO userDTO = (UserDTO) req.getSession().getAttribute("userDTO");
if (userDTO == null) {
userDTO = UserService.getInstance().validateUserDTO(email, password);
}
if (userDTO != null) {
req.getSession().setAttribute("userDTO", userDTO);
if (userDTO.getRole() == Role.ADMINISTRATOR || userDTO.getRole() == Role.SUPERADMINISTRATOR) {
req.getRequestDispatcher(req.getContextPath() + "/admin/catalog").forward(req, resp);
} else {
req.getRequestDispatcher(req.getContextPath() + "/WEB-INF/pages/user/catalog/catalog.jspx").forward(req, resp);
}
} else {
String errorMsg = "Email or password is incorrect";
req.setAttribute("error", errorMsg);
req.getRequestDispatcher(req.getContextPath() + "/WEB-INF/pages/login.jspx").forward(req, resp);
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
AdminCatalogServlet
public class AdminCatalogServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<ItemDTO> itemsDTO = ItemService.getInstance().getAllItemsDTO();
req.setAttribute("itemsDTO", itemsDTO);
req.getRequestDispatcher(req.getContextPath() + "/WEB-INF/pages/admin/catalog/catalog.jspx").forward(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
的web.xml
<servlet>
<servlet-name>StartServlet</servlet-name>
<servlet-class>...StartServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StartServlet</servlet-name>
<url-pattern>/</url-pattern>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ValidateServlet</servlet-name>
<servlet-class>...ValidateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ValidateServlet</servlet-name>
<url-pattern>/checklogin</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>AdminCatalogServlet</servlet-name>
<servlet-class>...AdminCatalogServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AdminCatalogServlet</servlet-name>
<url-pattern>/admin/catalog</url-pattern>
</servlet-mapping>
问题:我们从StartServlet
开始,在致电/
或/login
时。然后我们输入login,password to form(method = POST)并提交给ValidateServlet
。
之后我们转发到AdminCatalogServlet
(来自Admin的前瞻性)。然后我们转发到jsp
,我们的浏览器显示给我们页面。
(这是对数据库的2个请求)用户和项目。我想,就是这样,但是......然后tomcat再次在doGet()
中运行方法StartServlet
,将用户从会话中转移到ValidateServlet
转到方法Get。并反复向数据库请求项目。
为什么会这样?感谢。
更新:如果从ValidateServlet
或AdminCatalogServlet
删除了doGet()方法,则问题就会消失,但在尝试输入{时却无法工作登录后{1}}。
UPDATE2:我已按照评论中的建议进行追踪(可能不对)。首先是第一个请求(/login
),然后是引导程序的附加数据集。最后,使用POST
方法在/validate
上发出第二个请求。
UPDATE2:要解决此问题,我已将GET
映射从StartServlet
更改为/
。这意味着对""
的任何请求都将被重定向到此servlet。
问题是由于自从Bootstrap连接后,浏览器还向host:port/
发送了对favicon,css等的请求。由于host:port/favicon
已映射到StartServlet
,因此它已收到此请求,方法/
已启动。
有关详细信息,请参阅Tomcat中的GET