requestdispatcher problem

[nobr]Anyone have any idea whats going on over here? I am trying to use the requestdispatcher command in the servlet to forward the request to another servlet if the user name and password are good. if not then login failed error occurs.

Here is my code for CheckLoginServlet

package chapter3;

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.util.*;

publicclass CheckLoginServletextends HttpServlet{

privatevoid sendLoginForm(HttpServletResponse response,

boolean withErrorMessage)

throws ServletException, IOException{

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<HTML>");

out.println("<HEAD>");

out.println("<TITLE>Login</TITLE>");

out.println("</HEAD>");

out.println("<BODY>");

if (withErrorMessage)

out.println("Login failed. Please try again.<BR>");

out.println("<BR>");

out.println("<BR>Please enter your user name and password.");

out.println("<BR><FORM METHOD=POST>");

out.println("<BR>User Name: <INPUT TYPE=TEXT NAME=userName>");

out.println("<BR>Password: <INPUT TYPE=PASSWORD NAME=password>");

out.println("<BR><INPUT TYPE=SUBMIT VALUE=Submit>");

out.println("</FORM>");

out.println("</BODY>");

out.println("</HTML>");

}

publicvoid doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException{

sendLoginForm(response,false);

}

publicvoid doPost(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException{

String userName = request.getParameter("userName");

String password = request.getParameter("password");

if (userName!=null && password!=null &&

userName.equals("jamesb") && password.equals("007")){

RequestDispatcher rd = request.getRequestDispatcher("/servlet/WelcomeServlet");

rd.forward(request, response);

}

else{

sendLoginForm(response,true);

}

}

}

Now here is my WelcomeServlet code.

package chapter3;

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

publicclass WelcomeServletextends HttpServlet{

publicvoid doPost(HttpServletRequest request,

HttpServletResponse response)throws ServletException, IOException{

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<HTML>");

out.println("<HEAD>");

out.println("<TITLE>Welcome</TITLE>");

out.println("</HEAD>");

out.println("<BODY>");

out.println("<P>Welcome to the Bulbul's and Boni's Web Site.</P>");

out.println("</BODY>");

out.println("</HTML>");

}

}

My CheckLoginServlet and welcome servlet is in this directory

C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\myApp\WEB-INF\classes\chapter3

What is going on why is it not forwarding my request to the wellcome servlet? did i type something wrong while specifying the path in the requestdispatcher?

Thanks[/nobr]

[5765 byte] By [lrngjavaa] at [2007-11-27 5:56:50]
# 1
What IS happening now?
cotton.ma at 2007-7-12 16:28:11 > top of Java-index,Java Essentials,New To Java...
# 2

when i typed:

username: jamesb

password: 007

and hit the submit button this is what i get

type Status report

message /myApp/servlet/WelcomeServlet

description The requested resource (/myApp/servlet/WelcomeServlet) is not available.

rather than going to my WelcomeServlet page and printing

Welcome to the Bulbul's and Boni's web site. if the login is successful if not then error msg is send stating that the username and password is not correct

lrngjavaa at 2007-7-12 16:28:11 > top of Java-index,Java Essentials,New To Java...
# 3
Okay, so the forwarding is working.It's just the the servlet cannot be found. Can you get to the welcome servlet by putting a in path directly in your browser? If so then compare what you have with what the path is that you have redirected to and alter as needed.
cotton.ma at 2007-7-12 16:28:11 > top of Java-index,Java Essentials,New To Java...
# 4

and i have one more very important question. Lets say one of my servlet program runs good but i would like to make some changes on it, so after making changes i reload my servlet page but still the output is same.

Any idea why is this thing also happening? i am new to servlet but this is really painful.

lrngjavaa at 2007-7-12 16:28:11 > top of Java-index,Java Essentials,New To Java...
# 5

yes i can if i put the path

http://localhost:8080/myApp/servlet/chapter3.WelcomeServlet

but i get error which makes sense since i dont have a doGet method

message HTTP method GET is not supported by this URL

description The specified HTTP method is not allowed for the requested resource (HTTP method GET is not supported by this URL).

Message was edited by:

lrngjava

Message was edited by:

lrngjava

lrngjavaa at 2007-7-12 16:28:11 > top of Java-index,Java Essentials,New To Java...
# 6
I think your redirect line should look like thisRequestDispatcher rd = request.getRequestDispatcher("/servlet/chapter3.WelcomeServlet");In future you should probably be posting these question into the JSP and/or Servlet forum(s).
cotton.ma at 2007-7-12 16:28:11 > top of Java-index,Java Essentials,New To Java...
# 7

One suggestion is to use ServletContext's request dispatcher specifying a non-relative path. Of course, request's should work equally well.

Another is to use a named dispatcher, providing the name as specified in the deployment descriptor.

Message was edited by:

filestream

filestreama at 2007-7-12 16:28:11 > top of Java-index,Java Essentials,New To Java...
# 8

Ok Thankyou so much cotton.m, you are always very helpful. you are right but actually my path should look like this.

RequestDispatcher rd = request.getRequestDispatcher("chapter3.WelcomeServlet");

Now everything works fine, but this is a problem that everytime i make any changes in a working servlet program i had to stop my tomcat server and restart it all the time. this is very painful. any ideas if there is an easy way, if i make any changes i dont have to restart and stop my server all the time.

Secondly sorry for posting in this forum because jsp and servlet forums are really slow. i mean i had to wait 1 whole day just to find a simple solution. if i can't find a solution to it i can't move forward.

Thank you

lrngjavaa at 2007-7-12 16:28:11 > top of Java-index,Java Essentials,New To Java...
# 9

> Now everything works fine, but this is a problem that

> everytime i make any changes in a working servlet

> program i had to stop my tomcat server and restart it

> all the time. this is very painful. any ideas if

> there is an easy way, if i make any changes i dont

> have to restart and stop my server all the time.

Yeah. There is some sort of hotdeploy kind of option in Tomcat but it depends on the version and I don't really know how it works. Sorry.

I think it is possible, I just don't know how. But hopefully someone else does.

cotton.ma at 2007-7-12 16:28:11 > top of Java-index,Java Essentials,New To Java...