您的位置:首页 > Web前端 > JavaScript

Html/Jsp常用的页面跳转方法

2009-12-30 17:30 429 查看
第一种:
<script language="javascript" type="text/javascript">
window.location.href="login.jsp?backurl="+window.location.href;
</script>

第二种:
<script language="javascript">
alert("返回");
window.history.back(-1); 
</script>

第三种: 
<script language="javascript">
window.navigate("top.jsp"); 
</script>

第四种: 
<script language="JavaScript">
self.location="top.htm"; 
</script>

第五种: 
<script language="javascript">
alert("非法访问!");
top.location="xx.jsp";
</script>

第六种: 

servlet实现跳转-->request.getRequestDispatcher("/WEB-INF/pages/error.jsp").forward(request,response);

第七种: 

由于以上的跳转都是webRoot下的,或者已知完整url地址的跳转.如果html或者jsp是存放在WEB-INF下的,在使用struts的情况下,可以通过配置struts的路径获得完整的url地址,不使用struts实现WEB-INF下的页面跳转...

/**
* @author Vicky
* @emial eclipser@163.com
*/
public class NavigateTo extends HttpServlet {

private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
String url = request.getParameter("url");
request.getRequestDispatcher(url).forward(request,response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
doGet(request, response);
}

}


web.xml配置:

<servlet>
<servlet-name>NavigateTo</servlet-name>
<servlet-class>cn.vicky.servlet.NavigateTo</servlet-class>
</servlet>

使用的页面:

<%
String path = request.getContextPath();

%>

<script type="text/javascript">
function playGame()
{
window.location.href = "<%=path%>/servlet/NavigateTo?url=/WEB-INF/pages/game.jsp";
}
</script>

<input type="button" value="进入游戏" ${sessionScope.user == null ? "disabled='false'" : ""} onclick="playGame();"/>

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: