Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in send redirect

 
Rashmi Pant
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
IntelliJ IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Rashmi Pant
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way is you can append your application context root in the action attribue.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see this JSP FAQ entry.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic