gpt4 book ai didi

java - 如何使在一个 Servlet 中初始化的登录 session 在另一个 Servlet 中初始化且中间有 href 的登录 session 无效?

转载 作者:行者123 更新时间:2023-12-01 12:47:40 24 4
gpt4 key购买 nike

我正在开发一个网络应用程序,我必须实现一个登录/注销系统。我已经在 Controller servlet 中实现了登录系统(通过数据库验证进行验证)。基本上,我的项目最重要的方面是保持 MVC 方法
所以这是我实现的 Controller 登录 servlet,

package com.cid_org.controller;
import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.cid_org.model.*;

import java.sql.*;

/**
* Servlet implementation class LoginControllerServlet
*/

public class LoginControllerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public LoginControllerServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

/*Take the data submitted by the user though the login
* form(Remember the HTTP Post request ->HttpServletRequest request object*/
String username = request.getParameter("username");
String pwd = request.getParameter("pass");
System.out.println(username + "aaa");
System.out.println(pwd);
Connection connection = (Connection)getServletContext().getAttribute("connection_context_param");

LoginModelPOJO lmpojo = new LoginModelPOJO(username, pwd, connection);
boolean isUserValidFlag = lmpojo.isValid();

if(isUserValidFlag){
/*Entering this block indicates the user has been validated and if the
* user has been validated, we should start a session. But here is a
* question, where exactly(at which point) should we say that user has
* logged in? -I guess when the user sends his/her our login info.
* for validation and right at the moment the info. gets validated,
* we can say at this particular point in program he is IN. And this
* is the precise point for a Login session to start. So remember
* at this point we are logged in*/

/*Getting the session*/
HttpSession session = request.getSession();

RequestDispatcher view =request.getRequestDispatcher("/view/view_profile.jsp");
view.forward(request, response);
}
else{
/*And obviously the unauthorized user will be redirected to the login page.*/
response.sendRedirect("login.html");
}

}

}

这是 view_profile.jsp,我将请求分派(dispatch)到,

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Crime Investigation Department-User" content="text/html; charset=ISO-8859-1">
<title>Criminal Investigation Department</title>
<link rel="stylesheet" href="/CrimeReportingSystem5/static/css/view_profile_page.css">
</head>

<body>

<img src="css/images/logo/CID_Logo_1.png" alt="CID Logo">

<nav id="navigation">
<a id="link1" class="header_links" href="most_wanted.html">Most Wanted</a>
<a id="link2" class="header_links" href="hotnews.html">Hot News</a>
<a id="link3" class="header_links" href="report_crime.html">Report Crime</a>
<a id="link4" class="header_links" href="login.html">Login</a>
<a id="link5" class="header_links" href="about.html">About Us</a>
<a id="link6" class="header_links" href="contact.html">Contact Us</a>
<a id="link7" class="header_links" href="safety_measures.html">Safety Measures</a>
</nav>

<%
/*Remember we got to this page only after validation of the username
*from the servlet ,point being the username has already been validated
*so all we got to do here is retrieve the username form the request
*object and it.*/
String username = request.getParameter("username");

if(username == null){
response.sendRedirect("/CrimeReportingSystem5/static/login.html");
}
%>
<div id="login_page_border">
<span id="welcome_username_text">Welcome <%=username%></span>
<a href="/CrimeReportingSystem5/view/RetrieveComplaints.do" id="view_complaints_tab">View Complaints</a>
<a href="/CrimeReportingSystem5/view/edit_profile.jsp" id="edit_profile_tab">Edit Profile</a>
<a href="/CrimeReportingSystem5/view/Logout.do" id="logout_profile_tab">Logout</a>
<div id="profile_layout">

</div>

</div>

</body>

</html>

我的问题:我想实现一个注销系统,我已经在登录servlet中生成了一个 session ,并且我决定在另一个注销servlet中使该 session 无效,但是它们之间有一个链接(请参阅 jsp),因此发送的请求将是对 servlet 的 GET 请求,我如何发送 session 信息。到注销 servlet 进行失效。
顺便说一句,我读过另一个答案,其中建议创建一个静态 Map 变量并将 JSESSIONID 和 session 存储为映射变量,但即使我这样做了,我怎么知道哪个用户点击了注销链接?
注意:我无法使用JavaScript 或 Jquery 来解决该问题,因为我还没有阅读它们。请提供一个简单的解决方案。

最佳答案

你在没有问题的地方看到了问题。

使 servlet 内的当前 session 失效非常容易:

// Check, if there is a session active. Avoid to create a new one.
HttpSession session = request.getSession(false);
if(session != null) {
session.invalidate();
}

如果此代码在与您创建 session 的位置不同的 servlet 中运行,也没关系。

在内部, session 通常由浏览器 cookie 管理。因此,当您创建新 session 时,服务器会向与该域关联的浏览器发送一个 cookie。然后,浏览器的每个后续请求都会将此 cookie 传输到您的服务器。然后,您的 servlet 服务器实现(如 Tomcat)会根据 Activity session 检查此 cookie,以便您的下一个 request.getSession() 返回与特定用户相对应的正确 session 。

关于java - 如何使在一个 Servlet 中初始化的登录 session 在另一个 Servlet 中初始化且中间有 href 的登录 session 无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24530536/

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