Question

something awful is happening

i have 2 servlets in my project - one of them simply has a post method and is responsible for handling file uploads. i recently added the other one - it has a get and a post method.

here is the 2nd servlet code

@Singleton
@WebServlet("/Medical_Web")
public class XXXDetailsServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Inject
    private Provider<XXXPersistenceManager> persistenceManager;

    @Inject
    private Provider<XXXChain> chainProvider;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        System.out.println("servlet address = " + this);
        final String xxx= request.getParameter("xxx");
        String json = "";
        try {
            final XXXBean xxxBean = persistenceManager.get().find(xxx);
            json = new GsonBuilder().create().toJson(xxxBean);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        request.setAttribute("json", json.trim());
        getServletContext().getRequestDispatcher("/XXX.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        System.out.println("servlet address = " + this);
        final String xxx = request.getParameter("xxx");
        try {
            final XXXChain chain = chainProvider.get();
            chain.getContext().setAttribute(XXX_TYPE, XXXType.DELETE);
            final XXXBean xxxBean = persistenceManager.get().find(xxx);
            final List<XXXBean> xxxList = new ArrayList<XXXBean>();
            xxxList.add(xxxBean);
            chain.process(xxxList);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

now - here's whats getting me

for some reason - even though this is marked as a @Singleton - the servlet addresses are definitely coming back as different. I noticed this initially when I hit a null pointer in my post method - whenever i call the get method, the instance of the servlet i get back has all the fields populated. whenever i call the post method, the instance of the servlet i get back (it is a different instance) does not have the fields populated (just null, seems like they didn't get injected).

I'm really struggling to figure out what's going on here. it seems as if an instance of this servlet was created outside of the guice context. if it matters - we are using JBoss 7.1

(sorry about all the XXX's, don't know if i can post actual names)


here's the rest of my guice setup

public class XXXServletContextListener extends GuiceServletContextListener {

    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new XXXUploadModule(), new XXXServletModule());
    }
}

and here's the servlet module

public class XXXServletModule extends ServletModule {

    @Override
    protected void configureServlets() {
        serve("/xxx1").with(XXXDetailsServlet.class); // this one fails on post
        serve("/xxx2").with(XXXUploadServlet.class);  // this one works 
    }
}
Was it helpful?

Solution

I am not familiar with how Guice servlet integration works, but having the @WebServlet("/Medical_Web") annotation means that your web container will also instantiate that servlet to serve requests. A pool of them actually, it doesn't have a concept of singletons.

My guess is you just have to remove the annotation and let ServletModule control the servlet life-cycle.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top