美文网首页
简单servlet

简单servlet

作者: liut_2016 | 来源:发表于2016-11-18 10:58 被阅读8次

    一个简单的servlet,用来测试GET,POST请求

    package xyz.liut;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @WebServlet("/servlet") // 请求接口
    public class Test extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            // 设置编码和content-type
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/json");
            // response.setContentType("application/json;charset=utf-8");
    
            // 获取参数
            String param0 = request.getParameter("param0");
            String param1 = request.getParameter("param1");
    
            // 输出
            PrintWriter pw = response.getWriter();
            pw.println("req by " + request.getMethod());
            pw.println("req_ContentType " + request.getContentType());
            pw.println("resp_ContentType " + response.getContentType());
            pw.println();
            pw.println("param0 = " + param0);
            pw.println("param1 = " + param1);
            pw.close();
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doGet(request, response);
        }
    
    }
    
    
    

    相关文章

      网友评论

          本文标题:简单servlet

          本文链接:https://www.haomeiwen.com/subject/ropjpttx.html