problem forwarding from servlet to JSP
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Arjan_Jonker
Posted On:   Wednesday, June 12, 2002 12:35 AM

testPath.html I have the following two files: testPath.jsp testPath TestRedirect .. in the tomcat/webapps/myproject/ directory and: TestRedirect.java public class TestRedirect extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { gotoPage("/testPath.jsp", request, response); } private void gotoPage(String address, HttpServletRequest request, HttpServletResponse response) throws IOException, Ser   More>>
testPath.html

I have the following two files:

testPath.jsp

			






testPath



TestRedirect



.. in the tomcat/webapps/myproject/ directory and:


TestRedirect.java

			

public class TestRedirect extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

gotoPage("/testPath.jsp", request, response);

}

private void gotoPage(String address, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address);
if (dispatcher==null)
throw new ServletException("Dispatcher==null! address: "+address);
dispatcher.forward(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}

}

in the tomcat/webapps/myproject/WEB_INF/classes/myproject.jar directory

Now when I first load the HTML page, the url of the link is:

http://localhost/myproject/servlet/nl.inbitween.cms.servlets.TestRedirect

Everyting goes fine, but when the JSP page reloads, the url of the link has changed to:
http://localhost/myproject/servlet/servlet/nl.inbitween.cms.servlets.TestRedirect

This is not good. How do I remove the second /servlet directory from the new url?

   <<Less

Re: problem forwarding from servlet to JSP

Posted By:   michael_dean  
Posted On:   Wednesday, June 12, 2002 07:35 AM

You have a little typo in the URL for the servlet given in the JSP. Since an "unregistered" servlet's URI is always /servlet/fully.qualified.ServletName, always refer to them with a URI that is relative to the servlet context (application root) (i.e. it starts with "/"). In other words, change the line to:




TestRedirect


You can also register a servlet (give it a name in web.xml and/or provide a new mapping for it. See Re: Problem of servlet mapping for more information on registering and/or mapping servlets, and the options for accessing them after doing so.

Re: problem forwarding from servlet to JSP

Posted By:   Bosun_Bello  
Posted On:   Wednesday, June 12, 2002 04:46 AM

In the href on testPath.jsp, try taking off the servlet from the path. The container adds 'servlet' by default, and that is probably why it's showing up twice.
About | Sitemap | Contact