| Author |
calling servlet from java program and more
|
Vijaishanker bala
Ranch Hand
Joined: Sep 08, 2005
Posts: 82
|
|
Hi, Is it possible to call a servlet from a java program. The application is as follows, 1.I get a list(an ArrayList) of search results from a web service to a java program. 2.In this java program, i perform some functions on the obtained list. 3.In the next step, the List of results need to be passed on to a servlet, from the java program(this is where i need some help). Being specific on my query, I would like to pass an arrayList of objects to a servlet from a java program.Can some one please tell me, how this can be done. Thanks Vijai [ June 19, 2006: Message edited by: Bear Bibeault ]
|
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." <br />Linus Torvalds
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12267
|
|
The simplest thing to do would be to serialize the ArrayList to a File that is placed where the servlet can read it as required. This would be possible if: 1. all objects in the ArrayList are Serializable 2. your application and the servlet have access to the same disk directory somehow If these conditions do not hold then things get more complicated. Bill
|
Java Resources at www.wbrogden.com
|
 |
Vijaishanker bala
Ranch Hand
Joined: Sep 08, 2005
Posts: 82
|
|
Thanks William for your reply.From what u replied,instead of serializing it to a file, I think I will serialize the List to a Binary Output Stream and send it to the servlet using the java.net api and at the servlet side, deserialize it and use it there.I guess that shouldnt be a problem with the sandbox security required in creating a file and serializing to it. Thanks again Vijai
|
 |
Carl Trusiak
Sheriff
Joined: Jun 13, 2000
Posts: 3340
|
|
|
You don't need to serialize it to a file. Look at the code for ObjectServlet and HTTP for and example in the Code Barn
|
I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
|
 |
Harpreet Hira
Ranch Hand
Joined: Sep 27, 2001
Posts: 72
|
|
|
This thread discusses similar problem
|
 |
Darren Edwards
Ranch Hand
Joined: Aug 17, 2005
Posts: 69
|
|
|
This may not apply as I do not know the full details of your environment, but do you really need to pass to a servlet? Perhaps it is more appropiate for a servlet to do all the work (assuming it is a process started by a user and a web browser) or maybe you only require use of libraries that the servlet has?
|
 |
Vijaishanker bala
Ranch Hand
Joined: Sep 08, 2005
Posts: 82
|
|
Thanks for the help everyone. My code is as follows... This is the part which calls the servlet. And this is the servlet code which takes the result. But the problem right now is, I dont know if the servlet is being called and if any exception has occured. How can I test if the servlet is being called, i.e. like writing something to a file or something else. Thanks again Vijai
|
 |
Saritha Penumudi
Ranch Hand
Joined: Aug 18, 2003
Posts: 146
|
|
You can give log statements after each step. Also, catch Excpetion along with other exceptions. Just curious to know about the requirement. What servlet will do after it get results back from your java program. Looks like this the back ground process that runs and returns results to UI. Are you updating UI dynamically based on the results? [ June 19, 2006: Message edited by: Saritha ventrapragada ]
|
 |
Vijaishanker bala
Ranch Hand
Joined: Sep 08, 2005
Posts: 82
|
|
Hi, Thanks to everyone...I finally managed to make the java code communicate with the servlet work..Thanx Carl, that link was extremely useful and Harpreet Hira... thanks to ur link to a similar post, I managed to pick up some useful pointers....and Darren, my application requirements should I decide to accept it, is to connect to an already active session and get a User Context(which I can access only with servlet API),hence I needed to bridge the java code and the session part of the application. Thanks Vijai and herewith I am posting the code that worked should it help someone else The code that calls the servlet and passes an object(an ArrayList) to it. The servlet's doPost that reads the object, in this case an ArrayList and sends the size of the ArrayList back to the java code that called it
|
 |
Vijaishanker bala
Ranch Hand
Joined: Sep 08, 2005
Posts: 82
|
|
To Saritha: read your post late, The servlet takes up a list of results, for example, search results, and based upon a few other parameters, creates a JSP that is supposed to vary depending upon other dependent parameters Vijai
|
 |
Bindu Sanju
Greenhorn
Joined: Feb 23, 2006
Posts: 16
|
|
Hi Vijaishankar, I write the colde like this , I am getting exception, please help me. --------sender file --------------- import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.PrintWriter; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.List; import javax.servlet.Servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * @version 1.0 * @author */ public class URLSender extends HttpServlet implements Servlet { public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { ArrayList aList = new ArrayList(); aList.add("1"); aList.add("2"); aList.add("3"); aList.add("4"); aList.add("5"); aList.add("6"); aList.add("7"); aList.add("8"); aList.add("9"); URL aURL = new URL("http://localhost:9080/GUTA/URLReceiver"); System.out.println("URL : "+aURL); URLConnection aConnection = aURL.openConnection(); System.out.println("URL COnnection : "+aConnection); aConnection.setDoInput(true); aConnection.setDoOutput(true); aConnection.setUseCaches(false); aConnection.setDefaultUseCaches(false); aConnection.setRequestProperty("Content-Type", "application/octet-stream"); ObjectOutputStream out = new ObjectOutputStream(aConnection .getOutputStream()); out.writeObject(aList); out.flush(); out.close(); System.out.println("End of the file"); ObjectInputStream in = new ObjectInputStream(aConnection.getInputStream()); String text = (String) in.readObject(); System.out.println(text); } catch (Exception e) { e.printStackTrace(); } } } -----------receiver file in different application--------- import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OptionalDataException; import java.io.OutputStream; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.util.List; import javax.servlet.Servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * @version 1.0 * @author */ public class URLReceiver extends HttpServlet implements Servlet { public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); PrintWriter out=resp.getWriter(); out.println("Hi this is Receiver URL"); System.out.println("Hi this is Receiver URL"); ObjectInputStream resultStream = null; ObjectOutputStream sendStream = null; List results = null; try { resultStream = new ObjectInputStream(req.getInputStream()); results = (List) resultStream.readObject(); resultStream.close(); sendStream = new ObjectOutputStream(resp.getOutputStream()); //sendStream.writeObject(String.valueOf(results.size())); System.out.println("Hi this is Receiver Size "+String.valueOf(results.size())); sendStream.flush(); sendStream.close(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } /*try { resp.setContentType("text/html"); PrintWriter out=resp.getWriter(); out.println("Hi this is Receiver URL"); System.out.println("Hi this is Receiver URL"); //URL userloginservlet = new URL("http://localhost:9080/DEN/URLSender"); //System.out.println("URL : "+userloginservlet); //URLConnection servletConnection = userloginservlet.openConnection(); //ObjectOutputStream oos = new ObjectOutputStream(servletConnection.getOutputStream()); ObjectInputStream ois = new ObjectInputStream(req.getInputStream()); DataBean p = (DataBean) ois.readObject(); while(true){ System.out.println("Name Value is : "+p.getName()); System.out.println("Pass Value is : "+p.getPass()); } //DataBean db=new DataBean(); //db.setName("Yandra"); //db.setPass("ramsea"); //oos.writeObject(db); //ObjectInputStream ois = new ObjectInputStream(servletConnection.getInputStream()); ////outs = servletConnection.getOutputStream(); //DataBean db; //db = (DataBean) ois.readObject(); //db.setName("Yandra"); //db.setPass("ramsea"); //System.out.println("URL COnnection : "+servletConnection); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } */ } } ---------exception------------------- [4/17/07 15:42:54:000 CDT] 60e0c639 WebGroup E SRVE0026E: [Servlet Error]-[URLReceiver]: java.lang.IllegalStateException: Writer already obtained at com.ibm.ws.webcontainer.srt.SRTServletResponse.getOutputStream(SRTServletResponse.java:490) at URLReceiver.service(URLReceiver.java:41) at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.java:110) at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.java:174) at com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java:313) at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.java:116) at com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java:283) at com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletReferenceState.java:42) at com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceReference.java:40) at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java:948) at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:530) at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:176) at com.ibm.ws.webcontainer.srt.WebAppInvoker.doForward(WebAppInvoker.java:79) at com.ibm.ws.webcontainer.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:201) at com.ibm.ws.webcontainer.cache.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:71) at com.ibm.ws.webcontainer.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:182) at com.ibm.ws.webcontainer.oselistener.OSEListenerDispatcher.service(OSEListener.java:334) at com.ibm.ws.webcontainer.http.HttpConnection.handleRequest(HttpConnection.java:56) at com.ibm.ws.http.HttpConnection.readAndHandleRequest(HttpConnection.java:610) at com.ibm.ws.http.HttpConnection.run(HttpConnection.java:431) at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:593)
|
 |
NageshPilla
Greenhorn
Joined: Dec 12, 2008
Posts: 1
|
|
|
delete Printwriter out=resp.getWriter(); from URLReciever Servlet.
|
 |
Anil K Battu
Greenhorn
Joined: Jan 26, 2011
Posts: 1
|
|
I did the same implementation..Control is NOT coming to the other servlet, to read the object.
please help me in this regard
|
 |
 |
|
|
subject: calling servlet from java program and more
|
|
|