• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Writing a servlet controller

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am trying to write a controller servlet that redirects to JSP, HTML or to another servlet by looking at the request URL. It also has to handle form submits and process it before forwarding.
The servlet maaping as:

This creates a problem. If a redirection is done by line-4 (in below code) then as the redirected URL has /guest, the request is again caught by Controller and again a forward happens, this creates an infinite loop. The resources have to be under the "guest" folder

This is the code use for redirection:

Is there a quick and dirty way to put together such a controller servlet?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To avoid lots of trouble, master the requirements of the Servlet API and Servlet container use of web.xml. For example:


Is a bad idea - ALL Java classes used in servlets should be in a package because of the way the JVM finds classes.

See this FAQ entry.

Bill
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And you are not redirecting, you are fowarding. Be sure to use the proper term as a redirect and a forward are very very different things.

Also, be sure to use a context relative path to the forwarded resource. For example, not "xyz.jsp" but "/WEB-INF/views/xyz.jsp" or some such.
 
Reehan Lalkhanwar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some of the details were removed for brevity. The servlet-class has a fully qualified packaged class and the JSP referred are using context relative path request.getSession().getServletContext().getRealPath(path).
The forwarding is being done in if part there are other such blocks that forward / redirect to different HTML, JSP, Servlet like:

Either redirect or forward, it comes back to the same servlet (as web.xml makes all /guest to be passed to the Controller) and ends up in an infinite loop.
 
Would you like to try a free sample? Today we are featuring tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic