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

  • SEARCH
  • Home
  • 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 6683213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:47:42+00:00 2026-05-26T04:47:42+00:00

I am developing inherited jsp/java ee application and I would like to introduce Guice

  • 0

I am developing inherited jsp/java ee application and I would like to introduce Guice IoC container to my application. However, I found some obstacles. I can’t translate web.xml entries into guice registrations if there are more then one routing to single servlet using different urls. Problem is with init-parameters.

Here are some extracts from my web.xml:

This one is unrelated to problem, but it is good example how we are using init parameters. Basically it maps users with different roles in systems to appropriate pages.

<!-- LIST INTERNSHIPS SERVLET  -->
<servlet>
    <servlet-name>ListInternships</servlet-name>
    <servlet-class>pl.poznan.put.ims.controllers.ListInternships</servlet-class>
    <init-param>
        <param-name>CoordinatorPage</param-name>
        <param-value>WEB-INF/pages/coordinator/listinternships.jsp</param-value>
    </init-param>
    <init-param>
        <param-name>MentorPage</param-name>
        <param-value>WEB-INF/pages/mentor/listinternships.jsp</param-value>
    </init-param>
    <init-param>
        <param-name>AdministratorPage</param-name>
        <param-value>WEB-INF/pages/administrator/listinternships.jsp</param-value>
    </init-param>
    <init-param>
        <param-name>AllowedRoles</param-name>
        <param-value>Coordinator, Mentor, Administrator</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>ListInternships</servlet-name>
    <url-pattern>/internships</url-pattern>
</servlet-mapping>

Those two are the troublesome ones:

<!-- CHANGE PASSWORD SERVLET -->
<servlet>
    <servlet-name>ChangePassword</servlet-name>
    <servlet-class>myapp.controllers.ContextForwarder</servlet-class>
    <init-param>
        <param-name>SharedPage</param-name>
        <param-value>WEB-INF/pages/shared/password.jsp</param-value>
    </init-param>
    <init-param>
        <param-name>AllowedRoles</param-name>
        <param-value>*</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>ChangePassword</servlet-name>
    <url-pattern>/changepassword</url-pattern>
</servlet-mapping>

<!-- HELP SERVLET -->
<servlet>
    <servlet-name>Help</servlet-name>
    <servlet-class>myapp.controllers.ContextForwarder</servlet-class>
    <init-param>
        <param-name>SharedPage</param-name>
        <param-value>WEB-INF/pages/help/help.jsp</param-value>
    </init-param>
    <init-param>
        <param-name>AllowedRoles</param-name>
        <param-value>*</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>Help</servlet-name>
    <url-pattern>/help</url-pattern>
</servlet-mapping>

Here is my servlet:

 @Singleton
 public class ContextForwarder extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private final IUserDao dao;

    @Inject
    public ContextForwarder(IUserDao dao) {
      this.dao = dao;
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

            //trying to get rid of statics, using Ioc
            Validator.checkUserLoggedIn  (request);
            Validator.checkUserAuthorized(this, request);

            User currentUser        = UserManager.getCurrentUser(request);
            //pick matching page for user
            String userViewPage     = ServletUtils.getUserURL(this, currentUser, "Page");


            try {
                    dao.openSession();
                    dao.beginTransaction();
                    currentUser = UserManager.reloadCurrentUser(request, dao);

                    ServletUtils.forward(request, response, userViewPage);
                    dao.commit();
            }
            catch(ServletException e) {
                    dao.rollback();
                    throw e;
            }
            catch(Exception e) {
                    dao.rollback();
                    throw new ServletException(e);
            }
            finally {
                    dao.closeSession();
            }
    }
}

public class ServletUtils {
    public static void forward(HttpServletRequest request, HttpServletResponse     response, String location)
            throws ServletException, IOException {
            RequestDispatcher view = request
                    .getRequestDispatcher( response.encodeRedirectURL(location) );

            view.forward(request, response);
    }


    public static String getUserParameter(GenericServlet servlet, User user, String suffix) {
            return servlet.getInitParameter( user.getRoles() + suffix );
    }

    public static String getUserURL(GenericServlet servlet, User user, String suffix)
            throws ResourceNotFoundException {
            String URL = getUserParameter(servlet, user, suffix);

            if(URL == null) {
                    URL = servlet.getInitParameter("Shared" + suffix);
                    if(URL == null)
                            throw new ResourceNotFoundException(suffix);
            }

            return URL;
    }

    public static void redirect(HttpServletRequest request, HttpServletResponse response, String location)
            throws ServletException, IOException {
            response.sendRedirect( response.encodeRedirectURL(location) );
    }
}

When i try to translate it into guice (and then register this module):

public class MyServletModule extends ServletModule 
{

    @Override
 protected void configureServlets() {
    configureHelp();
    configurePassword();
 }

    private void configureHelp()
    {
    Map<String, String> params = new HashMap<String, String>();
    params.put("SharedPage", "WEB-INF/pages/shared/help.jsp");
    params.put("AllowedRoles", "*");

    serve("/help").with(ContextForwarder.class, params);
    }

    private void configurePassword()
    {
    Map<String, String> params = new HashMap<String, String>();
    params.put("SharedPage", "WEB-INF/pages/shared/password.jsp");
    params.put("AllowedRoles", "*");

    // it's routing correctly to servlet, but it uses params from first registration,
    // so that routing to jsp page is incorrect
    serve("/changepassword").with(ContextForwarder.class, params);
    }       
}      

The problem is that guice creates ContextForwarder servlet as a singleton with init parameters from the first registered method and then regardless of the request url it has parameters from the first registration. Is there any other solution to route user to different sites considering current user role? Is it possible to configure the same servlet class with two different configurations?

  • 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-26T04:47:43+00:00Added an answer on May 26, 2026 at 4:47 am

    I have found a solution, however I am not fully satisfied with it. I found out that, in solution without guice, web container (tomcat) creates two separate servlets using the same servlet class and injecting different init parameters. Guice by default restricts servlets to be singletons, so to copy default behaviour from web.xml solution I needed to find a way to create two instance of the same class and register it twice with different parameters. I solved this by creating two sub-classes to my servlet class, both with empty body, then I registered them with different parameters.

    This solution works, but it involve creating empty-body sub-classes which I am not satisfied with. It’s not a problem when I got two sub-classes, but with more of them code is becoming cumbersome.

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

Sidebar

Related Questions

Developing a heavily XML-based Java-application, I recently encountered an interesting problem on Ubuntu Linux.
I'm developing an Android application and i'm using a Sqlite database to store some
In a Linux embedded application I'm developing, there is the need to record some
Developing websites are time-consuming. To improve productivity, I would code a prototype to show
Developing a .NET WinForms application: how can I check if the window is in
Developing a project of mine I realize I have a need for some level
Developing a C# .NET 2.0 WinForm Application. Need the application to close and restart
When developing a desktop application in .NET, is it possible to not require the
I'm developing a VXML IVR application in Eclipse, and it's chock full of messy
I've inherited a C# window's application that I'm not real crazy about. I've got

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.