1

I am a complete beginner in web programming. I created an application with Eclipse Java EE and a Tomcat server running in localhost.

The goal of the application is to get information from a client and send back other information.

I developped a servlet and implement a doPost() method that works perfectly. I get information that I saved in a bean named USSDPull and write in a text file named log.txt.

public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException{


USSDPull ussdpull = new USSDPull();
                ussdpull.setUssdstring(request.getParameter("ussdstring"));
                ussdpull.setSessionid(Integer.parseInt(request.getParameter("sessionid")));
                ussdpull.setMsisdn(request.getParameter("msisdn"));
                ussdpull.setUssdcode(request.getParameter("ussdcode"));
                ussdpull.setEncoding(Integer.parseInt(request.getParameter("encoding")));

                response.setContentType("text/text");
                response.setCharacterEncoding( "UTF-8" );
                PrintWriter out = response.getWriter();
                out.flush();

                out.println("POST received : " + ussdpull.getUssdstring()+" "+ussdpull.getSessionid()+" "+ussdpull.getMsisdn()+" "+ussdpull.getUssdcode()+" "+ussdpull.getEncoding());

                //WRITE IN FILE
                FileWriter fw = new FileWriter("D:/Users/Username/Documents/log.txt", true);
                BufferedWriter output = new BufferedWriter(fw);
                output.write(dateFormat.format(date)+";"+ussdpull.getUssdstring()+";"+ussdpull.getSessionid()+";"+ussdpull.getMsisdn()+";"+ussdpull.getUssdcode()+";"+ussdpull.getEncoding()+"\n");  
                output.flush();
                output.close();
}

I need the servlet to send back 2 specific booleans and 1 string to the client. I don't know how to proceed. Is it possible to use the HttpServletResponse to send the data? Or do I need to find a way to "call" the doGet() method?

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Oxydroid
  • 21
  • 1
  • 6
  • You're sending back data in your example code already ("POST received" etc.). What exactly is your problem? – Kayaman Aug 04 '15 at 12:45
  • I wrote this only to check the data. Println() is enough to really send data? I thought it was only to display info in the command line since I used curl to simulate interaction. Basicaly, the data came from a device. Next, the data come to a service and they are re-sent to my application. I need to send back data from the server to tell the service if I weel received the information needed. – Oxydroid Aug 04 '15 at 12:51
  • Well, you're sending back character data by getting the response writer and printing to it. Curl is just displaying the response it got, if you were to run it in a browser you'd see it as text. Now it's up to you how you want to send your data. As text representation (maybe even JSON) or as binary data (although with the amount of data you're sending there's very little matter). – Kayaman Aug 04 '15 at 12:52
  • Thank you very much Kayaman ! I will search more deeply about JSON and binary data to complete my app. – Oxydroid Aug 04 '15 at 13:08

1 Answers1

1

The HttpServletResponse itself doesn't give you a way to write data back to the client other than some headers, such as the response code.

However, it has a method called getOutputStream and a method getWriter() that give you resp. an OutputStream or a PrintWriter. You can use these to write data to the response.

mthmulders
  • 9,483
  • 4
  • 37
  • 54