PlaintextServlet.java 864 B

123456789101112131415161718192021222324252627282930313233343536
  1. package hello;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. /**
  8. * Plaintext rendering Test
  9. */
  10. @SuppressWarnings("serial")
  11. public class PlaintextServlet extends HttpServlet {
  12. private static final String MESSAGE = "Hello, World!";
  13. private static final byte[] buffer;
  14. static {
  15. try {
  16. buffer = MESSAGE.getBytes("US-ASCII");
  17. } catch (Exception e) {
  18. throw new RuntimeException(e);
  19. }
  20. }
  21. @Override
  22. protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
  23. IOException {
  24. // Set content type to text/plain.
  25. res.setContentType(Common.CONTENT_TYPE_TEXT);
  26. // Write plaintext "Hello, World!" to the response.
  27. res.getOutputStream().write(buffer);
  28. }
  29. }