区分request请求时是forward还是include

 我来答
就烦条0o
2017-05-21 · 知道合伙人软件行家
就烦条0o
知道合伙人软件行家
采纳数:33315 获赞数:46483
从事多年系统运维,喜欢编写各种小程序和脚本。

向TA提问 私信TA
展开全部
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
}
}
}
Storm代理
2023-08-29 广告
"StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、IP+端口提取形式,不限带宽,I... 点击进入详情页
本回答由Storm代理提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式