JavaEE-Servlet的使用

目录

  • 显示 HttpServletRequest 的常用属性
  • 获取GET请求中的query string 的内容
  • 获取 POST 请求中 body 的内容
  • 实现自动刷新
  • 构造重定向页面

显示 HttpServletRequest 的常用属性

能够返回一个页面, 显示以下内容:

  • URL
  • 方法
  • 版本号
  • query string
  • context path
  • 所有的 header
  • 
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.util.Enumeration;
    
    @WebServlet("/ShowRequest")
    public class ShowRequest extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            StringBuilder result = new StringBuilder();
            result.append(req.getProtocol());
            result.append("
    "
    ); result.append(req.getMethod()); result.append("
    "
    ); result.append(req.getRequestURI()); result.append("
    "
    ); result.append(req.getQueryString()); result.append("
    "
    ); result.append(req.getContextPath()); result.append("
    "
    ); result.append("==================
    "
    ); Enumeration<String> headerNames = req.getHeaderNames(); while (headerNames.hasMoreElements()) { String headerName = headerNames.nextElement(); String headerValue = req.getHeader(headerName); result.append(headerNames + ":" + headerValue + "
    "
    ); } resp.setContentType("text/html;charset=utf8"); resp.getWriter().write(result.toString()); } }

    JavaEE-Servlet的使用_第1张图片

    获取GET请求中的query string 的内容

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @WebServlet("/message")
    public class GetStirng extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String aa = req.getParameter("aa");
            String bb = req.getParameter("bb");
            resp.getWriter().write("aa=" + aa + ",bb=" + bb);
        }
    }
    

    JavaEE-Servlet的使用_第2张图片

    获取 POST 请求中 body 的内容

    分别实现这两种 body 格式的处理:

    1. aa=10&bb=20
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @WebServlet("/message")
    public class GetStirng extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String aa = req.getParameter("aa");
            String bb = req.getParameter("bb");
            resp.getWriter().write("aa=" + aa + ",bb=" + bb);
        }
    }
    
    

    JavaEE-Servlet的使用_第3张图片

    1. { “aa”: 10, “bb”:20}
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @WebServlet("/message")
    public class GetStirng extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String aa = req.getParameter("aa");
            String bb = req.getParameter("bb");
            resp.getWriter().write("aa=" + aa + ",bb=" + bb);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            // 前端通过body,以form表单格式,把aa和bb传到服务器
            String aa = req.getParameter("aa");
            String bb = req.getParameter("bb");
            resp.getWriter().write("aa=" + aa + ",bb=" + bb);
        }
    }
    
    

    JavaEE-Servlet的使用_第4张图片

    实现自动刷新

    1. 实现 Servlet, 设置 header, 实现每隔 1s 自动刷新页面的效果. 页面上要显示当前时间戳
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @WebServlet("/servletResponse")
    public class ServletResponse extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setContentType("text/html;charset=utf8");
            resp.setStatus(200);
            resp.setHeader("Refresh", "3");
            resp.getWriter().write("timeStamp:" + System.currentTimeMillis());
        }
    }
    
    

    JavaEE-Servlet的使用_第5张图片

    1. 网上搜索 SimpleDateFormat 类的用法, 让页面上能够显示格式化的事前. 例如 2022-01-19 12:00:00
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.text.DateFormat;
    import java.util.Date;
    
    @WebServlet("/servletResponse")
    public class ServletResponse extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setContentType("text/html;charset=utf8");
            resp.setStatus(200);
            resp.setHeader("Refresh", "3");
            resp.getWriter().write("timeStamp:" + System.currentTimeMillis());
    
            DateFormat df1 = null;
            DateFormat df2 = null;
            df1 = DateFormat.getDateInstance();
            df2 = DateFormat.getDateTimeInstance();
    //        System.out.println("DATE:" + df1.format(new Date()));
    //        System.out.println("DATETIME:" + df2.format(new Date()));
    
            resp.getWriter().write("
    "
    ); resp.getWriter().write("DATE:" + df1.format(new Date())); resp.getWriter().write("
    "
    ); resp.getWriter().write("DATE:" + df2.format(new Date())); } }

    JavaEE-Servlet的使用_第6张图片

    构造重定向页面

    实现访问 /redirect 路径后, 自动重定向到 搜狗主页 的代码.

    package com.example.test_20230602;
    
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @WebServlet("/redirectServlet")
    public class RedirectServlet extends HelloServlet{
        @Override
        public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
            resp.setStatus(302);
            resp.setHeader("Location","https://sogou.com");
        }
    }
    
    

    JavaEE-Servlet的使用_第7张图片

你可能感兴趣的:(JavaEE,servlet,java-ee,java)