0

I am hitting one url for login

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());
}

after the successful hit url for login when i hit another url

http://test/afterLogin.jsp

I do not get output because of afterLogin.jsp not able to get session valule of username and password and i am also setting one variable in session in the login.jsp page

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

Is there any way by using core java to hit second url within the session of first url? so that afterLogin.jsp able to get every value of session that i have in login.jsp

Kunal Batra
  • 821
  • 3
  • 14
  • 35
  • read the cookie and send it in the following requests – JEY Apr 28 '16 at 12:39
  • u mean to say if i set value using session.setAttribute("someparamValue", "value"); then i can read that value using cookies? – Kunal Batra Apr 28 '16 at 12:42
  • your login request should return a cookie that contains a session id that's what you need to send in the following requests. Please read how http works. https://en.wikipedia.org/wiki/HTTP_cookie – JEY Apr 28 '16 at 12:45

1 Answers1

0

Thank you, BalusC, for pointing out the CookieHandler. I know using scriptlets is a sin. But, I will just use them for this demonstration code. In the page directive of the sender page, I turned off automatic session creation. That way we can test using a single browser. Please place these three JSPs into the root folder of your ROOT(if you are using Tomcat) web app. Otherwise you will have to manually insert your context path into the URLs.

<%@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} 
rickz
  • 4,324
  • 2
  • 19
  • 30