Servlet视角

在这里插入图片描述


@WebServlet("/info")
public class RequestInfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        {
            String method = req.getMethod();
            String message = "方法" + method;
            resp.getWriter().printf("<p>%s</p>",message);
            System.out.println(message);
        }

        {
            String path = req.getRequestURI();
            String message = "路径" + path;
            resp.getWriter().printf("<p>%s</p>",message);
            System.out.println(message);
        }

        {
            String version = req.getProtocol();
            String message = "版本" + version;
            resp.getWriter().printf("<p>%s</p>",message);
            System.out.println(message);
        }

        {
            resp.getWriter().println("所有的请求头信息");
            System.out.println("打印所有请求头信息");
            Enumeration<String> headerNames = req.getHeaderNames();
            while (headerNames.hasMoreElements()){
                String key = headerNames.nextElement();
                String value = req.getHeader(key);
                resp.getWriter().println("<p>" + key + " : " + value + "</p>");

            }
        }

        {
            InputStream is = req.getInputStream();
            resp.getWriter().println("<p>" + "请求体被封装成的输入流" + "</p>");
        }
    }
}

在这里插入图片描述


在这里插入图片描述

		{
            String schema = req.getScheme();
            resp.getWriter().println("<p>" + "url上的 schema" + schema + "</p>");
        }

        {
            String host = req.getRemoteHost();
            resp.getWriter().println("<p>" + "url上的 host" + host + "</p>");
        }

        {
            int localPort = req.getLocalPort();
            resp.getWriter().println("<p>" + "url上的 localPort" + localPort + "</p>");
            int remotePort = req.getRemotePort();
            resp.getWriter().println("<p>" + "url上的 remotePort" + remotePort+ "</p>");
            int serverPort = req.getServerPort();
            resp.getWriter().println("<p>" + "url上的 serverPort" + serverPort+ "</p>");
        }
    }

在这里插入图片描述



@WebServlet("/status")
public class StatuServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int status = Integer.parseInt(req.getParameter("status"));
        resp.setStatus(status);

        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().println("<h1>" + status + "</h1>");


    }
}

在这里插入图片描述
在这里插入图片描述


307重定向

@WebServlet("/307")
public class Redirect307 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        redirect(resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        redirect(resp);
    }

    private void redirect(HttpServletResponse resp) {
        resp.setStatus(307);
        resp.setHeader("Location","/2020/show-method");

    }
}

*****分隔符
@WebServlet("/show-method")
public class ShowMethodServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        printMethod("GET",resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        printMethod("POST",resp);
    }

    private void printMethod(String method,HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().println("<h1>" + method + "</h1>");


    }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Welcome</title>
</head>
<body>
    <h1>欢迎</h1>
    <form method="post" action="show-method">
        <button type="submit">提交Post访问show-method</button>
    </form>
    <form method="post" action="307">
        <button type="submit">提交Post访问307</button>
    </form>
</body>
</html>

对比301 重定向


@WebServlet("/301")
public class Redirect301 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        redirect(resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        redirect(resp);
    }

    private void redirect(HttpServletResponse resp) {
        resp.setStatus(301);
        resp.setHeader("Location","show-method");

    }
}

@WebServlet("/show-method")
public class ShowMethodServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        printMethod("GET",resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        printMethod("POST",resp);
    }

    private void printMethod(String method,HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().println("<h1>" + method + "</h1>");


    }
}
<h1>欢迎</h1>
    <form method="post" action="show-method">
        <button type="submit">提交Post访问show-method</button>
    </form>
    <form method="post" action="307">
        <button type="submit">提交Post访问307</button>
    </form>
    <form method="post" action="301">
        <button type="submit">提交Post访问301</button>
    </form>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值