javaurlweb-applicationsconfirmation-email

How can I prepare the full URL of a web application in a confirmation mail in java


In my application, I would like to send an email to the user who created a new account to confirm his email. As usual, there will be a URL to be clicked by the user in the mail. However, I couldn't figure out how the full URL of the web application is created.

Currently I can access to ServletContext. Is there a way to construct full URL of the application through the ServletContext object and is this an elegant way?

In general, I would like to learn about the best practice which is used to create such confirmation mails (especially the URL preparation part).

Thanks in advance


Solution

  • Try using methods such as below:

     ServletContext.getContextPath()
     HttpServletRequest.getRequestURL()
     HttpServletRequest.getServerName()
     HttpServletRequest.getServerPort()
     HttpServletRequest.getRequestURI()
     HttpServletRequest.getServletPath()
     HttpServletRequest.getPathInfo()
     HttpServletRequest.getQueryString()
    

    Use as appropriate.

    Please Note: If you are using HTTP server as a proxy, you may not be able to reconstruct the original URL. In that case, better do define some constants for the website URLs and use the same as base URL in your email. Any infomation other than base URL can be retrieved using above methods.