aspose file tools*
The moose likes Servlets and the fly likes Servelet Request Redirect Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servelet Request Redirect" Watch "Servelet Request Redirect" New topic
Author

Servelet Request Redirect

Shubham Guptas
Greenhorn

Joined: May 02, 2010
Posts: 9
Hi Ranchers,

I created the servlet which is running on following URL

http://server-ip/TestService/

and it is working fine.


but my problem is I am getting all the requests to URL

http://server-ip/EvaluateService (It should perform same function as that of servlet which I created)


and my tomcat webapps folder should not contain "EvaluateService" directory.

and if I am getting any request on URL "http://server-ip/EvaluateService"
it should redirect automatically to "http://server-ip/TestService/"

Please help me that how can I configure my tomcat for this. I don't think any changes in servlet code will be required.

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61773
    
  67

Just add another mapping to the deployment descriptor.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Shubham Guptas
Greenhorn

Joined: May 02, 2010
Posts: 9
Bear Bibeault wrote:Just add another mapping to the deployment descriptor.


thanks for your response.


but this won't work. I am not having directory in webapps as "EvaluateService"

so where to describe deployment descriptor. Please elaborate.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61773
    
  67

Servlet mappings have nothing to do with directories. If you want two URLs to go to the same servlet, just add two mappings to the servlet.
Shubham Guptas
Greenhorn

Joined: May 02, 2010
Posts: 9
Bear Bibeault wrote:Servlet mappings have nothing to do with directories. If you want two URLs to go to the same servlet, just add two mappings to the servlet.




my servlet is deployed in "TestService" directory. web.xml of this servlet will be accessed only if my URL pattern is


http://server-ip/TestService/

and through URL mapping I can change it to

http://server-ip/TestService/a

or

http://server-ip/TestService/b

or

http://server-ip/TestService/c


but "http://server-ip/TestService/" is mandatory as it is inside "Test Service" directory


but my incoming requests are in pattern "http://server-ip/EvaluateService/" and I don't want to have "EvaluateService" directory inside webapps.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61773
    
  67

One more time: servlet mappings have NOTHING to do with directories. Nothing. Nada. Zero. Zilch.

You can map the servlet to ANY path you want, and there are no directories with the same name. None. Zero.

For example:


This maps the servlet to both ralph/* and henry/*.

And there are NO directories named ralph or henry anywhere.

Once again: servlet mappings have nothing to do with directories. Nothing.
Shubham Guptas
Greenhorn

Joined: May 02, 2010
Posts: 9
Bear Bibeault wrote:One more time: servlet mappings have NOTHING to do with directories. Nothing. Nada. Zero. Zilch.

You can map the servlet to ANY path you want, and there are no directories with the same name. None. Zero.

For example:


This maps the servlet to both ralph/* and henry/*.

And there are NO directories named ralph or henry anywhere.

Once again: servlet mappings have nothing to do with directories. Nothing.



I am agreed that <servlet-mapping> is nothing to do with directories. okay....

one more doubt the above will be stored in web.xml and which will be stored in webapps inside some directory say "TestService"

the above web.xml will be only read when the URL will contain /TestService/

how I can configure servelet to read web.xml with out having /TestService/ in my URL?
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

Servlet doesnt read web.xml, it is used by web container, web.xml doesnt go in any arbitrary directory, it must be inside WEB-INF directory. Read some basic servlet tutorials.
See for more details on web application directory structure and this servlet example


[Servlet tutorial] [Servlet 3.0 Cook Book]
Shubham Guptas
Greenhorn

Joined: May 02, 2010
Posts: 9
sudhir nim wrote:Servlet doesnt read web.xml, it is used by web container, web.xml doesnt go in any arbitrary directory, it must be inside WEB-INF directory. Read some basic servlet tutorials.
See for more details on web application directory structure and this servlet example




ya i know that it will be in web.inf directory and web.inf will be in some directory say the name as "TestService".

now the web.xml will be only read if URL contains "TestService"

so through servlet-mapping it can be modified as

http://sever-ip/TestService/****

where the grenn color will be mandatory and red color portion we can change using servlet-mapping.

sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

web.xml being read by container has nothing to do with the URL weather it's TestService or any thing else. It will be used by container. You don't need to worry about when container reads it.
And the URL your are mentioning "http://sever-ip/TestService/" Here /TestService is called context path and you cant modify it using web.xml. I would still advice your to read basic guides on java web development, and get some ideas about things like, context path, url patterns, servlet mappings etc.
Shubham Guptas
Greenhorn

Joined: May 02, 2010
Posts: 9
sudhir nim wrote:web.xml being read by container has nothing to do with the URL weather it's TestService or any thing else. It will be used by container. You don't need to worry about when container reads it.
And the URL your are mentioning "http://sever-ip/TestService/" Here /TestService is called context path and you cant modify it using web.xml. I would still advice your to read basic guides on java web development, and get some ideas about things like, context path, url patterns, servlet mappings etc.



so I want to modify context path. I can do that by using <context> element in server.xml . OK done!




now i can access my service from http://server-ip/EvaluateService

and also from http://server-ip/TestService

Okkk Done...!



now the problem is tomcat creates two separate instances ie one for EvaluateService and one for TestService

I noticed this because i created hit counter to test this. and after I access TestService twice (refresh) the counter value 2 is displayed. now If I access EvaluateService the counter value is 1.


now if I access TestService again it display 3.

that is it is creating two separate instances.

How can I avoid the creation of these two separate instances.

--
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

Redirect all requests for /EvaluateService to /TestService. You can use URLRewrite filter to redirect all urls of /EvaluateService to /TestService. URLRewrite filter supports apache .htaccess type rules. Check this link for how you can redirect entire domain to another one using htaccess, You have to use these rules for urlrewrite filter

http://enarion.net/web/htaccess/migrate-domains/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servelet Request Redirect