Answer the question
In order to leave comments, you need to log in
Explain the use of the RequestDispatcher interface in JAVA?
Good afternoon! There was a question about the RequestDispatcher interface! This interface allows you to redirect requests from the receiving servlet to some other one! Code example:
String param = request.getParameter("param");
if(param.equals("enter")){
RequestDispatcher rD = request.getServletContext("/AnotherServlet");
rD.forward(request,response);
}
Answer the question
In order to leave comments, you need to log in
If the forward method is declared in the RequestDispatcher interface, it can already be called. If the object returned by the request.getServletContext method implements the RequestDispatcher interface, then it has some kind of implementation. Another question is whether this implementation does what you want it to do.
https://ideone.com/6wVcrt
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
static interface Foo {
public void foo();
}
static class Bar implements Foo {
@Override
public void foo() {
System.out.println("Bar");
}
}
static class Qux implements Foo {
@Override
public void foo() {
System.out.println("Qux");
}
}
public static void main (String[] args) throws java.lang.Exception
{
Foo bar = new Bar();
Foo qux = new Qux();
bar.foo();
qux.foo();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question