如何实现jsp本页面跳转
1、Jsp页面跳转的第一种方式:提交表单代码如下: 争犸禀淫<!--第一种跳转方式:提交表单--> <form name="form" 罪焐芡拂method="post" action="page2.jsp"> <input type="submit" value="跳转1"> </form>如图:
2、Jsp页面跳转的第二种方式:Javascript实现代艨位雅剖码如下:js部分: <script type="text/javascript"> function next(){ window.location = "page2.jsp"; } </script>html部分: <!--第二种跳转方式:通过js--> <input type="button" value="跳转2" onclick="next()">如图:
3、Jsp页面跳转的第三种方式:转发转发分两种方法:1.jsp自带的forword标签来实现跳转代码如下: <jsp:forward page="page2.jsp" />如图:
4、2.jsp中写java代码实现跳转代码如下:<% request.getRequestDispatcher("page2.jsp").forward(request, response);%>如图:
5、Jsp页面跳转的第四种方式:重定向重定向分两种方法:1.response.sendRedirect()实现代码如下: <!--第四种跳转方式:重定向--> <% //1. response.sendRedirect("page2.jsp"); %>
6、2.response.setHeader()实现代码如下: <!--第四种跳转方式:重定向--> <% //2. response.setHeader("Refresh", "1;url=page2.jsp"); %>
7、这四种方式的所有代码私网褡爸整理如下:<%@page contentType="text/html媪青怍牙" pageEncoding="UTF-8"%><!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>页面一</title> <script type="text/javascript"> function next(){ window.location = "page2.jsp"; } </script> </head> <body> <!--第一种跳转方式:提交表单--> <form name="form" method="post" action="page2.jsp"> <input type="submit" value="跳转1"> </form> <!--第二种跳转方式:通过js--> <input type="button" value="跳转2" onclick="next()"> <!--第三种跳转方式:转发--> <%--<jsp:forward page="page2.jsp" />--%> <% // request.getRequestDispatcher("page2.jsp").forward(request, response);%> <!--第四种跳转方式:重定向--> <% //1. response.sendRedirect("page2.jsp"); //2. response.setHeader("Refresh", "1;url=page2.jsp"); %> </body></html>如图: