How to forward current Session in HttpURLConnection?

UPDATED: 10 October 2014
Server Room

HttpURLConnection is helps to use HTTP-specific features. Each HttpURLConnection instance is used to make a single request however it can be shared by other instances at the server. The most common use of HttpURLConnection is to make HTTP connection with Cookies, CustomHeader, Set GET/POST Methods, etc...

I used HttpURLConnection to make HTTP call from Servlet, I also required current session to be forwarded to called Servlet. You may have your different scenario.

Source Code
try {
 /* ID of Session you want to forward */
 String sessionId = "";
 /* Reading response data */
 String inputLine = "";

 URL data = new URL("");
 /* comment below line in case of Proxy */
 HttpURLConnection con = (HttpURLConnection) data.openConnection();
 /* Forward current session in request */
 con.setRequestProperty("Cookie", "JSESSIONID=" + URLEncoder.encode(sessionId, "UTF-8"));
 /* Read webpage coontent */
 BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
 /* Read line by line */
 while ((inputLine = in.readLine()) != null) {
 /* close BufferedReader */
 /* close HttpURLConnection */
} catch (Exception e) {

How Session forwarded/passed in HttpURLConnection request?
Server access Cookies to get Session ID value. JSESSIONID cookie used by server to identify session. If Session ID is valid then server allows you to access that session. This is how it works.

I tested code with Apache Tomcat and JBoss and its working fine. I don't know about other web server but It should work because all web server uses common standards.

Java Documentation: