分布式集群環境下,如何實現session共享二(項目開發)


在上一篇分布式集群環境下,如何實現session共享一(應用場景)中,介紹了在分布式集群下,需要實現session共享的應用場景。並且最后留下了一個問題:在集群環境下,如何實現session的共享呢?。要解決這個問題,放在一篇中內容量有點大,還是一步一步來吧。本篇先搭建一個基礎的web應用,完全基於原生態的servlet實現。思路是這樣的:

  1.准備一個頁面index.jsp,頁面中可以提交key/value對的請求參數數據數據

  2.編寫一個servlet,接收頁面提交的請求,獲取請求參數,並且設置到會話域session中

  3.最后通過重定向的方式,回到index.jsp頁面,並且從session中獲取數據進行展示

 

1.創建項目

 

 

2.配置pom.xml導入依賴

 

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.anan</groupId>
  <artifactId>session-redis-demo</artifactId>
  <version>1.0</version>
  <packaging>war</packaging>

  <properties>
    <!-- jstl標簽版本 -->
    <jstl.version>1.2</jstl.version>
    <!--servlet版本-->
    <servlet.version>2.5</servlet.version>
    <!--jsp版本-->
    <jsp.version>2.0</jsp.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>

    <!--servlet依賴-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>${servlet.version}</version>
      <scope>provided</scope>
    </dependency>
    <!--jsp依賴-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jsp-api</artifactId>
      <version>${jsp.version}</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>${jstl.version}</version>
    </dependency>

  </dependencies>

  <build>
    <finalName>session-redis-demo</finalName>

  </build>
</project>

 

3.編寫jsp頁面

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Session Attributes</title>

</head>
<body>
<div class="container">

    <form class="form-inline" role="form" action="./session" method="post">
        <label for="attributeName">屬性名稱</label>
        <input id="attributeName" type="text" name="attributeName"/>
        <label for="attributeValue">屬性值</label>
        <input id="attributeValue" type="text" name="attributeValue"/>
        <input type="submit" value="設置"/>
    </form>

    <hr/>

    <table class="table table-striped">
        <thead>
        <tr>
            <th>屬性名稱</th>
            <th>屬性值</th>
        </tr>
        </thead>
        <tbody>
        <c:forEach items="${sessionScope}" var="attr">
            <tr>
                <td><c:out value="${attr.key}"/></td>
                <td><c:out value="${attr.value}"/></td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</div>
</body>

</html>

 

4.編寫servlet

 

package com.anan.servlet;

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 java.io.IOException;

/**
 * Servlet
 */
public class SessionServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    // post方法
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // 請求參數
        String attributeName = request.getParameter("attributeName");
        String attributeValue = request.getParameter("attributeValue");

        // session對象
        HttpSession session = request.getSession();
        session.setAttribute(attributeName, attributeValue);

        response.sendRedirect(request.getContextPath() + "/");
    }


    // get方法
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

 

5.配置web.xml

 

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">

  <display-name>session</display-name>


  <!--配置Servlet-->
  <servlet>
    <servlet-name>session</servlet-name>
    <servlet-class>com.anan.servlet.SessionServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>session</servlet-name>
    <url-pattern>/session</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

6.測試

6.1.谷歌瀏覽器測試

 

6.2.IE瀏覽器測試

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM