在Web中可以使用<jsp:forward>指令,将一个用户的请求(request)从一个页面传递到另一个页面,即完成跳转的操作。
1、调整前页:tiaozhuan_a.jsp
代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("UTF-8"); //这句是关键所在可以避免中文乱码 %> <html> <head> <title>My JSP 'tiaozhuan_a.jsp' starting page</title> </head> <body> <% String username = "wangyan"; %> <jsp:forward page="tiaozhuan_b.jsp"> <jsp:param name="name" value="<%=username%>" /> <jsp:param name="inf" value="我是传过来的inf" /> </jsp:forward> </body> </html>
2、调整后页:tiaozhuan_b.jsp
代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("UTF-8"); //这句是关键所在 %> <html> <head> <title>My JSP 'tiaozhuan_b.jsp' starting page</title> </head> <body> 参数一:<%=request.getParameter("name")%> 参数二:<%=request.getParameter("inf")%> </body> </html>
3、最终效果,地址栏输入:http://localhost:8080/JSP/tiaozhuan_a.jsp后,显示直接跳转到tiaozhuan_b.jsp,但是页面还是停留在a页面: