当前位置: 首页>>代码示例>>Java>>正文


Java HttpServletRequest.getContextPath方法代码示例

本文整理汇总了Java中javax.servlet.http.HttpServletRequest.getContextPath方法的典型用法代码示例。如果您正苦于以下问题:Java HttpServletRequest.getContextPath方法的具体用法?Java HttpServletRequest.getContextPath怎么用?Java HttpServletRequest.getContextPath使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.servlet.http.HttpServletRequest的用法示例。


在下文中一共展示了HttpServletRequest.getContextPath方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: accept

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
private void accept(HttpServletRequest req, HttpServletResponse resp) {
    try {
        String localAddr = req.getLocalAddr();
        Properties properties = EngineFactory.getPropeties();
        if (properties.getProperty("hostname") != null) {
            localAddr = properties.getProperty("hostname");
        }
        String path = "http://" + localAddr + ":" + req.getLocalPort() + req.getContextPath();
        InputStream is = getClass().getResourceAsStream(
                "/com/ramussoft/jnlp/ramus-http-client.jnlp");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int r;
        while ((r = is.read()) >= 0)
            out.write(r);
        String string = MessageFormat.format(new String(out.toByteArray(),
                "UTF8"), path);
        resp.setContentType("application/x-java-jnlp-file");
        OutputStream o = resp.getOutputStream();
        o.write(string.getBytes("UTF8"));
        o.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
 
开发者ID:Vitaliy-Yakovchuk,项目名称:ramus,代码行数:26,代码来源:JNLPHTTPServlet.java

示例2: setRequest

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
/**
 * Set the request that we are wrapping.
 *
 * @param request The new wrapped request
 */
void setRequest(HttpServletRequest request) {

    super.setRequest(request);

    // Initialize the attributes for this request
    dispatcherType = request.getAttribute(Globals.DISPATCHER_TYPE_ATTR);
    requestDispatcherPath = 
        request.getAttribute(Globals.DISPATCHER_REQUEST_PATH_ATTR);

    // Initialize the path elements for this request
    contextPath = request.getContextPath();
    pathInfo = request.getPathInfo();
    queryString = request.getQueryString();
    requestURI = request.getRequestURI();
    servletPath = request.getServletPath();

}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:23,代码来源:ApplicationHttpRequest.java

示例3: ZNodeResource

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
public ZNodeResource(@DefaultValue("") @QueryParam("session") String session,
        @Context UriInfo ui,
        @Context HttpServletRequest request
        )
        throws IOException {

    String contextPath = request.getContextPath();
    if (contextPath.equals("")) {
        contextPath = "/";
    }
    if (session.equals("")) {
        session = null;
    } else if (!ZooKeeperService.isConnected(contextPath, session)) {
        throw new WebApplicationException(Response.status(
                Response.Status.UNAUTHORIZED).build());
    }
    zk = ZooKeeperService.getClient(contextPath, session);
}
 
开发者ID:didichuxing2,项目名称:https-github.com-apache-zookeeper,代码行数:19,代码来源:ZNodeResource.java

示例4: getUrl

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
private String getUrl(HttpServletRequest request){
	String contextPath=request.getContextPath();
	String uri = request.getRequestURI();
	int pathParamIndex = uri.indexOf(';');
	if (pathParamIndex > 0) {
		// strip everything from the first semi-colon
		uri = uri.substring(0, pathParamIndex);
	}
	int queryParamIndex = uri.indexOf('?');
	if (queryParamIndex > 0) {
		// strip everything from the first question mark
		uri = uri.substring(0, queryParamIndex);
	}
	if(contextPath.length()>1){
		uri=uri.substring(contextPath.length(),uri.length());
	}
	return uri;
}
 
开发者ID:bsteker,项目名称:bdf2,代码行数:19,代码来源:ControllerFilter.java

示例5: getDomain

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
/**
 * 根据 request获取完整的域名,如http://www.abc.com:8080
 * 如果端口为80则为:http://www.abc.com
 * @return
 */
public static String getDomain(){
	HttpServletRequest request  = ThreadContextHolder.getHttpRequest();
	if(request==null) return "";
	String port  = ""+request.getServerPort();
	if(port.equals("80")){
		port="";
	}else{
		port =":"+ port;
	}
	
	String contextPath = request.getContextPath();
	if(contextPath.equals("/")){
		contextPath="";
	}
	
	String domain ="http://"+ request.getServerName()+port;
	domain+=contextPath;
	return domain;
}
 
开发者ID:yulele166,项目名称:pub-service,代码行数:25,代码来源:RequestUtil.java

示例6: setSessionCookie

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
public static void setSessionCookie(HttpServletRequest request, HttpServletResponse response, String name, String value) {
    if (value == null){
        value = StringUtils.EMPTY;
    }
    String path = request.getContextPath() != null ? request.getContextPath() : "/";
    if ("".equals(path)){
        path = "/";
    }
    Cookie cookie = new Cookie(name, value);
    cookie.setPath(path);
    cookie.setVersion(1);
    addSecureParam(request, cookie);
    response.addCookie(cookie);
}
 
开发者ID:bridgeli,项目名称:netty-socketio-demo,代码行数:15,代码来源:CookieUtils.java

示例7: doFilter

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
@Override
public void doFilter(ServletRequest req, ServletResponse resp,	FilterChain chain) throws IOException, ServletException {
	HttpServletRequest request = (HttpServletRequest)req;
	HttpServletResponse response = (HttpServletResponse)resp;	
	
	String path = request.getContextPath() + request.getServletPath() + request.getPathInfo();

	
	if (path.endsWith(LOGIN_PATH) || path.endsWith(LOGOUT_PATH) || path.endsWith(LOADDB_PATH)) {
		// if logging in, logging out, or loading the database, let the request flow
		chain.doFilter(req, resp);
		return;
	}
	
	Cookie cookies[] = request.getCookies();
	Cookie sessionCookie = null;

	if (cookies != null) {
		for (Cookie c : cookies) {
			if (c.getName().equals(LoginREST.SESSIONID_COOKIE_NAME)) {
				sessionCookie = c;
			}
			if (sessionCookie!=null)
				break; 
		}
		String sessionId = "";
		if (sessionCookie!=null) // We need both cookie to work
			sessionId= sessionCookie.getValue().trim();
		// did this check as the logout currently sets the cookie value to "" instead of aging it out
		// see comment in LogingREST.java
		if (sessionId.equals("")) {
			response.sendError(HttpServletResponse.SC_FORBIDDEN);
			return;
		}
		
		JSONObject jsonObject = authService.validateSession(sessionId);
		if (jsonObject != null) {
			String loginUser=(String) jsonObject.get("customerid");				
			request.setAttribute(LOGIN_USER, loginUser);
			chain.doFilter(req, resp);
			return;
		} else {
			response.sendError(HttpServletResponse.SC_FORBIDDEN);
			return;
		}
	}
	
	// if we got here, we didn't detect the session cookie, so we need to return 404
	response.sendError(HttpServletResponse.SC_FORBIDDEN);
}
 
开发者ID:ibmruntimes,项目名称:acmeair-modular,代码行数:51,代码来源:RESTCookieSessionFilter.java

示例8: extractContextPath

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
private String extractContextPath(HttpServletRequest request) {
  Handler[] handlers = this.getHandlers();
  String contextPath = request.getContextPath();
  for (Handler handler : handlers) {
    if (handler instanceof ServletContextHandler) {
      contextPath = ((ServletContextHandler) handler).getContextPath();
      break;
    }
  }

  if (contextPath == null || contextPath.length() == 1) {
    contextPath = "";
  }
  return contextPath;
}
 
开发者ID:ApptuitAI,项目名称:JInsight,代码行数:16,代码来源:JettyRuleHelper.java

示例9: service

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
/**
 * 复写父类service方法,进行请求转发
 *
 * @param request  请求
 * @param response 应答
 * @throws ServletException 异常
 * @throws IOException      异常
 */
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // 项目根路径
    String contextPath = request.getContextPath();
    // servlet访问路径
    String servletPath = request.getServletPath();
    // 访问全路径
    String requestURI = request.getRequestURI();
    // 设置编码
    response.setCharacterEncoding("utf-8");
    // 根目录为空的情况为,root context
    if (contextPath == null) {
        contextPath = "";
    }
    String uri = contextPath + servletPath;
    String path = requestURI.substring(contextPath.length() + servletPath.length());
    // request是否合法
    if (!isPermittedRequest(request, response)) {
        return;
    }
    //
    if ("/submitLogin".equals(path)) {
        response.getWriter().print(this.submitLogin(path, request, response));
        return;
    }
    if ("/submitLogout".equals(path)) {
        response.getWriter().print(this.submitLogout(path, request, response));
        return;
    }
    // 没有权限直接返回
    if (!hasPermission(path, request, response)) {
        return;
    }

    if ("".equals(path)) {
        if (contextPath.equals("") || contextPath.equals("/")) {
            response.sendRedirect(servletPath + "/index.html");
        } else {
            response.sendRedirect(requestURI + "/index.html");
        }
        return;
    }
    if ("/".equals(path)) {
        response.sendRedirect("index.html");
        return;
    }

    if (path.startsWith(servicePathPrefix)) {
        LOG.debug("path:" + path);
        response.getWriter().print(this.process(path, request, response));
        return;
    }
    // 其他
    this.returnResourceFile(path, uri, response);
}
 
开发者ID:quartzweb,项目名称:quartz-web,代码行数:63,代码来源:ResourceServlet.java

示例10: doHandleRequest

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
@Override
protected ModelAndView doHandleRequest(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
	Properties properties=new Properties();
       properties.setProperty("resource.loader", "file");
       properties.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
       String contextPath=request.getContextPath();
       if(!contextPath.endsWith("/")){
       	contextPath+="/";
       }
       VelocityEngine velocityEngine=new VelocityEngine(properties);
       VelocityContext context=new VelocityContext();
       StringBuffer sb=new StringBuffer();
       sb.append("\r");
       sb.append("<link rel=\"stylesheet\" type=\"text/css\" href=\""+contextPath+"dorado/res/dorado/resources/jquery.contextMenu.css\" />");
       sb.append("\r");
       sb.append("<link rel=\"stylesheet\" type=\"text/css\" href=\""+contextPath+"dorado/res/dorado/resources/jquery-ui-1.8.19.custom.css\" />");
       sb.append("\r");
       sb.append("<script type=\"text/javascript\" src=\""+contextPath+"dorado/res/dorado/scripts/jbpm4-designer-all-in-one.js\"></script>");
       sb.append("\r");
       
       String serverUrl=this.buildServerUrl(request.getScheme(),request.getServerName(),request.getServerPort());
       if(contextPath.endsWith("/")){
       	serverUrl+=contextPath.substring(0,contextPath.length()-1);
       }
       context.put("cssandscript", sb.toString());
       context.put("baseIconsDir", contextPath+"dorado/res/dorado/resources");
       context.put("serverUrl", serverUrl);
       StringWriter writer=new StringWriter();
       velocityEngine.mergeTemplate("dorado/resources/jbpm4-designer.html","utf-8", context, writer);
       response.setContentType("text/html; charset=utf-8");
       PrintWriter out=response.getWriter();
       out.write(writer.toString());
       out.flush();
       out.close();
	return null;
}
 
开发者ID:bsteker,项目名称:bdf2,代码行数:38,代码来源:DesignerController.java

示例11: ActionEnter

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
public ActionEnter ( HttpServletRequest request, String rootPath ) {
	
	this.request = request;
	this.rootPath = rootPath;
	this.actionType = request.getParameter( "action" );
	this.contextPath = request.getContextPath();
	this.configManager = ConfigManager.getInstance( this.rootPath, this.contextPath, request.getRequestURI() );
	
}
 
开发者ID:gingerjiang,项目名称:easycms,代码行数:10,代码来源:ActionEnter.java

示例12: buildMapping

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
/**
 * parse mapping
 *
 * @param request
 * @return
 */
private static String buildMapping(HttpServletRequest request) {
    String requestURI = request.getRequestURI();
    String contextPath = request.getContextPath();
    if (requestURI.length() <= contextPath.length() + 1) {
        logger.warn("buildMapping fail:" + requestURI);
        return null;
    }
    String mapping = requestURI.substring(contextPath.length() + 1).toLowerCase();
    return mapping;
}
 
开发者ID:xuxueli,项目名称:xxl-web,代码行数:17,代码来源:XxlWebHandlerFactory.java

示例13: deleteCookie

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
public static void deleteCookie(HttpServletRequest request, HttpServletResponse response,
        Cookie cookie) {
    if (cookie != null) {
        String path = request.getContextPath() != null ? request.getContextPath() : "/";
        if ("".equals(path)) {
            path = "/";
        }
        cookie.setPath(path);
        cookie.setValue("");
        response.addCookie(cookie);
    }
}
 
开发者ID:bridgeli,项目名称:netty-socketio-demo,代码行数:13,代码来源:CookieUtils.java

示例14: service

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //获取请求方法
    String requestMethod = req.getMethod().toLowerCase(Locale.ENGLISH);
    //请求路径url
    String url = req.getRequestURI();
    String contextPath = req.getContextPath();
    String requestPath = null;
    if (contextPath != null && contextPath.length() > 0) {
        requestPath = url.substring(contextPath.length());
    }
    //获取处理处理这个请求的handler
    Handler handler = ControllerHelper.getHandler(requestMethod, requestPath);
   // System.out.println(requestMethod + "  " + requestPath);
    if (handler != null) {
        Class<?> controllerClass = handler.getControllerClass();
        Object controllerBean = BeanContainer.getBean(controllerClass.getName());
        //解析请求参数
        Param param = ParameterUtil.createParam(req);
        Object result;//请求返回对象
        Method method = handler.getMethod();//处理请求的方法
        if (param.isEmpty()) {
            result = BeanFactory.invokeMethod(controllerBean, method);
        } else {
            result = BeanFactory.invokeMethod(controllerBean, method, param);
        }
        if (result instanceof ModelAndView) {
            handleViewResult((ModelAndView) result, req, resp);
        } else {
            handleDataResult((Data) result, resp);
        }
    }
}
 
开发者ID:CFshuming,项目名称:bfmvc,代码行数:34,代码来源:DispatherServlet.java

示例15: resolveUrl

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
/** Utility methods
 * taken from org.apache.taglibs.standard.tag.common.core.UrlSupport
 */
public static String resolveUrl(
        String url, String context, PageContext pageContext)
throws JspException {
    // don't touch absolute URLs
    if (isAbsoluteUrl(url))
        return url;
    
    // normalize relative URLs against a context root
    HttpServletRequest request =
        (HttpServletRequest) pageContext.getRequest();
    if (context == null) {
        if (url.startsWith("/"))
            return (request.getContextPath() + url);
        else
            return url;
    } else {
        if (!context.startsWith("/") || !url.startsWith("/")) {
            throw new JspTagException(
            "In URL tags, when the \"context\" attribute is specified, values of both \"context\" and \"url\" must start with \"/\".");
        }
        if (context.equals("/")) {
            // Don't produce string starting with '//', many
            // browsers interpret this as host name, not as
            // path on same host.
            return url;
        } else {
            return (context + url);
        }
    }
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:34,代码来源:Util.java


注:本文中的javax.servlet.http.HttpServletRequest.getContextPath方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。