自学内容网 自学内容网

11.20[JAVAEXP3]重定向细究【DEBUG】

设置了根域名访问为testServlet,让他重定向到首页为test.jsp,事实上也都触发了,但是最后显示的为什么不是test.jsp生成页面,依然还是index.jsp生成的页面??

重定向是通过Dispatcher进行的,而不是sendRedirect,会有影响吗?

        req.getRequestDispatcher("test.jsp").forward(req, resp);
[2024-11-20 02:08:24,706] 工件 mavenFinal: 工件已成功部署
[2024-11-20 02:08:24,706] 工件 mavenFinal: 部署已花费 974 毫秒
Session created: 68E11A7B6708B9A40D7618F6FACFE666
Active sessions: 1
index.jsp had been touch!!!
Session created: 4779DFD02DD214D2FD8D8DB472BDC34A
Active sessions: 2
index.jsp had been touch!!!
Session created: 3E9C28768C9EBC36543B7BFCB44C9B4D
Active sessions: 3
index.jsp had been touch!!!
the test servlet had been touch
test.jsp has been touch!!!

服务器运行时,初始的这三个session连接是怎么回事?

index.jsp had been touch!!!
the test servlet had been touch
test.jsp has been touch!!!

再次访问域名时,访问信息依次如上所述,test.jsp既然是最后访问到的,为什么不会覆盖Indwx.jsp?再或者,为什么会访问index.jsp(此时welcome并没有配置)?

如果没有配置 web.xml 的url的映射顺序,而是分散在各个servelt当中,以@WebServlet("/")的形式定义映射方式,那么url的处理顺序是怎样的?

并不是url的处理顺序问题导致的

能否禁用web的默认welcome-list?使其访问域名时默认不渲染任何页面?而是经由servlet处理后才行?

package filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter("/*")
public class LoginFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // 初始化过滤器
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        
        // 获取会话
        HttpSession session = httpRequest.getSession(false);
        
        // 检查会话是否存在并且用户已登录
        if (session != null && session.getAttribute("user") != null) {
            // 用户已登录,放行请求
            chain.doFilter(request, response);
        } else {
            // 用户未登录,重定向到登录页面
            httpResponse.sendRedirect(httpRequest.getContextPath() + "/login.jsp");
        }
    }

    @Override
    public void destroy() {
        // 销毁过滤器
    }
}

不应该是从客户端发来的cookie当中找到session的id,服务端再依据sessionId去查找对应的session吗?为什么会直接从request当中获取session?


原文地址:https://blog.csdn.net/m0_73553411/article/details/143899328

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!