在上一篇分布式集群环境下,如何实现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浏览器测试