當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。