Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Second call to servlet from a jsp not working .....

Status
Not open for further replies.

niblet

Programmer
Aug 7, 2001
30
US
I am running jsp in jakarta/tomcat as a stand alone and am having the following problem.

I have a form that has the following tag
<FORM name='NewSelections' method=&quot;post&quot; action=&quot;/servlet/com.servlets.MainController&quot;


the doPost in the servlet lands on the following actions

doPost(r,r)
{ ... do things

String nextPage = &quot;customer.jsp&quot;

ServletContext context = getServletContext();
RequestDispatcher rd = context.getRequestDispatcher(nextPage);
rd.forward(request, response);
}


The flow is I'm in the customer.jsp, as a result of a submit, I want to do some work in the doPost of the servlet and go right back to the customer.jsp page with an adjusted set of data. so its customer.jsp, using action /servlet/com.servlets.MainController and the MainController forwarding back to the customer.jsp. That is the flow ...

The problem is, I think maybe in the jarkarta configuration based on the following evindence.

when the customer.jsp is in its docs subdirectory the attempt to call the servler results in an error that says

docs/servlet/com.servlets.MainController not found. So it starts looking for the servet in the sub directory of the .jsp files instead of in the classes directory under web-inf

if I move the customer.jsp file to the top of the application directory so its in /%catalina_home%/webapps/dispatch (where dispatch is the name of the application) then I can execute the servlet on submit, once.

when more changes are made to the form and I hit submit the second time. I have the same problem where the servlet is not found and the path is like this

dispatch/servlet/servlet/com.servlets.MainController resource not available.

another interesting note is that after the forward from the servlet, although I am in the form, the url in the browser says

it doesn't say customer.jsp as I expected.

my web.xml is configured to have this one servlet like this

<servlet>
<servlet-name>MainController</servlet-name>
<servlet-class>com.webware.servlets.MainController</servlet-class>
</servlet>

which seems pretty straight forward.

I think I'm missing something really small either in tomcat configuration or in the foward or something. I'm not sure what...

can anyone help, I need to solve this really really quickly ...

thanks
niblet
 
To do a forward as you are doing, you need to have the jsp page in the same context as the servlet (e.g. /servlets/customer.jsp). You should be doing this:

RequestDispatcher rd = getServletContext.getRequestDispatcher(&quot;/customer.jsp&quot;);
rd.forward(request,response);

Note that the forward is invisible to your browser so it is not going to show the URL of the page you are on. As far as it knows, the servlet is the last page it hit. If you want the browser to know which page it is on (not necessary), you have to do a redirect, not a forward.
 
Hi,

Thanks for the response, I tried your suggestion with the .jsp addressed with the slash, I still get the same problem. Any other thoughts anyone ?

BTW, thanks for the tip on the foward and re-direct, I did not know that. If I am sharing a bean in a request though, I need to use the foward correct ?

thanks
niblet
 
Also, you don't need to give the complete package name of your servlet. You provide this mapping in the web.xml file. YOu should only need to do:

/servlet/MainController

People often seem to have problems with the concept of an application context. Remember, the container(Tomcat) expects paths to be relative to the root of the context that you are in. So, if you are in the dispatch context, all pathing is relative to:


The files in the root of your webapp should be referenced relative to this:


but they should reference each other RELATIVE to this context:

MainController should point to /customer.jsp
customer.jsp should point to /servlet/MainController.jsp

The servlet container always finds stuff RELATIVE to your current context.

BTW, which directory is your customer.jsp in?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top