JsonServlet.java 680 B

123456789101112131415161718192021222324
  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. * JSON Encoding Test
  9. */
  10. @SuppressWarnings("serial")
  11. public class JsonServlet extends HttpServlet {
  12. @Override
  13. protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
  14. IOException {
  15. // Set content type to JSON
  16. res.setHeader(Common.HEADER_CONTENT_TYPE, Common.CONTENT_TYPE_JSON);
  17. // Write JSON encoded message to the response.
  18. Common.MAPPER.writeValue(res.getOutputStream(), new Common.HelloMessage());
  19. }
  20. }