情况是这样的:
我想用URL类写一个获取网页的小程序.代码如下:
URL url = new URL(args[0]);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
urlConn.setRequestMethod("GET");
urlConn.setRequestProperty("Connection", "Keep-Alive");
urlConn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
urlConn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)");
urlConn.connect();
System.out.println("LEN:" + urlConn.getContentLength()); InputStream in = urlConn.getInputStream(); int n = in.available();
byte[] buf = new byte[1024];
while ( (n = in.read(buf)) >= 0) {
System.out.write(buf, 0, n);
}
这个代码对于某些网页是完全可以正确获取的.但是对于这样一个JSP确无法获取.我在TOMCAT下放了两个JSP:t1.jsp 和 t2.jsp
t1.jsp:
<%@ page contentType="text/html; charset=gb2312" %>
t1 PAGE <Br>
<%session.setAttribute("testattri","Hello,Test!");%>
<%response.sendRedirect("t2.jsp");%>
<%System.out.println("t1.jsp processed.");%>
<%System.out.println("SESSION id1:"+request.getSession().getId());%>t2.jsp:
<%@ page contentType="text/html; charset=gb2312" %>
t2 Page<br>
<%out.println( session.getAttribute("testattri"));%> <%System.out.println("t2.jsp processed.");%> <%System.out.println("SESSION id2:"+request.getSession().getId());%>功能就是,当访问t1.jsp时,往session里面放入一个字符串,然后跳转到t2.jsp显示出来.
这在浏览器中是完全正常的.但是用我上面写的这段程序却出了问题:<%out.println( session.getAttribute("testattri"));%>这句总是显示null.
后来我在两个页面中都加了<%System.out.println("SESSION id2:"+request.getSession().getId());%>,发现用这段程序访问时,两个页面的session竟然不一样,而用浏览器访问时却一样.
我想问题就出在session上面.但是如何改正我的程序才能让它产生与浏览器一样的效果呢??急切盼望您的解答!
我想用URL类写一个获取网页的小程序.代码如下:
URL url = new URL(args[0]);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
urlConn.setRequestMethod("GET");
urlConn.setRequestProperty("Connection", "Keep-Alive");
urlConn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
urlConn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)");
urlConn.connect();
System.out.println("LEN:" + urlConn.getContentLength()); InputStream in = urlConn.getInputStream(); int n = in.available();
byte[] buf = new byte[1024];
while ( (n = in.read(buf)) >= 0) {
System.out.write(buf, 0, n);
}
这个代码对于某些网页是完全可以正确获取的.但是对于这样一个JSP确无法获取.我在TOMCAT下放了两个JSP:t1.jsp 和 t2.jsp
t1.jsp:
<%@ page contentType="text/html; charset=gb2312" %>
t1 PAGE <Br>
<%session.setAttribute("testattri","Hello,Test!");%>
<%response.sendRedirect("t2.jsp");%>
<%System.out.println("t1.jsp processed.");%>
<%System.out.println("SESSION id1:"+request.getSession().getId());%>t2.jsp:
<%@ page contentType="text/html; charset=gb2312" %>
t2 Page<br>
<%out.println( session.getAttribute("testattri"));%> <%System.out.println("t2.jsp processed.");%> <%System.out.println("SESSION id2:"+request.getSession().getId());%>功能就是,当访问t1.jsp时,往session里面放入一个字符串,然后跳转到t2.jsp显示出来.
这在浏览器中是完全正常的.但是用我上面写的这段程序却出了问题:<%out.println( session.getAttribute("testattri"));%>这句总是显示null.
后来我在两个页面中都加了<%System.out.println("SESSION id2:"+request.getSession().getId());%>,发现用这段程序访问时,两个页面的session竟然不一样,而用浏览器访问时却一样.
我想问题就出在session上面.但是如何改正我的程序才能让它产生与浏览器一样的效果呢??急切盼望您的解答!
一般来说同一个进程访问一个URL,得到的session是相同的
你是不是用两个进程来取这两个网页的?
用的时候取得
你的这个程序必须要保存cookie(默认的应该是jsessionid),才能两次访问同一个会话。
---------------------------
请问该如何保存呢?这个jsessionid又该如何取得呢?我不能理解的是,在T1.JSP中,使用了JSP的跳转指令使之定向到T2.JSP,这应该是服务器端完成的工作,跟客户端应该没有什么关系吧?那为什么跳到T2.JSP后,在T1.JSP中产品的session又没了呢?
一个Web在服务器端同时会维护N个Session
一个请求来了,他怎么知道这个用户的Session是哪个呢?
靠一个SessionID,每个Session有个SessionID,这可以通过Session.getSessionID()获得。当一个请求来了,它能提供一个SessionID,而且此ID在服务器中能找到一个对应的Session时,就把该Session给这个请求。
默认情况下,客户的SessionID是存在Cookies中,用浏览器访问时,SessionID自动提交,所以你感觉不到。如果你禁用Cookies,则Session也会罢工。这时候你可以用http://XXX/abc/index.jsp?JSESSIONID=111111,类似的方式显示提交SessionID,就能让Session继续工作了。所以你的程序中,你需要找到Cookies,获得JSESSIONID的值,然后显示提交。
另一种方式是在Redirect时URL上动手脚,在服务器端动。具体怎么做自己想吧
你的程序Redirect后并没有保存Cookie,所以Session就不一样了.下面是我的部分代码:package net.oicp.sunflowerbbs;import java.net.*;
import java.io.*;public class MyHttpConnection { /**
* @param args
*/
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost/WebTest/t1.jsp");
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
// 从响应中检索会话ID.
String cookieVal = urlConn.getHeaderField("Set-Cookie");
String sessionId="";
if (cookieVal != null) {
//注意这里不同容器里面的字段分隔符不一样,这里是tomcat5.5
sessionId = cookieVal.substring(0, cookieVal.indexOf(";"));
urlConn.disconnect();
}
//注意这里reOpen
urlConn = (HttpURLConnection) url.openConnection();
//发送cookie.
if(sessionId != null)
{
urlConn.setRequestProperty("Cookie", sessionId);
}
urlConn.setRequestMethod("GET");
urlConn.setRequestProperty("Connection", "Keep-Alive");
urlConn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
urlConn
.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)");
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
urlConn.setAllowUserInteraction(false);
urlConn.connect();
System.out.println("LEN:" + urlConn.getContentLength()); InputStream in = urlConn.getInputStream();
int code = urlConn.getResponseCode();
//code==302 是重定向
int n = in.available();
byte[] buf = new byte[1024];
while ((n = in.read(buf)) >= 0) {
System.out.write(buf, 0, n);
}
}}
嗯,非常感谢各位的回复.等中午我试一下IhaveGotYou(扬扬)的方法.
非常感谢.
再次感谢各位的支持与赐教.特别感谢 " IhaveGotYou "! 分数不多,略表谢意.