Solved

Servlet controller and RequestDispatcher

Posted on 2006-06-28
5
364 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jboss 7.1 start up error 1 60
Chrome and Firefox Java 5 67
Notify sent to other threads in Java 9 41
Java Eclipse Loop 3 29
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

735 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