我真的无法解决问题,但有时候,当我启动我的码头服务器时,我的登录servlet会不时获得GET请求两次。
当它发生时,登录页面也没有得到css,因为这个
“资源解释为样式表,但使用MIME类型传输 的text / html”。
我所知道的是,当我禁用我的过滤器servlet时,一切正常。
以下是我在服务器中添加内容的方法:
UserService userService = new UserService();
CharactersService charactersService = new CharactersService();
SessionsService sessionsService = new SessionsService();
Server server = new Server(8080);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.addServlet(new ServletHolder(new LoginServlet(userService, charactersService, sessionsService)), "/login");
context.addServlet(new ServletHolder(new MainMenuServlet(sessionsService)), "/main");
context.addServlet(new ServletHolder(new DuelServlet(userService)), "/duel");
context.addServlet(new ServletHolder(new FightServlet(charactersService, sessionsService, userService)),"/duel/fight");
context.setContextPath("/");
context.setBaseResource(Resource.newResource("src/main/resources"));
server.setHandler(context);
ServletHolder defaultHolder = new ServletHolder("default", new DefaultServlet());
defaultHolder.setInitParameter("dirAllowed", "true");
context.addServlet(defaultHolder, "/");
context.addFilter(new FilterHolder(new AuthenticationFilter()), "/*", EnumSet.of(DispatcherType.REQUEST));
server.start();
server.join();
如果我启动我的服务器并看到登录页面被调用两次并且没有加载css,我用addFilter()方法注释行,启动服务器,关闭它,取消注释,再次启动它并且登录页面加载就好了,这很奇怪。
如何将所有内容添加到服务器,或者甚至可能导致此类行为的内容是否有问题?
过滤servlet代码:
public class AuthenticationFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
String uri = req.getRequestURI();
HttpSession session = req.getSession(false);
if (session == null && !uri.equals("/login")) {
resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
resp.sendRedirect("/login");
} else if (session != null && uri.equals("/login")) {
resp.setStatus(HttpServletResponse.SC_OK);
resp.sendRedirect("/main");
} else {
chain.doFilter(request, response);
}
}
@Override
public void destroy() {}
}
答案 0 :(得分:0)