I am trying to write some message in HttpURLConnection
and I am trying to retrieve the same in a Servlet. These are two different applications.
Message Write Code (Without Exception Handling)
public static void formUrlAndWrite()
{
URL url = null;
HttpURLConnection httpConnection = null;
OutputStreamWriter outStream = null;
String host = "http://127.0.0.1:8080/HttpTester";
url = new URL(host);
httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setDoOutput(true);
httpConnection.setAllowUserInteraction(true);
httpConnection.setDefaultUseCaches(false);
outStream = new OutputStreamWriter(httpConnection.getOutputStream());
outStream.write("This is a plain text");
outStream.flush();
}
Servlet Code (Without Exception Handling)
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException
{
BufferedReader reader = null;
PrintWriter out = null;
StringBuffer buffer = new StringBuffer();
try
{
//reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
reader = request.getReader();
int read = 0;
char[] charArray = new char[2000];
while((read=reader.read(charArray))>-1)
{
buffer.append(charArray, 0, read);
}
response.setContentType("text/html");
out = response.getWriter();
out.println("<html><body>");
out.print("<h2> Request Buffer is"+buffer+"</h2>");
out.println("</body></html>");
}
}
These two applications are running asynchronously and I am not getting the output from my servlet. My doubt is If I run the message write app, it will write the data in the url. Then when I am running the servlet, how does it get the request? Probably I am doing something wrong in this. Any help is appreciated.
I have tried the below URL which is quite similar to my problem, but it does not help.