Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Servlet controller and RequestDispatcher

Posted on 2006-06-28
5
Medium Priority
?
371 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1200 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

721 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