jsp中如何取得当前页面完整的URL

时间:2023-03-09 19:15:31
jsp中如何取得当前页面完整的URL
JSP页面
<% String url = request.getScheme()+"://"+ request.getServerName()+request.getRequestURI()+"?"+request.getQueryString(); %>

JAVA代码

    String basePath = request.getScheme()+"://"+request.getServerName();
    String    gotoURL = request.getRequestURI();
    
    gotoURL = gotoURL.replaceAll("//", "/");
    if(gotoURL.indexOf("/WEB-INF/jsp/") != -1){
        gotoURL = gotoURL.replace("/WEB-INF/jsp/","/");
        gotoURL = gotoURL.replace(".jsp",".shtm");
    }
    gotoURL = basePath+gotoURL;
    String queryStr = request.getQueryString();
    if(queryStr!=null){
        gotoURL = gotoURL +"?"+queryStr;
    }
    try {
        gotoURL = URLEncoder.encode(gotoURL,"UTF-8");
    } catch (Exception e1) {
        e1.printStackTrace(); 

}