@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>