页面跳转的两种方式:转发和重定向

小鱼儿 2021-12-11 11:53 395阅读 0赞

理论参考https://blog.csdn.net/xybelieve1990/article/details/49486751这篇文章

1 Servlet、location和状态吗302一起完成重定向

  1. import java.io.IOException;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.http.HttpServlet;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. /**
  7. * 和location和302一起完成重定向
  8. * @author Administrator
  9. *
  10. */
  11. public class ServletDmo1 extends HttpServlet {
  12. public void doGet(HttpServletRequest request, HttpServletResponse response)
  13. throws ServletException, IOException {
  14. // 向页面输出内容
  15. response.setContentType("text/html;charset=UTF-8");
  16. // response.getWriter().write("向班长借钱...");
  17. // 班长没钱
  18. response.setStatus(302);
  19. // 班长告诉我副班长的地址(重定向都是客户端的,带项目名)
  20. response.setHeader("location", "/day09/1.html");
  21. }
  22. public void doPost(HttpServletRequest request, HttpServletResponse response)
  23. throws ServletException, IOException {
  24. doGet(request, response);
  25. }
  26. }

项目day09/1.html

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <h3>1.html的相对路径</h3>
  9. <a href="./demo5">demo5</a>
  10. <a href="demo5">demo5</a>
  11. <h3>1.html的绝对路径</h3>
  12. <a href="http://localhost/day09/demo5">demo5</a>
  13. <a href="/day09/demo5">demo5</a>
  14. </body>
  15. </html>

demo5是通过配web.xml置文件与反射技术跳转到ServletDemo5

  1. import java.io.IOException;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.http.HttpServlet;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. /**
  7. * 配置servlet启动时加载
  8. * @author Administrator
  9. *
  10. */
  11. public class ServletDemo5 extends HttpServlet {
  12. /**
  13. * 默认的情况下第一次访问的时候init被调用。
  14. *
  15. */
  16. public void init() throws ServletException {
  17. System.out.println("init...");
  18. // 初始化数据库的链接
  19. }
  20. public void doGet(HttpServletRequest request, HttpServletResponse response)
  21. throws ServletException, IOException {
  22. // 写的内容
  23. // 获取表单输入的内容
  24. // 自己逻辑,通过名称查询数据库,把张三的姓名查到了
  25. // 把张三返回浏览器,
  26. //向控制台输出结果
  27. System.out.println("doGet...");
  28. // 向页面输出内容
  29. response.getWriter().write("hello demo5...");
  30. }
  31. public void doPost(HttpServletRequest request, HttpServletResponse response)
  32. throws ServletException, IOException {
  33. doGet(request,response);
  34. }
  35. }

2 页面的定时跳转

  1. import java.io.IOException;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.http.HttpServlet;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. /**
  7. * 页面定时跳转
  8. * @author Administrator
  9. *
  10. */
  11. public class RefreshServlet extends HttpServlet {
  12. public void doGet(HttpServletRequest request, HttpServletResponse response)
  13. throws ServletException, IOException {
  14. response.setContentType("text/html;charset=UTF-8");
  15. response.getWriter().write("访问到了...");
  16. // 页面5秒会跳转到ServletDmo1
  17. response.setHeader("refresh", "5;url=/day09/1.html");
  18. }
  19. public void doPost(HttpServletRequest request, HttpServletResponse response)
  20. throws ServletException, IOException {
  21. doGet(request, response);
  22. }
  23. }

转载于:https://www.cnblogs.com/zsj03180204/p/11045532.html

发表评论

表情:
评论列表 (有 0 条评论,395人围观)

还没有评论,来说两句吧...

相关阅读