aspose file tools*
The moose likes Servlets and the fly likes Problem in send redirect Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem in send redirect" Watch "Problem in send redirect" New topic
Author

Problem in send redirect

Rashmi Pant
Greenhorn

Joined: Dec 19, 2007
Posts: 25
Hi all,
Please solve my problem.
I am testing a servlet which uses sendRediresct() method.
I have put my .html file(form1.html) insied C:\Tomcat5.0\webapps\servlets-examples folder code for this is

<html>
<head>
<title>New Page 1</title>
</head>
<body>

<form method="POST" action="testpackage/SendRedirectServlet">
<p>Enter your name<input type="text" name="username" size="20"></p>
<p>Enter your password  <input type="text" name="password"
size="20"></p>
<p><input type="submit" value="Submit" name="B1"></p>
</form>

</body>
</html>

and i put my servlet files inside testpackage,package(C:\Tomcat5.0\webapps\servlets-examples\WEB-INF\classes\testpackage)
code for these servlets are as

code for SendRedirectServlet.java is as:
----------------------------------------
package testpackage;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SendRedirectServlet extends HttpServlet{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String name = request.getParameter("username");
String password = request.getParameter("password");
if(name.equals("James")&& password.equals("abc")){
response.sendRedirect("/testpackage/ValidUserServlet");
}
else{
pw.println("you are not a valid user");
}
}
}
-------------------------------------------

And code for ValidUserServlet.java is as:
-------------------------------------------
package testpackage;


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ValidUserServlet extends HttpServlet
{

protected void doget(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
PrintWriter pw = response.getWriter();
pw.println("Welcome to Tis page " + " ");
pw.println("how are you");
}
}
--------------------------------------------------------------------------
After this i made following entry for web.xml file

<servlet>
<servlet-name>sendredirect</servlet-name>
<servlet-class>testpackage.SendRedirectServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>validuser</servlet-name>
<servlet-class>testpackage.ValidUserServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>sendredirect</servlet-name>
<url-pattern>/testpackage/SendRedirectServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>validuser</servlet-name>
<url-pattern>/testpackage/ValidUserServlet</url-pattern>
</servlet-mapping>


But when i am running this example as:
http://localhost:8080/servlets-examples/form1.html
It is displaying this .html page properly
and as coded in form1.html file it is showing field for entring name and password.
If i am entrying a wrong name and pass(except Name->James and password->abc) then it is calling the SendRedirectServlet correctly and shoeing the message as coded in SendRedirectServlet.
Now problem arises when i am inputing James for name and abc for password.
When i am inputing thes values i am getting following error message.
-------------------------------------------------------------------------

HTTP Status 404 - /testpackage/ValidUserServlet

--------------------------------------------------------------------------------

type Status report

message /testpackage/ValidUserServlet

description The requested resource (/testpackage/ValidUserServlet) is not available.


--------------------------------------------------------------------------------

Apache Tomcat/5.0.28

-------------------------------------------------------------------------

Please tell me what can be the problem.
Onething more i want to say is that i saperately make a check for ValidUserServlet in this case it is working properly.
I think problem is arising when i am callint it from SendRedirectServlet.
Please tell me the solution for this problem.
Thanks to you all
[ January 03, 2008: Message edited by: Bear Bibeault ]
Wayan Saryada
Ranch Hand

Joined: Feb 05, 2004
Posts: 104

Your code is redirecting your servlet to the root of your web container path. It's redirecting to http://localhost:8080/testpackage/ValidateUserServlet instead of http://localhost:8080/servlets-examples/testpackage/ValidateUserServlet. That's why you get error 404.

So you need to redirect your response to the correct context path. Use the request.getContextPath().


Website: Learn Java by Examples
Rashmi Pant
Greenhorn

Joined: Dec 19, 2007
Posts: 25
Originally posted by I Wayan Saryada:
Your code is redirecting your servlet to the root of your web container path. It's redirecting to http://localhost:8080/testpackage/ValidateUserServlet instead of http://localhost:8080/servlets-examples/testpackage/ValidateUserServlet. That's why you get error 404.

So you need to redirect your response to the correct context path. Use the request.getContextPath().



Yes exactly this is happining but please tell me what change i should made so that i can run this example properly
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

One way is you can append your application context root in the action attribue.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60056
    
  65

Please see this JSP FAQ entry.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in send redirect
 
Similar Threads
Stuck on HelloWorldServlet
Servlet
prb from Hemant Deshmukh...
Doubt on calling java class in Servlet
servlet-jdbc - internal servlet error (error:500)