apache反向代理问题
今天早晨一个兄弟晕倒了apache2.2和tomcat6.0.18的反向代理问题,session保持不住,刷新页面的时候session不断的被创建。
写了个简单的工程做测试。这个简单工程从朋友那里收到的。他喜欢用servlet。。。。汗
web.xml的配置:
<servlet>
<description></description>
<display-name>CreateServlet</display-name>
<servlet-name>CreateServlet</servlet-name>
<servlet-class>com.temp.CreateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CreateServlet</servlet-name>
<url-pattern>/CreateServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>TempServlet</display-name>
<servlet-name>TempServlet</servlet-name>
<servlet-class>com.temp.TempServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TempServlet</servlet-name>
<url-pattern>/TempServlet</url-pattern>
</servlet-mapping>
两个servlet的代码,CreateServlet.java:
package com.temp;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class CreateServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public CreateServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(false);
// 不创建,看是否有session
if (session == null) {
// 没有才创建
session = request.getSession(true);
}
session.setAttribute("username", "aaaaaaa");
System.out.println(session.getId());
response.sendRedirect("TempServlet");
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
TempServlet.java
package com.temp;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class TempServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public TempServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(false);
// 坚决不创建session
System.out.println(session.getAttribute("username"));
System.out.println(session.getId());
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
访问index.jsp页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="CreateServlet">aaa</a>
</body>
</html>
apache的反向代理配置如下,首先打开#module的几个开关
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_http_module modules/mod_proxy_http.so
不知道多开没有,上次配置了2.2上的loadbalance的。
最下方他配置的
ProxyPass /temp http://localhost:8080/Temp
ProxyPassReverse /temp http://localhost:8080/Temp
访问http://localhost/temp后,不断刷新页面,出现了sessionId打印每次都不一致的问题。
后来修改了配置,改为
ProxyPass /temp http://localhost:8080/temp
ProxyPassReverse /temp http://localhost:8080/temp
再次访问,不断刷新页面,session保持住了,sessionId每次都一致。
看来这个大小写还是比较重要:)
所谓反向代理,可能是要求这个前面的/path后后面的/path是要保持一致的? 不是很确信。注意一下这个问题,以后再查查资料看。
今天早晨一个兄弟晕倒了apache2.2和tomcat6.0.18的反向代理问题,session保持不住,刷新页面的时候session不断的被创建。
写了个简单的工程做测试。这个简单工程从朋友那里收到的。他喜欢用servlet。。。。汗
web.xml的配置:
<servlet>
<description></description>
<display-name>CreateServlet</display-name>
<servlet-name>CreateServlet</servlet-name>
<servlet-class>com.temp.CreateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CreateServlet</servlet-name>
<url-pattern>/CreateServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>TempServlet</display-name>
<servlet-name>TempServlet</servlet-name>
<servlet-class>com.temp.TempServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TempServlet</servlet-name>
<url-pattern>/TempServlet</url-pattern>
</servlet-mapping>
两个servlet的代码,CreateServlet.java:
package com.temp;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class CreateServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public CreateServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(false);
// 不创建,看是否有session
if (session == null) {
// 没有才创建
session = request.getSession(true);
}
session.setAttribute("username", "aaaaaaa");
System.out.println(session.getId());
response.sendRedirect("TempServlet");
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
TempServlet.java
package com.temp;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class TempServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public TempServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(false);
// 坚决不创建session
System.out.println(session.getAttribute("username"));
System.out.println(session.getId());
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
访问index.jsp页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="CreateServlet">aaa</a>
</body>
</html>
apache的反向代理配置如下,首先打开#module的几个开关
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_http_module modules/mod_proxy_http.so
不知道多开没有,上次配置了2.2上的loadbalance的。
最下方他配置的
ProxyPass /temp http://localhost:8080/Temp
ProxyPassReverse /temp http://localhost:8080/Temp
访问http://localhost/temp后,不断刷新页面,出现了sessionId打印每次都不一致的问题。
后来修改了配置,改为
ProxyPass /temp http://localhost:8080/temp
ProxyPassReverse /temp http://localhost:8080/temp
再次访问,不断刷新页面,session保持住了,sessionId每次都一致。
看来这个大小写还是比较重要:)
所谓反向代理,可能是要求这个前面的/path后后面的/path是要保持一致的? 不是很确信。注意一下这个问题,以后再查查资料看。