2016-04-28 49 views
0

我打一个网址进行登录如何用第一个网址的会话命中第二个网址?

public void hittingurl(){ 
String url = "http://test/login.jsp?username=hello&password=12345"; 

     URL obj = new URL(url); 
     HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 

     // optional default is GET 
     con.setRequestMethod("GET"); 

     int responseCode = con.getResponseCode(); 
     System.out.println("\nSending 'GET' request to URL : " + url); 
     System.out.println("Response Code : " + responseCode); 

     BufferedReader in = new BufferedReader(
       new InputStreamReader(con.getInputStream())); 
     String inputLine; 
     StringBuffer response = new StringBuffer(); 

     while ((inputLine = in.readLine()) != null) { 
      response.append(inputLine); 
     } 
     in.close(); 

     //print result 
     System.out.println(response.toString()); 
} 

成功命中网址后登录 当我打另一个URL

http://test/afterLogin.jsp 

我不明白由于afterLogin.jsp输出不能得到 se用户名和密码 的裂变valule我也是在login.jsp页面

session.setAttribute("someparamValue", "value"); 

设置一个变量在会话有没有办法通过使用Java核心到的会话 内打第二网址第一个网址? 使afterLogin.jsp能够得到会议的每一个值,我login.jsp中

+0

读取cookie,并在以下请求 – JEY

+0

发送ü的意思是说,如果我使用session.setAttribute(设定值“someparamValue”,“value”); 然后我可以使用cookie读取该值? –

+0

您的登录请求应该返回一个包含会话标识的cookie,这是您在以下请求中需要发送的内容。请阅读http的工作方式。 https://en.wikipedia.org/wiki/HTTP_cookie – JEY

回答

0

有 谢谢BalusC,用于指明了的CookieHandler。我知道使用scriptlets是一种罪过。但是,我只是将它们用于此演示代码。在发件人页面的页面指令中,我关闭了自动创建会话。这样我们可以使用单个浏览器进行测试。请将这三个JSP放置到ROOT的根文件夹(如果您使用的是Tomcat)Web应用程序。否则,您将不得不手动将上下文路径插入到URL中。

<%@page import="java.io.*,java.net.*" session="false"%> 
<%! 
public void jspInit() { 
    CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); 
} 
public void hittingURL(JspWriter out, String url) throws Exception{ 
    URL obj = new URL(url); 
    HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 
    con.setRequestMethod("GET"); 
    int responseCode = con.getResponseCode(); 
    out.print("Sending 'GET' request to URL : " + url + "<br/>"); 
    out.print("Response Code : " + responseCode + "<br/>"); 
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); 
    String inputLine; 
    StringBuffer responseBuffer = new StringBuffer(); 
    while ((inputLine = in.readLine()) != null) { 
     responseBuffer.append(inputLine); 
    } 
    in.close(); 
    out.print(responseBuffer.toString() + "<br/>"); 
    return; 
} 
%> 
<% 
    hittingURL(out, "http://localhost:8080/target1.jsp"); 
    hittingURL(out, "http://localhost:8080/target2.jsp"); 
%> 

targer1.jsp

<% 
    session.setAttribute("someKey", "myValue"); 
%> 
Hi from target1.jsp 
someKey = ${someKey} 
The session id is ${pageContext.session.id} 

target2.jsp

Hi from target2.jsp 
someKey = ${someKey} 
The session id is ${pageContext.session.id} 
相关问题