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


Java HttpServletRequest.getServerPort方法代码示例

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


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

示例1: forwardToLogin

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
private boolean forwardToLogin(HttpServletRequest request,
                               HttpServletResponse response) throws ServletException, IOException {
    String url = "http://" + request.getServerName() + ":"
            + request.getServerPort() + request.getContextPath()
            + "/login.jsp";
    response.sendRedirect(url);
    return false;
}
 
开发者ID:tomoncle,项目名称:JavaStudy,代码行数:9,代码来源:AuthInterceptorAdapter.java

示例2: getBasePath

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
/**
 * 获取请求basePath
 * @param request
 * @return
 */
public static String getBasePath(HttpServletRequest request) {
	StringBuffer basePath = new StringBuffer();
	String scheme = request.getScheme();
	String domain = request.getServerName();
	int port = request.getServerPort();
	basePath.append(scheme);
	basePath.append("://");
	basePath.append(domain);
	if("http".equalsIgnoreCase(scheme) && 80 != port) {
		basePath.append(":").append(String.valueOf(port));
	} else if("https".equalsIgnoreCase(scheme) && port != 443) {
		basePath.append(":").append(String.valueOf(port));
	}
	return basePath.toString();
}
 
开发者ID:youngMen1,项目名称:-Spring-SpringMVC-Mybatis-,代码行数:21,代码来源:RequestUtil.java

示例3: constructBaseUrl

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
protected String constructBaseUrl(HttpServletRequest request) {
  String scheme = request.getScheme();
  if (request.getHeader("x-forwarded-proto") != null) {
    scheme = request.getHeader("x-forwarded-proto");
  }
  int serverPort = request.getServerPort();
  if (request.getHeader("x-forwarded-port") != null) {
    try {
      serverPort = request.getIntHeader("x-forwarded-port");
    } catch (NumberFormatException e) {
    }
  }

  String baseUrl = String.format("%s://%s:%d", scheme, request.getServerName(), serverPort);
  return baseUrl;
}
 
开发者ID:osswangxining,项目名称:iotplatform,代码行数:17,代码来源:BaseController.java

示例4: generateServletUrl

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
public static String generateServletUrl(HttpServletRequest request, String relativePath) {
	String scheme = request.getScheme().toLowerCase();
	String serverName = request.getServerName();
	int serverPort = request.getServerPort();
	String contextPath = request.getContextPath();
	StringBuilder url =  new StringBuilder();
	url.append(scheme).append("://").append(serverName);
	if (("http".equals(scheme) && serverPort != 80) || ("https".equals(scheme) && serverPort != 443)) {
		url.append(":").append(serverPort);
	}
	url.append(contextPath);
	if (!Preconditions.isEmptyString(relativePath)) {
		url.append(relativePath);
	}
	return url.toString();
}
 
开发者ID:sgr-io,项目名称:social-signin,代码行数:17,代码来源:GoogleSignInService.java

示例5: getCachingKey

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
/**
 * Returns the caching key which is based on the servlet path
 * as well as the  cachedPath.
 *
 * @param request
 */
protected String getCachingKey(HttpServletRequest request) {
    StringBuilder absolutePath = new StringBuilder();
    String scheme = request.getScheme();
    int port = request.getServerPort();
    
    absolutePath.append(scheme);
    absolutePath.append("://");
    absolutePath.append(request.getServerName());
    
    if (port > 0 && (("http".equalsIgnoreCase(scheme) && port != 80) || ("https".equalsIgnoreCase(scheme) && port != 443))) {
        absolutePath.append(':');
        absolutePath.append(port);
    }
    
    absolutePath.append(request.getContextPath());
    absolutePath.append(request.getServletPath());
    
    if (request.getPathInfo() != null) {
        absolutePath.append(request.getPathInfo());
    }
    return absolutePath.toString();
}
 
开发者ID:devefx,项目名称:validator-web,代码行数:29,代码来源:CachingHandler.java

示例6: getWholeUrl

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
/**
 * 获取完整的url,包括域名端口等
 * @return
 */
public static String getWholeUrl(HttpServletRequest request){
	String servername =request.getServerName();
	String path  = request.getServletPath();
	int port = request.getServerPort();
	
	String portstr="";
	if(port!=80){
		portstr=":"+port;
	}
	String contextPath = request.getContextPath();
	if(contextPath.equals("/")){
		contextPath="";
	}
	
	
	String url  = "http://"+servername+portstr+contextPath+"/"+path;
	 
	return url;
	
}
 
开发者ID:yulele166,项目名称:pub-service,代码行数:25,代码来源:RequestUtil.java

示例7: Saved2Request

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
public Saved2Request(HttpServletRequest request, String backUrl) {
	super(request);
	this.scheme = request.getScheme();
	this.domain = request.getServerName();
	this.port = request.getServerPort();
	this.backUrl = backUrl;
	this.contextPath = request.getContextPath();
}
 
开发者ID:jiangzongyao,项目名称:kettle_support_kettle8.0,代码行数:9,代码来源:Saved2Request.java

示例8: getUrlBase

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
public static String getUrlBase(HttpServletRequest request) {
  if ((request.getServerPort() == 80) || (request.getServerPort() == 443)) {
    return request.getScheme() + "://" + request.getServerName();
  } else {
    return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
  }
}
 
开发者ID:logistimo,项目名称:logistimo-web-service,代码行数:8,代码来源:HttpUtil.java

示例9: testIncomingRequestIsSecuredButProtocolHeaderSaysItIsNotWithCustomValues

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
@Test
public void testIncomingRequestIsSecuredButProtocolHeaderSaysItIsNotWithCustomValues() throws Exception {
    // PREPARE
    FilterDef filterDef = new FilterDef();
    filterDef.addInitParameter("protocolHeader", "x-forwarded-proto");
    filterDef.addInitParameter("remoteIpHeader", "x-my-forwarded-for");
    filterDef.addInitParameter("httpServerPort", "8080");

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr("192.168.0.10");
    request.setSecure(true);
    request.setScheme("https");
    request.setHeader("x-my-forwarded-for", "140.211.11.130");
    request.setHeader("x-forwarded-proto", "http");

    // TEST
    HttpServletRequest actualRequest = testRemoteIpFilter(filterDef, request).getRequest();

    // VERIFY
    boolean actualSecure = actualRequest.isSecure();
    assertFalse("request must be unsecured as header x-forwarded-proto said it is http", actualSecure);

    String actualScheme = actualRequest.getScheme();
    assertEquals("scheme must be http as header x-forwarded-proto said it is http", "http", actualScheme);

    int actualServerPort = actualRequest.getServerPort();
    assertEquals("wrong http server port", 8080, actualServerPort);

    String actualRemoteAddr = actualRequest.getRemoteAddr();
    assertEquals("remoteAddr", "140.211.11.130", actualRemoteAddr);

    String actualRemoteHost = actualRequest.getRemoteHost();
    assertEquals("remoteHost", "140.211.11.130", actualRemoteHost);
}
 
开发者ID:sunmingshuai,项目名称:apache-tomcat-7.0.73-with-comment,代码行数:35,代码来源:TestRemoteIpFilter.java

示例10: processRequest

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
	response.setContentType("text/html;charset=UTF-8");
	try (PrintWriter out = response.getWriter()) {
		out.println("<html>");
		out.println("<head>");
		out.println("<title>Invoke the servlet clients</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<h1>Invoke the servlet clients</h1>");

		String path = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath()
				+ "/WritingServlet";
		out.println("Invoking the endpoint: " + path + "<br>");
		out.flush();
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		try (BufferedReader input = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
			out.println("Sending data ..." + "<br>");
			out.flush();
			out.println(input.readLine());
			out.println("Sleeping ..." + "<br>");
			out.flush();
			sleep(5000);
			out.println("Sending more data ..." + "<br>");
			out.flush();
			out.println(input.readLine());
			input.close();
		}
		out.println("<br><br>Check server.log for output");
		out.println("</body>");
		out.println("</html>");
	} catch (InterruptedException | IOException ex) {
		logger.log(SEVERE, null, ex);
	}
}
 
开发者ID:PacktPublishing,项目名称:Mastering-Java-EE-Development-with-WildFly,代码行数:37,代码来源:ReadClient.java

示例11: getServerBaseUrl

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
public static String getServerBaseUrl(final HttpServletRequest request){
    String schema=request.getHeader("x-forwarded-proto");
    if(schema==null || "".equals(schema)){
        schema=request.getScheme();
    }
    schema+="://";
    String host =request.getHeader("x-forwarded-host");
    if(host==null || "".equals(host)){
        host=request.getServerName() + ":" + request.getServerPort();
    }
    String url=schema+host;
    url=regularUrl(url);

    return url;
}
 
开发者ID:bingo-oss,项目名称:sso-client,代码行数:16,代码来源:Urls.java

示例12: XForwardedRequest

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
public XForwardedRequest(HttpServletRequest request) {
    super(request);
    this.localPort = request.getLocalPort();
    this.remoteAddr = request.getRemoteAddr();
    this.remoteHost = request.getRemoteHost();
    this.scheme = request.getScheme();
    this.secure = request.isSecure();
    this.serverPort = request.getServerPort();
    
    headers = new HashMap<String, List<String>>();
    for (Enumeration<String> headerNames = request.getHeaderNames(); headerNames.hasMoreElements();) {
        String header = headerNames.nextElement();
        headers.put(header, Collections.list(request.getHeaders(header)));
    }
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:16,代码来源:RemoteIpFilter.java

示例13: getRootUrl

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
public static String getRootUrl(HttpServletRequest request) {
	return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
}
 
开发者ID:bndynet,项目名称:web-framework-for-java,代码行数:4,代码来源:HttpHelper.java

示例14: doFilter

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
		throws IOException, ServletException {
	// TODO Auto-generated method stub
			HttpServletRequest req = (HttpServletRequest) request;
			HttpServletResponse res = (HttpServletResponse) response;
			//从SESSION获取对象
			Object obj = req.getSession().getAttribute("user");
			String servletPath = req.getServletPath();
			//System.out.println(obj+"0");
			if(obj != null){
				//System.out.println(obj+"3");
				chain.doFilter(request, response);//请求:我自己定义请求,例外就是页面JS,JSP,HTML
			}else{
				//获取访问的地址
				/**
				 * res.getScheme() 获取访问的协议 http https
				 * res.getServerName() 获取访问的项目名称
				 * res.getServerPort() 获取访问的端口
				 * res.getContextPath();//获取项目名称
				 * res.getServletPath();//实际访问的路径
				 */
				String path = req.getContextPath();
				String basePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+path+"/";
				
				if(servletPath.contains(".")){
					int index = servletPath.lastIndexOf(".");
					String suffix = servletPath.substring(index);
					//System.out.println("suffix:"+suffix);
					if(".js.html.css.jpg.png".contains(suffix)||"/login.html".equals(servletPath)){
						chain.doFilter(request, response);
					}else{//重定向
						System.out.println("重定向");
						res.sendRedirect(basePath);
					}
				}else{//这里是处理我们自己定义请求
					String loginStatus = (String) req.getSession().getAttribute("loginStatus");
					if(loginStatus!=null&&!loginStatus.equals("")&&loginStatus.equals("loginout")){//如果是登出后 刷新原有的页面则需要进行重定向到登录页面
						req.getSession().invalidate();//清除session
						res.sendRedirect(basePath);
					}else{
					//System.out.println(servletPath+"1");
					if(servletPath.contains("login")||servletPath.contains("regist")){
						//System.out.println(req.getServletPath()+"::::::::"+res.getLocale());
						chain.doFilter(request, response);
					}else if(servletPath.contains("ficationCode")||servletPath.contains("regist")){
						chain.doFilter(request, response);
					}else if(servletPath.contains("repassword")||servletPath.contains("regist")){
						chain.doFilter(request, response);
					}else{//重定向
						System.out.println(basePath+"login.html");
						res.sendRedirect(basePath+"login.html");
					}
				    }
				}
			}
	
}
 
开发者ID:lonelyshine,项目名称:xcx-specialty,代码行数:59,代码来源:LoginFilter.java

示例15: getWebUrlPath

import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
public static String getWebUrlPath(HttpServletRequest request) {
    String port = request.getServerPort() == 80 ? "" : (":"+request.getServerPort());
    String path = request.getContextPath().replaceAll("/$","");
    return request.getScheme()+"://"+request.getServerName()+port+path;
}
 
开发者ID:wolfboys,项目名称:opencron,代码行数:6,代码来源:WebUtils.java


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