Jsp利用404错误页进行URL重写

有些时候为了SEO,尽可能的吧网站的URL弄成静态的,这样有利于搜索引擎的收录。    
如果空间足够的话,可以把所有的网页生成静态的,而且打开速度也快。但如果条件不允许,又想实现那样的链接?可以通过伪静态,就是通过重新构造URL来实现URL重写    
首先定义一个404.jsp页面 配置404错误,在web.xml里面配置   

     
 404     
 404.jsp     
    
<%@ 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=";  //根据当前请求的URL分析 然后处理   
%>     
  // 跳转   
<%     
  return;     
        } catch (Exception ex) {}     
      }     
%>     
">     
<%     
  return;     
    }     
  }     
%>    

lucifer -
共有0个回答