Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Web.xml Question - Map Servlet

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to map a name the user can type into the address bar to take a specific action.
For instance, I have the following servlet tag:
<servlet>
<servlet-name>controller</servlet-name>
<servletclass>
com.qs.pro.controller.ControllerServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
So, when the user types in:
http://mysite/mycontext/controller
it executes the specified controller. Now, is there any way to make ANOTHER mapping that will let the user type in:
http://mysite/mycontext
and have it execute like this:
http://mysite/mycontext/controller?key=value
A.k.a., I want my landing page to be my servlet, but I want it automatically called with a parameter.
Any mapping guru's out there got any tricks for me?
Jason
 
Jason Stortz
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A simpler way to say this is I want type
http://mysite/mycontext
and see the result of
http://mysite/mycontext/myservlet?someKey=someVal
Thanks guys and gals,
-Jason
 
Author
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the simplest ways is to create a Filter and map it to <url-pattern>/</url-pattern>. This filter will be invoked when you use http://mysite/mycontext. Within the filter you can
either use a Query String as getRequestDispatcher(/servlet/myServlet?someKey=someValue)
or use an HttpServletRequestWrapper that returns someValue for someKey
and then forward the request to myservlet.
As a bonus, you dont have to hardcode the values someKey=someValue in the filter code. You can pass as many of them as required as init-parameters in web.xml
HTH
-j
 
Not so fast naughty spawn! I want you to know about
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic