import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//重定向 302 与localhost 响应头(服务-》客户端)
public class Demo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
////服务端通知客户端重定向
response.setStatus(302);
//服务端通知客户端重定向的目标资源
response.setHeader("location", "WEB-INF/my.html");
}
}
新建 my.html
<!DOCTYPE html>
<html>
<head>
<title>my.html</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
localhost+302
</body>
</html>
访问
输入地址:
http://localhost:8080/day04/Demo
结果成如下
原文地址:http://blog.51cto.com/357712148/2104327