HttpDemoServlet.java 897 B

123456789101112131415161718192021222324
  1. package com.lovecoding.servlet;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.http.HttpServlet;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import java.io.IOException;
  7. public class HttpDemoServlet extends HttpServlet{
  8. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  9. resp.addHeader("Content-Type","text/html;charset=utf-8");
  10. resp.setCharacterEncoding("UTF-8");
  11. resp.getWriter().print("<html> <body><h1> 你好 HttpServlet </h1></body> </html>");
  12. }
  13. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  14. resp.addHeader("Content-Type","text/html;charset=utf-8");
  15. resp.setCharacterEncoding("UTF-8");
  16. resp.getWriter().print("你好 HttpServlet");
  17. }
  18. }