`
hyshucom
  • 浏览: 810222 次
文章分类
社区版块
存档分类
最新评论

Getting the Requesting URL in a Servlet

 
阅读更多

Getting the Requesting URL in a Servlet

A servlet container breaks up the requesting URL into convenientcomponents for the servlet. The standard API does not require theoriginal requesting URL to be saved and therefore it is not possibleto get the requesting URL exactly as the client sent it. However, afunctional equivalent of the original URL can be constructed. Thefollowing example assumes the original requesting URL is:
http://hostname.com/mywebapp/servlet/MyServlet/a/b;c=123?d=789
The most convenient method for reconstructing the original URL is touse ServletRequest.getRequestURL(), which returns all but thequery string. Adding the query string reconstructs an equivalent ofthe original requesting URL:
// http://hostname.com/mywebapp/servlet/MyServlet/a/b;c=123?d=789
public static String getUrl(HttpServletRequest req) {
    String reqUrl = req.getRequestURL().toString();
    String queryString = req.getQueryString();   // d=789
    if (queryString != null) {
        reqUrl += "?"+queryString;
    }
    return reqUrl;
}
If the hostname is not needed, ServletRequest.getRequestURI()should be used:
// /mywebapp/servlet/MyServlet/a/b;c=123?d=789
public static String getUrl2(HttpServletRequest req) {
    String reqUri = req.getRequestURI().toString();
    String queryString = req.getQueryString();   // d=789
    if (queryString != null) {
        reqUri += "?"+queryString;
    }
    return reqUri;
}
The original URL can also be reconstructed from more basic componentsavailable to the servlet:
// http://hostname.com:80/mywebapp/servlet/MyServlet/a/b;c=123?d=789
public static String getUrl3(HttpServletRequest req) {
    String scheme = 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();         // /a/b;c=123
    String queryString = req.getQueryString();          // d=789

    // Reconstruct original requesting URL
    String url = scheme+"://"+serverName+":"+serverPort+contextPath+servletPath;
    if (pathInfo != null) {
        url += pathInfo;
    }
    if (queryString != null) {
        url += "?"+queryString;
    }
    return url;
}

分享到:
评论

相关推荐

    计算机网络第六版答案

    that target a single node (for example, all nodes in the botnet might be commanded by the attacker to send a TCP SYN message to the target, which might result in a TCP SYN flood attack at the target)...

    Unity.in.Action.Multiplatform.Game.Development.in.Csharp

    Displaying inventory items in the UI 186 ■ Equipping a key to use on locked doors 188 ■ Restoring the player’s health by consuming health packs 190 8.5 Summary 191 STRONG FINISH ......................

    WordPress 3 Plugin Development Essentials.pdf

    In your browser—getting the post URL 60 Getting the post title 60 Getting the description 60 Getting the media type 62 Getting the post topic 62 In your browser—title, description, and topic ...

    The Landmarks of New York, Fifth Edition

    that had taken place in a landmark since designation and determining the use of the landmark. Effecting changes in landmark structures is not only wholly possible, but has been constant and widespread...

    CommonsWare.The.Busy.Coders.Guide.to.Android.Development.Version.8.2.2017

    Android, the next-generation open mobile platform from Google and the Open Handset Alliance, is poised to become a significant player in the mobile device market. The Android platform gives developers...

    The Busy Coders Guide to Android Development最终版2019

    Use the search field in the nav bar to search all of the CommonsWare books to see what they hold! Key Android Concepts Choosing Your Development Toolchain Tutorial #1 - Installing the Tools Android ...

    Cloud Programming (EECS-2019-3).pdf

    and download the data in a secure way plays an important role, as users are nowadays performing these actions on all types of devices, including e.g. smartphones. Signing and encryption of the ...

    微软内部资料-SQL性能优化2

    Although a system having less than 2 GB of physical memory can be booted using the /3G switch, in most cases this is ill-advised. If you restart with the 3 GB switch, also known as 4-Gig Tuning, the ...

    一本android的好书beginning android 2 和 源码

    In the Beginning, There Was the Root, And It Was Good Permissions, Instrumentations, and Applications (Oh My!) Your Application Does Something, Right? Achieving the Minimum Version=Control ...

    Perl-compatible Regular Expressions VC compile project

    In addition to the Perl-compatible matching function, PCRE contains an alternative matching function that matches the same compiled patterns in a different way. In certain circumstances, the ...

    Molecular Biology Problem Solver - A Laboratory Guide.pdf 下载

    headings contained in the answer.The authors and I would like to think that this information and the questions they inspire will provide insight and perspective to help you solve problems that go ...

    Recover4all Professional

    You may register a different individual by sending email requesting a change in registration to support@recover4all.com. The newly registered individual, and not the previously registered individual...

    USB Complete 3rdEdition

    Requesting a Structure Containing the Device Path Name 298 Extracting the Device Path Name 301 Closing Communications 302 Contents ix Obtaining a Handle 303 Requesting a Communications Handle 303 ...

    java7帮助文档

    JDK 7 is a superset of JRE 7, and contains everything that is in JRE 7, plus tools such as the compilers and debuggers necessary for developing applets and applications. JRE 7 provides the libraries, ...

    rfc全部文档离线下载rfc1-rfc8505

    a conversation is contained in the IMP software. It is thus not possible to query an IMP about the state of a link (although it might be possible to query an IMP about the recent history of a link ...

    libgcc_s_dw2-1.dll另一种版本

    After extracting your zip-file, place the extracted DLL-file in the directory of the program that is requesting the file. Make sure to use a 32bit DLL-file for a 32bit program, and a 64bit DLL-file ...

    Learning Spring 5.0

    At the end, you will enhance your development skills by getting to grips with the integration of RESTful APIs, building microservices, and doing reactive programming using Spring, as well as ...

    Dynamical Partial Reconfigurable FPGA

    which has one static part and two partial reconfigurable modules, ICMP and HTTP. A Web Client sends different packets to the system requesting different services. These packets’ type information are...

    Android SampleNetworking

    What happens when I find a bug in the parsing code or perhaps a new node is added to the XML? We’d have to go through all the different places and make change. By having a single point for parsing ...

    FCKeditor.Net_2.5

    For file uploads, the file extension is precisely controlled in a list defined in the config.ascx file. It is possible to define different folder locations for each file type. Attention :...

Global site tag (gtag.js) - Google Analytics