Solved

Servlet controller and RequestDispatcher

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

932 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

9 Experts available now in Live!

Get 1:1 Help Now