本文整理汇总了Java中javax.servlet.http.HttpServletRequest.getServerName方法的典型用法代码示例。如果您正苦于以下问题:Java HttpServletRequest.getServerName方法的具体用法?Java HttpServletRequest.getServerName怎么用?Java HttpServletRequest.getServerName使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.servlet.http.HttpServletRequest
的用法示例。
在下文中一共展示了HttpServletRequest.getServerName方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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();
}
示例2: getWebRoot
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
/**
* 得到项目根路径(形如: http://bridgeli.cn/)
*
* @param request
* @return
*/
public static String getWebRoot(HttpServletRequest request) {
// 得到协议如:http
String scheme = request.getScheme();
// 得到服务器名称如:127.0.0.1
String serverName = request.getServerName();
// 得到端口号如8080
int serverPort = request.getServerPort();
// 得到当前上下文路径,也就是安装后的文件夹位置。
String contextPath = request.getContextPath();
// 连起来拼成完整的url
StringBuffer sb = new StringBuffer();
sb.append(scheme);
sb.append("://");
sb.append(serverName);
if (80 != serverPort) {
sb.append(":");
sb.append(serverPort);
}
sb.append(contextPath);
sb.append("/");
return sb.toString();
}
示例3: redirect
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
private boolean redirect(HttpServletRequest request, HttpServletResponse response){
String url = "http://" + request.getServerName() + ":"
+ request.getServerPort() + request.getContextPath();
try {
// request.getSession().setAttribute("redirect",request.getServletPath());
response.sendRedirect(url);
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
示例4: 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;
}
示例5: 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();
}
}
示例6: getFullURL
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
/**
* 获取Http请求的完整路径,包括参数
*
* @param request
* @return
*/
public static String getFullURL(HttpServletRequest request) {
String url = "";
url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getServletPath();
if (request.getQueryString() != null) {
url += "?" + request.getQueryString();
}
return url;
}
示例7: isLocalOrigin
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
private boolean isLocalOrigin(HttpServletRequest request, String origin) {
// Build scheme://host:port from request
StringBuilder target = new StringBuilder();
String scheme = request.getScheme();
if (scheme == null) {
return false;
} else {
scheme = scheme.toLowerCase(Locale.ENGLISH);
}
target.append(scheme);
target.append("://");
String host = request.getServerName();
if (host == null) {
return false;
}
target.append(host);
int port = request.getServerPort();
if ("http".equals(scheme) && port != 80 ||
"https".equals(scheme) && port != 443) {
target.append(':');
target.append(port);
}
return origin.equalsIgnoreCase(target.toString());
}
示例8: 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);
}
}
示例9: doGet
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String redirectUrl = request.getScheme() + "://"
+ request.getServerName() + ":" + regionServerInfoPort
+ request.getRequestURI();
response.sendRedirect(redirectUrl);
}
示例10: getApplicationRootUrl
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
public static String getApplicationRootUrl() {
try {
HttpServletRequest request = ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest());
URL reconstructedUrl = new URL(request.getScheme(), request.getServerName(), request.getServerPort(), "");
return reconstructedUrl.toString();
} catch (Exception e) {
return getHttpBaseUrl();
}
}
示例11: execute
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
@Override
public String execute() throws Exception {
HttpServletRequest request=Struts2Utils.getRequest();
String tabId=request.getParameter("tabId");
String baseUrl = "";
baseUrl = request.getScheme() +"://" + request.getServerName()
+ (request.getServerPort() == 80 ? "" : ":" +request.getServerPort())
+ request.getContextPath();
request.setAttribute("baseUrl", baseUrl);
User user=accountManager.getCurUser();
if(user!=null){
SurveyDirectory surveyDirectory=surveyDirectoryManager.getSurveyByUser(surveyId, user.getId());
if(surveyDirectory!=null){
request.setAttribute("survey", surveyDirectory);
if(IFRAME.equals(tabId)){
return IFRAME;
}else if(SITECOMP.equals(tabId)){
return SITECOMP;
}else if(WEIXIN.equals(tabId)){
return WEIXIN;
}else if(SHARE.equals(tabId)){
return SHARE;
}
return COLLECT1;
}
}
return null;
}
示例12: doFilterInternal
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
String requestURI = request.getRequestURI();
LOG.debug("filt uri : " + requestURI);
requestURI = requestURI.replaceAll("/+", "/");
if (requestURI.equals("/")||requestURI.startsWith("/resources")|| requestURI.equals("/home/auth")) {
filterChain.doFilter(request, response);
return;
}
String path = request.getContextPath();
this.basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
HttpSession session = request.getSession();
User user=(User) session.getAttribute("curUser");
if(null==user){
java.io.PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<script>");
out.println("window.open ('"+basePath+"','_top')");
out.println("</script>");
out.println("</html>");
}else{
filterChain.doFilter(request, response);
return;
}
}
示例13: assembleCallbackUrl
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
protected String assembleCallbackUrl(HttpServletRequest request) {
return request.getScheme() + "://" +
request.getServerName() +
getServerPort(request) +
request.getContextPath();
}
示例14: authenticate
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
/**
* Authenticate the user making this request, based on the specified
* login configuration. Return <code>true</code> if any specified
* constraint has been satisfied, or <code>false</code> if we have
* created a response challenge already.
*
* @param request Request we are processing
* @param response Response we are creating
* @param login Login configuration describing how authentication
* should be performed
*
* @exception IOException if an input/output error occurs
*/
public boolean authenticate(HttpRequest request,
HttpResponse response,
LoginConfig config)
throws IOException {
// Have we already authenticated someone?
Principal principal =
((HttpServletRequest) request.getRequest()).getUserPrincipal();
if (principal != null) {
if (debug >= 1)
log("Already authenticated '" + principal.getName() + "'");
return (true);
}
// Validate any credentials already included with this request
HttpServletRequest hreq =
(HttpServletRequest) request.getRequest();
HttpServletResponse hres =
(HttpServletResponse) response.getResponse();
String authorization = request.getAuthorization();
String username = parseUsername(authorization);
String password = parsePassword(authorization);
principal = context.getRealm().authenticate(username, password);
if (principal != null) {
register(request, response, principal, Constants.BASIC_METHOD,
username, password);
return (true);
}
// Send an "unauthorized" response and an appropriate challenge
String realmName = config.getRealmName();
if (realmName == null)
realmName = hreq.getServerName() + ":" + hreq.getServerPort();
// if (debug >= 1)
// log("Challenging for realm '" + realmName + "'");
hres.setHeader("WWW-Authenticate",
"Basic realm=\"" + realmName + "\"");
hres.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
// hres.flushBuffer();
return (false);
}
示例15: setAuthenticateHeader
import javax.servlet.http.HttpServletRequest; //导入方法依赖的package包/类
/**
* Generates the WWW-Authenticate header.
* <p>
* The header MUST follow this template :
* <pre>
* WWW-Authenticate = "WWW-Authenticate" ":" "Digest"
* digest-challenge
*
* digest-challenge = 1#( realm | [ domain ] | nOnce |
* [ digest-opaque ] |[ stale ] | [ algorithm ] )
*
* realm = "realm" "=" realm-value
* realm-value = quoted-string
* domain = "domain" "=" <"> 1#URI <">
* nonce = "nonce" "=" nonce-value
* nonce-value = quoted-string
* opaque = "opaque" "=" quoted-string
* stale = "stale" "=" ( "true" | "false" )
* algorithm = "algorithm" "=" ( "MD5" | token )
* </pre>
*
* @param request HTTP Servlet request
* @param resonse HTTP Servlet response
* @param login Login configuration describing how authentication
* should be performed
* @param nOnce nonce token
*/
protected void setAuthenticateHeader(HttpServletRequest request,
HttpServletResponse response,
LoginConfig config,
String nOnce) {
// Get the realm name
String realmName = config.getRealmName();
if (realmName == null)
realmName = request.getServerName() + ":"
+ request.getServerPort();
byte[] buffer = md5Helper.digest(nOnce.getBytes());
String authenticateHeader = "Digest realm=\"" + realmName + "\", "
+ "qop=\"auth\", nonce=\"" + nOnce + "\", " + "opaque=\""
+ md5Encoder.encode(buffer) + "\"";
// System.out.println("Authenticate header value : "
// + authenticateHeader);
response.setHeader("WWW-Authenticate", authenticateHeader);
}