0

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

suat
  • 4,239
  • 3
  • 28
  • 51
  • I've generally seen the url handed to the web application as a deployment parameter. Given that the application server may be behind web servers, proxy servers, and load balancers, there is rarely a consistent way to programatically determine the externally visible url. – Devon_C_Miller Oct 02 '12 at 21:06

2 Answers2

1

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.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • OK, thanks. Then the best practice seems to configure it through a configuration file. – suat Oct 03 '12 at 04:37
1

I think you can provide a configuration to configure the root URL. Are u sure the confirming url is same as registering url??

Tony Zhu
  • 301
  • 1
  • 6
  • 16
  • Root URL enough for me. But it seems I should configure it through a configuration file, thanks. – suat Oct 03 '12 at 04:36