好了,一个办法做到这一点(我猜)是发送从你的链接的HTTP GET请求,这样
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<a href="./MyJspAction?targetUrl=page.html">click here</a>
</body>
</html>
这个JSP将请求发送到一个servlet
package mypackage;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MyJspAction
*/
@WebServlet("/MyJspAction")
public class MyJspAction extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String targetUrl = request.getParameter("targetUrl");
new RedirectManager().doRedirect(request,response,targetUrl);
}
}
那么servlet调用你的RedirectManager对象
package mypackage;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RedirectManager {
public void doRedirect(HttpServletRequest request,
HttpServletResponse response, String targetUrl) throws ServletException, IOException {
request.getRequestDispatcher(targetUrl).forward(request, response);
}
}
发送请求到page.html
<html>
Hello
</html>
请注意forward and redirect are different things。
来源
2014-10-03 11:39:29
Leo