File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Error using forward method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Error using forward method" Watch "Error using forward method" New topic
Author

Error using forward method

clyde melly
Ranch Hand

Joined: Sep 04, 2003
Posts: 152
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ForwardServlet extends HttpServlet
{
public void doPost (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
getServletContext().getRequestDispatcher("login.html").forward(req, res);
}

}
The html file which call's this servlet
<html>
<body bgcolor="#ccccff">
<form action="http://localhost:8080/servlet/ForwardServlet" method="post"/>
<input type="submit" value="OK"/>
</body>
</html>
The init method is called,but an error page is displayed.I'm using jsdk2.0 to test.Plz help
I also tried http://localhost:8080/servlet/ForwardServlet,but error page with init being called.
Amit KumarS
Ranch Hand

Joined: Oct 10, 2003
Posts: 100
hiiii,
i think the problem is with the way u r calling the login.html... it has to be absolute path.. if not then you will have to use the request object to call the getRequestDispatcher() in which you can pass relative path but in that case u will have to call it like

i hope this is clear.. if still in doubt please revert back
Thanks
Amit


****************************<br />In 24 hrs Earth rotates once on its Axis.
clyde melly
Ranch Hand

Joined: Sep 04, 2003
Posts: 152
Hi Amit,I will do the needful and get back to u.Thanx
clyde melly
Ranch Hand

Joined: Sep 04, 2003
Posts: 152
Amit still an error page displayed.I'm using jsdk2.0 with the servletrunner utility.No problem with that na.Kindly help.
Malli Raman
Ranch Hand

Joined: Nov 07, 2001
Posts: 312
Originally posted by clyde melly:
Amit still an error page displayed.I'm using jsdk2.0 with the servletrunner utility.No problem with that na.Kindly help.

Hi Clyde Melly,
Following code is working in weblogic server 7.1. As Amit said you missed '/' path in the login.html file.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ForwardServlet extends HttpServlet
{
public void doPost (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
getServletContext().getRequestDispatcher("/login.html").forward(req,res);

}
}
Regards,
M.S.Raman
clyde melly
Ranch Hand

Joined: Sep 04, 2003
Posts: 152
So does jsdk2.0 not support the given forward method in the given code??Will i have to upgrade??
praveenkumar singamsetty
Greenhorn

Joined: Oct 29, 2003
Posts: 6
Hi...
U should take care of the context root also..
r both the servlets in the same context root
if they exist then u can use simple name
else u should specify the rootname n simplename
clyde melly
Ranch Hand

Joined: Sep 04, 2003
Posts: 152
The code will work with jsdk2.0 or not??
praveenkumar singamsetty
Greenhorn

Joined: Oct 29, 2003
Posts: 6
getServletContext().getRequestDispatcher("login.html").forward(req, res);
The code will certainly work...
but make sure of the context root
in getRequestDispatcher specify it as("UrOwnContextRoot/login.html");
try it out...
it worked 4 me...
clyde melly
Ranch Hand

Joined: Sep 04, 2003
Posts: 152
Not get an error that the requestdispatcher stuff is not found within the servletcontext interface.I set the classpath again by putting jsdk.jar and servlet.jar in my existing classpath.Again just to confirm jsdk2.0 supports this feature??
Malli Raman
Ranch Hand

Joined: Nov 07, 2001
Posts: 312
Originally posted by clyde melly:
Not get an error that the requestdispatcher stuff is not found within the servletcontext interface.I set the classpath again by putting jsdk.jar and servlet.jar in my existing classpath.Again just to confirm jsdk2.0 supports this feature??

Hi Clyde,
Just try to print the RequestDispatcher object (I think you are getting null for this one).
Else you can try this option
<------------------------------->
ServletContext context = null;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
context = config.getServletContext();
}
inside your doGet method initialize the RD as

RequestDispatcher rd = context.getRequestDispathcer();
<------------------------>
Regards,
M.S.Raman
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

So does jsdk2.0 not support the given forward method in the given code??Will i have to upgrade??

I cannot express how strongly I recommend that you upgrade.

Tomcat 4.1.29 is the latest stable release of the implementation of a servlet container that replaced (a LONG time ago) the jsdk2.0

Find it at: http://jakarta.apache.org/tomcat


Also... you should not have jsdk2.0 and servlet.jar *both* in the classpath, that is probably confusing the issue.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error using forward method