Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Servlet controller and RequestDispatcher

Posted on 2006-06-28
5
Medium Priority
?
372 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 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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:
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…
Suggested Courses

824 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