Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Get complete URL address on address bar of browser

Get complete URL address on address bar of browser

Scheduled Pinned Locked Moved Java
javacomtutorialquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tannghia
    wrote on last edited by
    #1

    I want to get complete URL address is displaying on address bar of browser. Example http://wwww.eshop.com/ShowMember?name=Peter White&id=123 I use JSP and Serlet to create web application. In above exmaple : ShowMember is a serlet http://wwww.eshop.com/ShowMember?name=Peter White&id=123 : display a JSP page on browser that is invoked by ShowMember Serlet

    L G 2 Replies Last reply
    0
    • T tannghia

      I want to get complete URL address is displaying on address bar of browser. Example http://wwww.eshop.com/ShowMember?name=Peter White&id=123 I use JSP and Serlet to create web application. In above exmaple : ShowMember is a serlet http://wwww.eshop.com/ShowMember?name=Peter White&id=123 : display a JSP page on browser that is invoked by ShowMember Serlet

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Your message is not very clear, please try and explain what is not working. I would also suggest you move this to the Web Development[^] forum, as your question appears to be concerned with Web issues rather than Java.

      It's time for a new signature.

      1 Reply Last reply
      0
      • T tannghia

        I want to get complete URL address is displaying on address bar of browser. Example http://wwww.eshop.com/ShowMember?name=Peter White&id=123 I use JSP and Serlet to create web application. In above exmaple : ShowMember is a serlet http://wwww.eshop.com/ShowMember?name=Peter White&id=123 : display a JSP page on browser that is invoked by ShowMember Serlet

        G Offline
        G Offline
        Gerben Jongerius
        wrote on last edited by
        #3

        It depends a bit on how the page was called, but for as far as I know you can't get the complete URL simply by one call. You will need to construct it yourself as follows:

        String url = request.getScheme() + "://" +
        request.getServerName() + "/" +
        request.getContextPath() + "/" +
        request.getServletPath() +
        (request.getQueryString() != null ? "?" + request.getQueryString() : "");

        I haven't done a code check but this should work presuming you are in the servlet or JSP file and have the HttpServletRequest object available as 'request'. Which should be the default for JSP files and the .get() and .post() members in any servlet.

        T 1 Reply Last reply
        0
        • G Gerben Jongerius

          It depends a bit on how the page was called, but for as far as I know you can't get the complete URL simply by one call. You will need to construct it yourself as follows:

          String url = request.getScheme() + "://" +
          request.getServerName() + "/" +
          request.getContextPath() + "/" +
          request.getServletPath() +
          (request.getQueryString() != null ? "?" + request.getQueryString() : "");

          I haven't done a code check but this should work presuming you are in the servlet or JSP file and have the HttpServletRequest object available as 'request'. Which should be the default for JSP files and the .get() and .post() members in any servlet.

          T Offline
          T Offline
          tannghia
          wrote on last edited by
          #4

          http://wwww.eshop.com/ShowMember?name=Peter White&id=123 : display a JSP page on browser which is called by ShowMember Serlet But the follwing code will return http://wwww.eshop.com/ShowMember.jsp?name=Peter White&id=123 String url = request.getScheme() + "://" + request.getServerName() + "/" + request.getContextPath() + "/" + request.getServletPath() + (request.getQueryString() != null ? "?" + request.getQueryString() : ""); I want to get the Serlet which called the JSP page which mean the address does not contain ".jsp"

          G 1 Reply Last reply
          0
          • T tannghia

            http://wwww.eshop.com/ShowMember?name=Peter White&id=123 : display a JSP page on browser which is called by ShowMember Serlet But the follwing code will return http://wwww.eshop.com/ShowMember.jsp?name=Peter White&id=123 String url = request.getScheme() + "://" + request.getServerName() + "/" + request.getContextPath() + "/" + request.getServletPath() + (request.getQueryString() != null ? "?" + request.getQueryString() : ""); I want to get the Serlet which called the JSP page which mean the address does not contain ".jsp"

            G Offline
            G Offline
            Gerben Jongerius
            wrote on last edited by
            #5

            You could replace the getContextPath() and getServletPath() with getRequestURL() this should provide the url part after the domain up to the query string.

            T 1 Reply Last reply
            0
            • G Gerben Jongerius

              You could replace the getContextPath() and getServletPath() with getRequestURL() this should provide the url part after the domain up to the query string.

              T Offline
              T Offline
              tannghia
              wrote on last edited by
              #6

              I tried it but it only return the address of JSP page which is called by Serlet. I want to get the Serlet which called this JSP Page

              G 1 Reply Last reply
              0
              • T tannghia

                I tried it but it only return the address of JSP page which is called by Serlet. I want to get the Serlet which called this JSP Page

                G Offline
                G Offline
                Gerben Jongerius
                wrote on last edited by
                #7

                How do you forward the call from the Servlet to the JSP, are you doing this with the RequestDispatcher.forward() or by a response.sendRedirect()? If you are using the sendRedirect it is impossible to get the path of the servlet, as this is lost by a secondary client call to the JSP page.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups