Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

help required about URLConnection  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..
i m using urlconnection class in one servlet(say connecttoServer.java) to connect another servlet.
After fist read and write connection becomes null.
Is there any way to connect to same servlet?
also plz guide me to know
How to connect to another servlet from connecttoServer.java?

thx in advance...
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry but why you are trying to connect to another servlet?
 
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by javabeans:
Hi..
i m using urlconnection class in one servlet(say connecttoServer.java) to connect another servlet.
After fist read and write connection becomes null.
Is there any way to connect to same servlet?
also plz guide me to know
How to connect to another servlet from connecttoServer.java?

thx in advance...



HTTP is a stateless protocol.
You make a request, and the server responds.
After that, if you want to do more, then you establish another connection.
 
ranger
Sheriff
Posts: 17314
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"javabeans"

Please click the My Profile link above to change your display name to match JavaRanch's Naming Policy of using your real first and real last names.

Thanks

Mark
 
aditya seth
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thx for reply...
actually i have one servlet that will take userId an userPassword and sends this info. to remote servlet .I m using urlconnection for this purpose.
But aftrer first write and read is done connection is null.
I f i want to write more data i m making another new connection with different servlet.
But problem is that servlet is not getting executed.


send reply...
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once you get your screenname issue straightened out, I have some other questions about how you are implementing this.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aditya,

Can you post the code to your servlet or describe the program flow for what you are doing?

Where, in the servlet, are you establishing the URLConnection to the other resource?
 
aditya seth
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ...
thx for reply..
program is working now...
actually i was trying break the connection establishment function ,write to stream function and reading response function.
somewhere it went wrong and i was getting that problem.

The simplest code will be..
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.net.*;


public class TestingServlet extends HttpServlet
{

public void init(ServletConfig config)
throws ServletException
{
System.out.println("once done");
}

public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
URLConnection con;
try
{
con = getConnection("http://ipaddress:8080/servlets-examples/servlet/TestingServletServer");
writeToServlet(con);
Boolean result = readFromServlet(con);

con = getConnection("http://ipaddress:8080/servlets-examples/servlet/TestingServletServer2");
writeToServlet(con);
Boolean result2 = new Boolean(true);
result2 = readFromServlet(con);
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Servlet Testing</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("Welcome to the Servlet Testing Center"+result+"\t"+result2);
out.println("</BODY>");
out.println("</HTML>");
System.out.println("RESULT "+result+"RESULT 2 "+result2);
}
catch(Exception e)
{
System.out.println("Exception caught "+e);
}
}
private void writeToServlet(URLConnection con)
{
try
{
ObjectOutputStream oos=new ObjectOutputStream(con.getOutputStream());
oos.writeObject(new Boolean(true));
oos.flush();
oos.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
private Boolean readFromServlet(URLConnection con)
{
Boolean result = null;
try
{
ObjectInputStream ois=new ObjectInputStream(con.getInputStream());
result = (Boolean)ois.readObject();
}
catch(Exception e)
{
System.out.println(e);
}
return result;
}
private URLConnection getConnection(String RL)
{
URLConnection con = null;
try
{
URL url = new URL(RL);
con = url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);

}
catch(Exception e)
{
System.out.println(e);
}
return con;
}
}


code for TestingServletServer
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class TestingServletServer extends HttpServlet
{

public void init(ServletConfig config)
throws ServletException
{
System.out.println("once done");
}
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
doGet(req,res);
}
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
try
{
System.out.println("inside doGet");
res.setContentType("Application/Octect-Stream");
ObjectInputStream ois=new ObjectInputStream(req.getInputStream());
Boolean result = (Boolean) ois.readObject();
ObjectOutputStream oos=new ObjectOutputStream(res.getOutputStream());
oos.writeObject(new Boolean(true));
oos.flush();
oos.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}


code for TestingServletServer2
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class TestingServletServer2 extends HttpServlet
{

public void init(ServletConfig config)
throws ServletException
{
System.out.println("once done 2");
}
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
doGet(req,res);
}
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
try
{
System.out.println("inside doGet 2");
res.setContentType("Application/Octect-Stream");
ObjectInputStream ois=new ObjectInputStream(req.getInputStream());
Boolean result = (Boolean) ois.readObject();
ObjectOutputStream oos=new ObjectOutputStream(res.getOutputStream());
oos.writeObject(new Boolean(false));
oos.flush();
oos.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi ...
thx for reply..
program is working now...



I'm glad it's working for you.
Welcome to the ranch.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!