null-6ren"> null-我创建了一个登录 servlet: @WebServlet( name = "SignInServlet", description = "check email & pass", -6ren">
gpt4 book ai didi

java - getSession().getAttribute ("....") -> null

转载 作者:太空宇宙 更新时间:2023-11-04 11:13:53 24 4
gpt4 key购买 nike

我创建了一个登录 servlet:

@WebServlet(
name = "SignInServlet",
description = "check email & pass",
urlPatterns = {"/authorization_signin"}
)
public class SignInServlet extends javax.servlet.http.HttpServlet {

public SignInServlet(){
}

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws javax.servlet.ServletException, IOException {
request.setCharacterEncoding("UTF-8");
UserDataSet user = new UserDataSet();
SignInModel modelSignIn = new SignInModel();


user.setEmail(request.getParameter("email"));
user.setPassword(request.getParameter("password"));

user = modelSignIn.doSignIn(user);

if (request.getSession().getAttribute("loggedUser") == null) {
if (user != null) {
request.getSession().setAttribute("loggedUser", user); request.getRequestDispatcher("authorization.jsp").forward(request, response);
response.setStatus(HttpServletResponse.SC_OK);
} else {
request.setAttribute("errorMessage", "Email or password is incorrect");
request.getRequestDispatcher("index.jsp").forward(request, response);
response.setStatus(HttpServletResponse.SC_OK);
}
}

}

@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException{
response.setContentType("text/html; charset=utf-8");
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
}

当用户登录时,servlet 将其重定向到“authorization.jsp”

<body>
<%

if (request.getSession().getAttribute("loggedUser") != null){
UserDataSet user = (UserDataSet) request.getSession().getAttribute("loggedUser");

System.out.println("In author :" + request.getSession().getAttribute("loggedUser"));
%>
<h1> Hello <%= user.getFirstName() %> <%= user.getLastName() %>!</h1>
<h2>AUTORIZED!</h2>
<a href="/authorization_logout">Log Out</a>
<%
}
else {
%>
<h1>GO HOME</h1>
<%
}
%>
</body>

然后浏览器显示此页面和登录用户的数据

如果写入 URL“localhost:8080”并转到“index.jsp”,则 再次讨论“authorization.jsp”

过滤器检查 session :

@WebFilter(filterName = "LoginFilter")
public class LoginFilter implements Filter {
public void destroy() {
}



public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
HttpServletRequest httpRequest = (HttpServletRequest) req;
HttpServletResponse httpResponse = (HttpServletResponse) resp;

System.out.println("Enter filter");

System.out.println("Filter session: " + httpRequest.getSession(false).getAttribute("loggedUser"));



UserDataSet user = (UserDataSet) httpRequest.getSession(false).getAttribute("loggedUser");

if (user != null) {
System.out.println("CHAIN");
chain.doFilter(req, resp);
} else {
httpResponse.sendRedirect("/");
System.out.println("Not signin");
}
}

public void init(FilterConfig config) throws ServletException {

}

}

并且 getSession().getAttribute("loggedUser") 返回 null为什么?

下一段文字:如果我登录并 servlet 打开“authorization.jsp”,则尝试在任何 *.jsp 上抛出 URL,其中将检查 session ,结果将为 null

出了什么问题?

最佳答案

我的 friend 帮我在 stackoverflow 上的一些答案中找到了这个问题的答案

If you put something in the session with request.getSession().setAttribute you have to read it from the session, not the request. Try <%= session.getAttribute("test") %>

Link to answer

关于java - getSession().getAttribute ("....") -> null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45696968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com