通過服務器的404文件沒有找到的錯誤處理,實現url rewrite的功能

本站提供的功能如下
http://www.java2000.net/f29
會被自動轉發到
http://www.java2000.net/forumdisplay.jsp?fid=29

因爲系統並不存在f29,所以使用了404的錯誤處理頁面來實現這個功能
頁面代碼如下

<%@ page language="java" contentType="text/html; charset=GBK" isErrorPage="true" pageEncoding="GBK"%>
<%
  response.setStatus(HttpServletResponse.SC_OK);
  String key = (String) request.getAttribute("javax.servlet.forward.servlet_path");
  if (key != null) {
    int index = key.lastIndexOf("/");
    if (index != -1) {
      key = key.substring(index + 1);
      if (key.startsWith("f") || key.startsWith("p")) {
        try {
          long id = Long.parseLong(key.substring(1));
          String url = key.startsWith("f") ? "forumdisplay.jsp?fid=" : "viewthread.jsp?tid=";
%>
<jsp:forward page="<%=url+id%>"></jsp:forward>
<%
  return;
        } catch (Exception ex) {}
      }
%>
<jsp:forward page="<%=response.encodeURL("search.jsp")%>"><jsp:param name="keyword" value="<%=key %>" /></jsp:forward>
<%
  return;
    }
  }
%>
對不起,您請求的頁面沒有找到!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章