Java getRemoseUser()使用简单的登录html表单返回null
我是网络技术的新手,我正在努力进行会话跟踪,特别是用户身份验证 我已创建此html页面:Java getRemoseUser()使用简单的登录html表单返回null,java,html,tomcat,netbeans,Java,Html,Tomcat,Netbeans,我是网络技术的新手,我正在努力进行会话跟踪,特别是用户身份验证 我已创建此html页面: <html> <head> <title>TODO supply a title</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<p>Click
<a href="login.jsp">here</a>
to authenticate.</p>
</body>
</html>
其结果通过以下视图显示:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Success Page</title>
</head>
<body>
<p>Hello</p>
<a href="LogoutServlet">logout</a>
<%= request.getRemoteUser() %> <%--RETURN NULL--%>
</body>
</html>
JSP成功页面
你好
但不幸的是,在jsp视图中,request.getRemoteUser()返回null
,我不知道为什么(我希望它显示“demo”
(登录用户的名称)
- 有人能解释一下吗
您需要将身份验证委托给tomcat,以便能够正确初始化请求,所以它是标准化的。具有您需要的所有指针-它们涉及领域的配置,并将您的实现更改为指向不同的登录URL-而不进行处理。您将不再看到用户的密码。我正要发表评论,但您的速度更快。对,他应该查看j_安全检查
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
String paramUser = request.getParameter("username");
String paramPassword = request.getParameter("password");
if(paramUser.equals("demo") && paramPassword.equals("demo")){
// Success
// Invalid session if existing
HttpSession oldSession = request.getSession(false);
if(oldSession != null)
oldSession.invalidate();
HttpSession currentSession = request.getSession(); // Creates new session
currentSession.setAttribute(u, paramUser);
currentSession.setAttribute(p, paramPassword);
response.sendRedirect("success.jsp");
}
else{
// Failed authentication
response.sendRedirect("login.jsp");
}
}
}
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Success Page</title>
</head>
<body>
<p>Hello</p>
<a href="LogoutServlet">logout</a>
<%= request.getRemoteUser() %> <%--RETURN NULL--%>
</body>
</html>