Get the IP and port of the server in java

struts2 get request

HttpServletRequest requet=ServletActionContext.getRequest();

requet.getScheme()+"://"+requet.getServerName()+":"+requet.getServerPort()

(for example) the result is: http://localhost:8080/

 

request.getHeader("User-Agent"); //Get the client's system version     
request.getRemoteAddr(); //Get the client's IP     
request.getRemoteHost() //Get the client's host name     
request.getRemotePort(); / /Get the client's port     
request.getRemoteUser(); //Get the client's user     
request.getLocalAddr(); //Get the server IP     
request.getLocalPort(); //Get the server port

 


  1. Obtain the relative path of the current application and the absolute path corresponding to the absolute path root directory in JSP : request.getRequestURI()
  The absolute path of the file: application.getRealPath(request.getRequestURI());
  the absolute path of the current web application: application .getRealPath("/");
  Get the upper directory of the requested file: new File(application.getRealPath(request.getRequestURI())).getParent()

2 Obtain the relative path and absolute path
  root directory corresponding to the current application in the Servlet Absolute path: request.getServletPath();
  Absolute path of file: request.getSession().getServletContext().getRealPath(request.getRequestURI())   
  Absolute path of current web application: servletConfig.getServletContext().getRealPath("/" );
  (The ServletContext object can be obtained in several ways:
  javax.servlet.http.HttpSession.getServletContext()
  javax.servlet.jsp.PageContext.getServletContext()
  javax.servlet.ServletConfig.getServletContext()
  )

3. Obtain the absolute path in the Java class
  According to the Doc file of java.io.File, we know that: By default, the directory represented by new File("/") is: System.getProperty( "user.dir").

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324804705&siteId=291194637