通过服务器的404文件没有找到的错误处理,实现url rewrite的功能

例如:http://www.java2000.net/f29
自动转发到
http://www.java2000.net/forumdisplay.jsp?fid=29

因为系统并不存在f29,所以使用了404的错误处理页面来实现这个功能
页面代码如下
1. <%@ page language="java" contentType="text/html; charset=GBK" isErrorPage="true" pageEncoding="GBK"%>

2. <%

3. response.setStatus(HttpServletResponse.SC_OK);

4. String key = (String) request.getAttribute("javax.servlet.forward.servlet_path");

5. if (key != null) {

6. int index = key.lastIndexOf("/");

7. if (index != -1) {

8. key = key.substring(index + 1);

9. if (key.startsWith("f") || key.startsWith("p")) {

10. try {

11. long id = Long.parseLong(key.substring(1));

12. String url = key.startsWith("f") ? "forumdisplay.jsp?fid=" : "viewthread.jsp?tid=";

13. %>

14. <jsp:forward page="<%=url+id%>"></jsp:forward>

15. <%

16. return;

17. } catch (Exception ex) {}

18. }

19. %>

20. <jsp:forward page="<%=response.encodeURL("search.jsp")%>"><jsp:param name="keyword" value="<%=key %>" /></jsp:forward>

21. <%

22. return;

23. }

24. }

25. %>

26. 对不起,您请求的页面没有找到!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值