This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes help required about URLConnection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "help required about URLConnection " Watch "help required about URLConnection " New topic
Author

help required about URLConnection

aditya seth
Greenhorn

Joined: Nov 01, 2005
Posts: 11
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

Joined: Mar 28, 2005
Posts: 133
Sorry but why you are trying to connect to another servlet?


She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

"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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
aditya seth
Greenhorn

Joined: Nov 01, 2005
Posts: 11
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

Joined: Dec 11, 2004
Posts: 13410

Once you get your screenname issue straightened out, I have some other questions about how you are implementing this.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Nov 01, 2005
Posts: 11
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

Joined: Dec 11, 2004
Posts: 13410

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


I'm glad it's working for you.
Welcome to the ranch.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help required about URLConnection
 
Similar Threads
applet - servlet communication
how we can post to another url from servlet ?
HELP...Form Submission using java.net.HttpURLConection?
EOF error
Check dead link with Javascript