• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing a servlet controller

 
Reehan Lalkhanwar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • 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?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13077
6
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic