Solved

Servlet controller and RequestDispatcher

Posted on 2006-06-28
5
357 Views
Last Modified: 2013-11-24
Hi Experts,

I am trying to control all users' requests by one servlet and then it passes it to the next servlet based on the request. However, I only get blank page instead of welcome page if I request from  login page.

This is what I did.
+++++++++++++++++++++

login.html

...
<FORM ACTION="RegistrationController" METHOD="POST">
...
...
++++++++++++++++

public class RegistrationController extends HttpServlet {
   
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, java.io.IOException {
               
                RequestDispatcher rd = request.getRequestDispatcher("/LoginServlet");
                rd.include( request, response ) ;  
...
...
++++++++++++
public class LoginServlet extends HttpServlet {
   
   
     protected void doGet(HttpServletRequest request,
             HttpServletResponse response)
             throws ServletException, java.io.IOException {
         response.sendRedirect("/AS4/welcome.html");
....
...
++++++++++++
      <servlet>
            <servlet-name>RegistrationController</servlet-name>
            <servlet-class>RegistrationController</servlet-class>
      </servlet>
      <servlet>
            <servlet-name>LoginServlet</servlet-name>
            <servlet-class>LoginServlet</servlet-class>
      </servlet>
      
      <servlet-mapping>
           <servlet-name>RegistrationController</servlet-name>
           <url-pattern>/RegistrationController</url-pattern>
        </servlet-mapping>

        <servlet-mapping>
           <servlet-name>LoginServlet</servlet-name>
           <url-pattern>/*</url-pattern>
        </servlet-mapping>
+++++++++++

thanks in advance
0
Comment
Question by:dkim18
  • 3
  • 2
5 Comments
 
LVL 10

Expert Comment

by:mukundha_expert
ID: 17003331
if you are getting the correct URL on the browser then try including or forwarding to the welcome.html  instead of redirecting it in LoginServlet

0
 

Author Comment

by:dkim18
ID: 17003441
I don't think I am getting correct url. This is url that I got:

http://localhost:8080/AS4/RegistrationController

and acutally, there is error message on the screen that says:

The requested resource (/AS4/LoginServlet) is not available

0
 
LVL 10

Accepted Solution

by:
mukundha_expert earned 300 total points
ID: 17003568
try,

public class RegistrationController extends HttpServlet {
   
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, java.io.IOException {
               
                RequestDispatcher rd = request.getRequestDispatcher("/LoginServlet");
               ////////************ rd.include( request, response ) ;  

              rd.forward(request,response) ;

...
...



>>>>>>>>>>>> use system.out.println() in both the servlets, you can easily find where it is going wrong. rd.include() did not work. coz include includes the output of the other servlet into this. forward() gives the control to the new servlet.

check,

http://www.brics.dk/~amoeller/WWW/servlets/dispatch.html


0
 
LVL 10

Expert Comment

by:mukundha_expert
ID: 17003581
also try changing this,

<servlet-mapping>
           <servlet-name>LoginServlet</servlet-name>
           <url-pattern>/LoginServlet</url-pattern>
        </servlet-mapping>
0
 

Author Comment

by:dkim18
ID: 17003643
Yes, it worked. I didn't even have to change servlet mapping. Thanks!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now