找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 8265|回复: 2

区分request请求时是forward还是include

[复制链接]
发表于 2009-11-25 02:06:43 | 显示全部楼层 |阅读模式 IP:江苏扬州
package com.ibm.crl.ias.cif.web;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Hashtable;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import com.ibm.crl.ias.cif.web.action.*;
import com.ibm.crl.ias.util.IASServletUtils;
import com.ibm.crl.ias.util.WrappedRuntimeException;
/**
* @author Walter Hu
*/
public class ActionDispatchServlet extends HttpServlet {

public static final Logger     logger = Logger.getLogger(ActionDispatchServlet.class);

private static final Map<String, IAction> action_map = new Hashtable<String, IAction>();

static {
action_map.put("signin", new ActionSignin());
// action_map.put("externalView", new ActionExternalView());
action_map.put("externalCategoryView", new ActionCategoryView());
action_map.put("externalCategoryManage", new ActionCategoryManage());
action_map.put("externalFolderView", new ActionFolderView());
action_map.put("externalFolderManage", new ActionFolderManage());
action_map.put("externalSearch", new ActionSearch());
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
   IOException {
dispatchAction(request, response);
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
dispatchAction(request, response);
}

private void dispatchAction(HttpServletRequest request, HttpServletResponse response) {
try {
   request.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e1) {
   
   e1.printStackTrace(); // Auto-generated catch block
}

String viewName = null;

    //得到请求的URL,include的URI就是include_rui属性
String requestURI = IASServletUtils.getRequestAttribute(request, "javax.servlet.include.request_uri") ;

boolean hasIncludeUri = requestURI != null;

if (requestURI == null)
   requestURI = request.getRequestURI();



IAction action = null;

try {
   if (requestURI.indexOf(".action") != -1) {
   
    int index = requestURI.indexOf(".action");
    String actionName = requestURI.substring(0, index);
    index = actionName.lastIndexOf('/');
    actionName = actionName.substring(index + 1);
   
    action = action_map.get(actionName);
   
    if (action == null)
     throw new IllegalArgumentException("incorrect action name: " + actionName);
   
    viewName = action.doAction(request, response);
   } else {
    throw new WrappedRuntimeException("Incorrect action url:" + requestURI);
   }
} catch (Exception e) {
   request.setAttribute(WebConstants.REQ_EXCEPTION, e);
   
   if (action != null && action instanceof IAjaxAction)
    viewName = WebConstants.VIEW_AJAX_ERROR;
   else
    viewName = "/error.jsp";
   
   logger.error("controller internal error", e);
}

try {   
      
   if (!response.isCommitted() && !hasIncludeUri)
    this.getServletContext().getRequestDispatcher(viewName).forward(request, response);
   else
    this.getServletContext().getRequestDispatcher(viewName).include(request, response);
  
} catch (ServletException e) {
   e.printStackTrace(); //Auto-generated exception handling
} catch (IOException e) {
   e.printStackTrace(); //Auto-generated exception handling
}
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xxonline8888/archive/2008/04/07/2257633.aspx
发表于 2010-2-8 08:05:05 | 显示全部楼层 IP:柬埔寨
@,@..是什么意思呀?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-10-1 03:28 , Processed in 0.184585 second(s), 14 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表