Note that there are some explanatory texts on larger screens.

plurals
  1. POguice servlet is not a singleton
    text
    copied!<p>something awful is happening</p> <p>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.</p> <p>here is the 2nd servlet code</p> <pre><code>@Singleton @WebServlet("/Medical_Web") public class XXXDetailsServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Inject private Provider&lt;XXXPersistenceManager&gt; persistenceManager; @Inject private Provider&lt;XXXChain&gt; 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&lt;XXXBean&gt; xxxList = new ArrayList&lt;XXXBean&gt;(); xxxList.add(xxxBean); chain.process(xxxList); } catch (Exception ex) { ex.printStackTrace(); } } } </code></pre> <p>now - here's whats getting me</p> <p>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).</p> <p>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</p> <p>(sorry about all the XXX's, don't know if i can post actual names)</p> <hr> <p>here's the rest of my guice setup</p> <pre><code>public class XXXServletContextListener extends GuiceServletContextListener { @Override protected Injector getInjector() { return Guice.createInjector(new XXXUploadModule(), new XXXServletModule()); } } </code></pre> <p>and here's the servlet module</p> <pre><code>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 } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload