Why can Tomcat not connect from one servlet to another on the same Tomcat instance (and context) via HTTP?
-
If this is not possible what is the best workaround for testing on a single machine a multi-machine inter-Tomcat API based service? Note the servlets are in the same context (see comments to 's answer) The following code works standalone: public class Post { public static void main(String[] args) throws IOException { URL url=new URL("Page on localhost:8169"); HttpURLConnection conn=(HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "text/plain"); conn.setDoOutput(true); conn.connect(); OutputStream os=conn.getOutputStream(); os.write(getCommand().getBytes()); os.close(); BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream())); while(br.ready()) System.out.println(br.readLine()); } public static String getCommand() throws IOException { BufferedReader br=new BufferedReader(new FileReader("/usr/home/aryeh/specmed.C227/src/samples/addDoctor.json")); String s=""; while(br.ready()) s+=br.readLine(); br.close(); return s; } } but when doing it via a servlet on the same tomcat (localhost:8169) does not work: public class PlayServlet extends HttpServlet { public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException { PrintWriter pw=new PrintWriter(res.getWriter()); Post.main(null); } public static final long serialVersionUID=0L; } Stack trace: ]ava.io.IOException: Server returned HTTP response code: 500 for URL: Page on localhost:8169 sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1626) play.Post.main(Post.java:25) play.PlayServlet.doPost(PlayServlet.java:13) javax.servlet.http.HttpServlet.service(HttpServlet.java:647) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
-
Answer:
How to educate on this? This is not how you do web programming in the Servlet API. Based on what I can glean from this attempt, you're trying to get a servlet to call another on a server to return a JSON file. So you want the Play servlet to call some other web servlet to dump the file out. Do you understand the error you're getting? Do you know why? The PlayServlet isn't doing anything. You're getting a writer to send web output to and then not doing anything with it. The main() method of the Post class is reading your file and outputting to the System console. It isn't sending it back to your servlet. Because you didn't paste the entire source code including the imports, the stack trace is off but I suspect the source of the IOException is System.out.println(br.readLine());. This is not correct code by any stretch of the imagination. I can see you're trying to chain together web calls without understanding how the web or servlets, in particular, work. If the servlet /foo/api is in the same context root as the Play servlet, you can do what is called a "forward". This sends the servlet request from one servlet to another within the same application. You will need to know this to do any kind of MVC programming. It is known as "servlet chaining" and is necessary for Servlet/JSP interactions. If the Play servlet is in a different application context (i.e. bound to /play instead of /foo),m then you need to do the web request inside the doGet/doPost method of the Play servlet. And then use the HttpServletRequest/Response streams to take the output from the one servlet over the network and bridge it out via the Play servlet. While you can use HttpUrlConnection, a dedicated HTTP client API is preferable. I, like many, use Apache HttpClient for this. It's kind of hard to explain this in a Quora answer since it is not interactive. If you've like some assistance, feel free to drop me a note.
Matt Pickering at Quora Visit the source
Related Q & A:
- Why can't I connect to my Yahoo messenger?Best solution by Yahoo! Answers
- Why can't I connect to yahoo messenger?
- How can I move bookmarks from one email to another one?Best solution by pcworld.com
- How can I transfer contacts from one phone to another?Best solution by eHow old
- Can you move photos from one album to another?Best solution by wiki.answers.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.