2016-04-28 53 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} 
相關問題