how can I get the servlet access url from servlet context

S

shaoanqing

hi
I am coding for a WebApp
I wrote a servlet and defined the mapping in web.xml like:

<servlet>
<display-name>MySer</display-name>
<servlet-name>MySer</servlet-name>
<servlet-class>com.santa.servlet.MyServlet</servlet-class>
<load-on-startup>30</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/service/MySer</url-pattern>
</servlet-mapping>

I am now can access the servlet using the URL:
http://localhost:8080/myapp/service/MySer

the question is how can I get the URL from the servlet Context
or at least how can I get the url pattern("service/MySer") from
servletContext?

thanks.
 
S

santax

request.getRequestURL()

Arne
Thanks.
I am sorry , I was not present my problem clearly,
I want get the URL before any user accessing it.
I set the servle as load-on-startup servlet by definded below in
web.xml

<servlet>
<display-name>MySer</display-name>
<servlet-name>MySer</servlet-name>
<servlet-class>com.santa.servlet.MyServlet</servlet-class>
<load-on-startup>30</load-on-
startup>
<<---------------------------------------- init the servlet when
server start
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/service/MySer</url-pattern>
</servlet-mapping>
so ,I want get the url-pattern in the method

@Override
public void init(ServletConfig arg0) throws ServletException {
System.out.println("*****************************");
// I want get the url-pattern here
System.out.println("*****************************");
super.init(arg0);
}

Thank you very much for your reply.
 
A

angrybaldguy

Thanks.
I am sorry , I was not present my problem clearly,
I want get the URL before any user accessing it.
I set the servle as load-on-startup servlet by definded below in web.xml

<servlet>
<display-name>MySer</display-name>
<servlet-name>MySer</servlet-name>
<servlet-class>com.santa.servlet.MyServlet</servlet-class>
<load-on-startup>30</load-on-
startup>
<<---------------------------------------- init the servlet when
server start
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/service/MySer</url-pattern>
</servlet-mapping>
so ,I want get the url-pattern in the method

@Override
public void init(ServletConfig arg0) throws ServletException {
System.out.println("*****************************");
// I want get the url-pattern here
System.out.println("*****************************");
super.init(arg0);
}

Thank you very much for your reply.

Who says there is one? There could be none at all, if load-on-startup
is turned on on the servlet entry. For that matter there could be
several distinct mappings configured.

The only time URL information is available to a servlet is when it's
processing a request. During that time the URL used for that request
is available to the servlet. Your container (eg., Tomcat or Weblogic)
may provide extensions that allow you more detailed access to the
application configuration, but the Java EE specification doesn't
require it.
 
S

santax

Who says there is one? There could be none at all, if load-on-startup
is turned on on the servlet entry. For that matter there could be
several distinct mappings configured.

The only time URL information is available to a servlet is when it's
processing a request. During that time the URL used for that request
is available to the servlet. Your container (eg., Tomcat or Weblogic)
may provide extensions that allow you more detailed access to the
application configuration, but the Java EE specification doesn't
require it.

So i can't get the url-pattern during init of servlet as your reply.
Then is there any way reading the web.xml directly??
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top