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?

4

1 回答 1

1

HttpServletResponse除了某些标头(例如响应代码)之外,它本身并没有为您提供将数据写回客户端的方法。

但是,它有一个被调用的方法getOutputStream和一个getWriter()为您提供响应的方法。一个OutputStream或一个PrintWriter。您可以使用这些将数据写入响应。

于 2015-08-04T12:37:03.807 回答