- How to send a HTTP Response to a HTTP Request in a Live Thread
- public void handle(String target, HttpServletRequest request,
- HttpServletResponse response, int dispatch)
- throws IOException {
- // Scan request into a string
- Scanner scanner = new Scanner(request.getInputStream());
- StringBuilder sb = new StringBuilder();
- while (scanner.hasNextLine()) {
- sb.append(scanner.nextLine());
- }
- GET / HTTP/1.1
- Host: 10.10.10.100:8800
- User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0) Gecko/20100101 Firefox/10.0
- Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
- Accept-Language: en-us,en;q=0.5
- Accept-Encoding: gzip, deflate
- Connection: keep-alive
- HTTP/1.1 200
- POST something back to the GET Request
- public void handle(String target, HttpServletRequest request,
- HttpServletResponse response, int dispatch)
- throws IOException {
- // Scan request into a string
- Scanner scanner = new Scanner(request.getInputStream());
- StringBuilder sb = new StringBuilder();
- while (scanner.hasNextLine()) {
- sb.append(scanner.nextLine());
- }
- response.getOutputStream().println("This is servlet response");
- }
- public void doPost(HttpServletRequest request,
- HttpServletResponse response) throws ServletException,
- IOException {
- DataInputStream in =
- new DataInputStream((InputStream)request.getInputStream());
- String text = in.readUTF();
- String message;
- try {
- message = "100 ok";
- } catch (Throwable t) {
- message = "200 " + t.toString();
- }
- response.setContentType("text/plain");
- response.setContentLength(message.length());
- PrintWriter out = response.getWriter();
- out.println(message);
- in.close();
- out.close();
- out.flush();
- }