Vitajte na [www.pocitac.win] Pripojiť k domovskej stránke Obľúbené stránky
Použite ServletRequest.getRequestURL ( ) :
public static String getURL ( HttpServletRequest req ) {
String reqUrl = req.getRequestURL ( ) toString ( . ) ;
String QueryString = req.getQueryString ( ) ; //d = 789
if ( QueryString = null ) {
reqUrl + = + QueryString ! " ? " ;
}
vrátiť reqUrl ; .
}
Táto metóda vráti všetko, ale reťazec dotazu
2
používania ServletRequest.getRequestURI ( ) , ak nepotrebujete názov hostiteľa : .
public static String getURL ( HttpServletRequest req ) {
String reqUri = req.getRequestURI ( ) toString ( ) ;
String QueryString = req.getQueryString ( ) ; //d = 789
if ( QueryString = null ! ) {
reqUri + = + QueryString " ? " ;
}
vrátiť reqUri ;
}
3
Pre vytvorenie URL od základov , použite nasledujúce :
public static String getURL ( HttpServletRequest req ) {
String schéma = req.getScheme ( ) ; //http
String ServerName = req.getServerName ( ) ; //hostname.com
int serverPort = req.getServerPort ( ) ; //80
String contextPath = req.getContextPath ( ) ; ///MyWebApp
String servletPath = req.getServletPath ( ) ///servlet /MyServlet
String PathInfo = req.getPathInfo ( ) ; ////b , c = 123
String QueryString = req.getQueryString ( ) ; //d = 789
//Rekonštrukcia pôvodnej žiadajúci URL
String url = program + " ://" + SERVERNAME + " : " + serverPort + contextPath + servletPath ; if ( ! PathInfo = null ) {
url = + PathInfo ;
}
if ( QueryString = null ! ) {
url = + + QueryString " ? " ;
}
vrátiť url ;
}