How to get my URL path?

I would like to show my current url path when I execute a jsp page.

I write a javabean file, using this method:

String currentdir = System.getProperty("user.dir");

Then print it out in my jsp page, it shows:

D:\Program Files\Apache Software Foundation\Tomcat 5.5 but my desired answer should be:

http://roamer:8080/myweb/

How can I acheive this purpose? Beside, I tried

getServletConfig().getServletContext().getRealPath("/") and it returns nothing in my jsp page, I guess the reason is I am calling this page as a java bean, not via a servlet call, thus the related object, function, method are not available. Am I right?

[672 byte] By [roamera] at [2007-11-27 10:46:20]
# 1

You need HttpServletRequest#getRequestURL().

BalusCa at 2007-7-28 20:18:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Thx, but how to use such method? Creating an object? Or use statically?

roamera at 2007-7-28 20:18:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Depends on your class. Assuming that it just extends HttpServlet and you're implementing for example the doGet(), the HttpServletRequest object is available to you as 1st method parameter.

BalusCa at 2007-7-28 20:18:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Yes, and how can I acheive this if I call this java file as javabean? It doesn't invoke any HttpServlet object / method.

roamera at 2007-7-28 20:18:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Then pass the 'request' reference to it?

BalusCa at 2007-7-28 20:18:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Can't, because I am calling it in jsp via javabean, it won't invoke any doget or dopost method.

roamera at 2007-7-28 20:18:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Yes .. I said, pass the 'request' reference (or maybe even the value from request.getRequestURL()) to the Javabean method.

BalusCa at 2007-7-28 20:18:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

ShowEvent.java:45: cannot find symbol

symbol : variable request

location: class html.ShowEvent

request.setCharacterEncoding("utf-8");

^

1 error

No such variable.

roamera at 2007-7-28 20:18:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...