1

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.

Getting request payload from POST request in Java servlet

Community
  • 1
  • 1
Somnath Musib
  • 3,548
  • 3
  • 34
  • 47

3 Answers3

1

If you can't retrieve any data in the second servlet, it's probably because you're calling something like request.getParameter() /getParameterValues()/getParameterMap() somewhere. Indeed these methods call getInputStream() and you can only read once the content of the request.

alain.janinm
  • 19,951
  • 10
  • 65
  • 112
  • Thanks for the response. The idea is My first application will always write some content in the url. In second application my servlet should retrieve the content written by my first application. My problem is the writing by first application is fine, but in my servlet I am not getting the content. I need the content in my servlet, I do not want to send anything back to my first application. – Somnath Musib Oct 03 '14 at 09:12
  • @Som Sry didn't understand well your need, I've edited my answer. – alain.janinm Oct 03 '14 at 09:46
  • Thanks for the response. Your earlier answer was correct. I have one Java program which writes some message in the url. I have a separate servlet (a separate application) which is suppose to receive the request and print the message written by first java program. But I could not achieve it. What I had achieve so far is `Write the message in URL (Java Program1) -> do the processing in Servlet (Java Program2) -> Print the processed message (Java Program1)` Thanks for showing the path. But my original problem is still remain unresolved. – Somnath Musib Oct 03 '14 at 10:09
  • Ok, if you debug, do you have content in your reader? With current infos I can't help you more. It's either the request that has already been read hence can't be read twice. Or you read the request correctly but the way you read it is wrong. Also when you say `print the message written by first java program`, you mean print where exactly? – alain.janinm Oct 03 '14 at 11:39
0

I had used this code in android, and it may help you, It may not be the complete solution

Writing to Server

 try{

               String u="hello";
               String p="world";

                 URL url=new URL("http://localhost:8080/LoginExample/LoginServlet?"+"key1="+u+"&key2="+p);

                HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();

                urlConnection.setRequestMethod("GET");
                urlConnection.connect();


            }catch(Exception e)
                {
                e.printStackTrace();    
                System.out.println("ERROR IN URL CONNECTION---"+e);
                }

ServletPage

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) 
               throws ServletException, java.io.IOException {

    try
    {       
        System.out.println("-----servlet--------------");
        // UserBean user = new UserBean();
        String uname=request.getParameter("key1");
        String password=request.getParameter("key2");

        System.out.println("uname ins ervlet==="+uname);

        System.out.println("password in servlet==="+password);
    }               
    catch (Throwable theException)      
    {
        System.out.println(theException); 
    }

 }

 }
Anptk
  • 1,125
  • 2
  • 17
  • 28
  • Thank you so much for the response. I have tried with your program. But this does not working. I run the first program and then the servlet, I am getting _uname ins ervlet===null_ and _password in servlet===null_ – Somnath Musib Oct 03 '14 at 09:20
-1

i think you should cast the result to string so the code is similar to this(in your servlet Page):

String uname=(String)request.getParameter("key1");//cast to string
String password=(String)request.getParameter("key2");//cast to string
Zheng Qu
  • 781
  • 6
  • 22
Simo
  • 33
  • 8