JsonServlet.java 770 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package hello;
  2. import java.io.*;
  3. import javax.servlet.*;
  4. import javax.servlet.http.*;
  5. /**
  6. * JSON Encoding Test
  7. */
  8. @SuppressWarnings("serial")
  9. public class JsonServlet extends HttpServlet
  10. {
  11. // Response message class.
  12. public static class HelloMessage {
  13. public final String message = "Hello, World!";
  14. }
  15. @Override
  16. protected void doGet(HttpServletRequest req, HttpServletResponse res)
  17. throws ServletException, IOException
  18. {
  19. // Set content type to JSON
  20. res.setHeader(Common.HEADER_CONTENT_TYPE, Common.CONTENT_TYPE_JSON);
  21. // Write JSON encoded message to the response.
  22. try
  23. {
  24. Common.MAPPER.writeValue(res.getOutputStream(), new HelloMessage());
  25. }
  26. catch (IOException ioe)
  27. {
  28. // do nothing
  29. }
  30. }
  31. }