Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6780949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:34:00+00:00 2026-05-26T16:34:00+00:00

Hello i have a question about servlet calling another servlet I have a main

  • 0

Hello i have a question about servlet calling another servlet

I have a main servlet called Relay which is going to be responsible to control the other servlets

the user will click on and will be forwarded to Relay servlet

<li><a href="/oosd/Relay?selectAnimal=SelectAnimalServlet">Check the available animals</a> </li>

inside Relay servlet will get the value of the parameter to determine which servlet is going to run

String selectAnimal = request.getParameter("selectAnimal");

if (selectAnimal.equals("SelectAnimalServlet")){
            getServletContext().getNamedDispatcher("/SelectAnimalServlet") 
            .forward(request, response); 
            //for testing
            System.out.println("Request forwarded to " + selectAnimal + " servlet");
        }

SelectAnimalServlet code:

try
{     
    HttpSession session = request.getSession(true);
    session.getAttribute("currentSessionUser");
    List<AnimalSelectBean> beans = DAO.getAnimalList(); 
    request.setAttribute("beans", beans); 
    request.getRequestDispatcher("CheckAnimal.jsp").forward(request, response); 


}

Now when i run that it’s not working for some reason, if i change the link to SelectAnimalServlet directly the code works any idea how to solve this ?

Edit:

Here is my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>content.LoginServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>UpdateAnimalServlet</servlet-name>
    <servlet-class>content.UpdateAnimalServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>SelectAnimalServlet</servlet-name>
    <servlet-class>content.SelectAnimalServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>Relay</servlet-name>
    <servlet-class>content.Relay</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>SelectAnimalServlet</servlet-name>
    <url-pattern>/SelectAnimalServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>UpdateAnimalServlet</servlet-name>
    <url-pattern>/UpdateAnimalServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Relay</servlet-name>
    <url-pattern>/Relay</url-pattern>
  </servlet-mapping>
</web-app>

also I changed:

getServletContext().getNamedDispatcher("/SelectAnimalServlet") 
                .forward(request, response); 

to:

response.sendRedirect(response.encodeRedirectURL(selectAnimal));

and still the same thing blank webpage with http://localhost:8080/oosd/Relay?selectAnimal=SelectAnimalServlet link

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:34:00+00:00Added an answer on May 26, 2026 at 4:34 pm

    getNamedDispatcher expects a servlet name; you’re providing it with a servlet URL.

    Either use the name, or use getRequestDispatcher with the URL.

    Since you’re forwarding, the URL will not change–there is no redirect response sent back to the browser on a forward. The contents of the forward are written directly to the original response.

    Now that you’re forwarding, you need to redirect to the URL, not just the name of the servlet.

    What does the servlet you redirect to do for output?

    I don’t believe your parameter naming convention makes any sense. The parameter shouldn’t be named the same as a servlet name; the parameter should be something like “command” or “select”. You would then use the command parameter value to look up the URL of the servlet. Or, in your case, just prepend a /, and you’re done. There’s no need to do any if/else comparisons.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I have question about android spinner I have spinner which is populated by
Hello I have question about android spinner I have spinner which is populated by
I have a question about type casting. I have the following JSON String: {server:clients,method:whoIs,arguments:[hello]}
I have a question about the source-code binary on Windows. #include <stdio.h> int main()
I have a question about the images displaying using a function getImage_w($image,$dst_w), which takes
Hello fellow drupalers, I have a question about ajax loaded views. Lets say i
Hello all,I'm a junior on html,I have a question about css position : first
Hello everybody Well my question is about sql commands... If I have 2 tables
hello I have some question about writing class in Java, why this one is
Hello i have some question about java. here is my code: public static void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.