Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help required about URLConnection

 
aditya seth
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...
 
Jaime M. Tovar
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?
 
Ben Souther
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.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
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