简体   繁体   中英

How to access servlet attribute in GWT server side code?

I've assembled my GWT app as war file which I am running in embedded Jetty-

String confFile = System.getProperty("configFilename");
config = new XMLConfiguration(configFilename);
Server server = new Server(8080);
WebAppContext webapp = new WebAppContext();
webapp.setAttribute("config", config);      
webapp.setContextPath("/");
webapp.setWar("file.war");
server.setHandler(webapp);
server.start();
server.join();

I am trying to access my "config" object in GWT server side code-

public class MyServiceImpl extends RemoteServiceServlet implements
        MyService {

    config = (XMLConfiguration) this.getThreadLocalRequest().getAttribute("config");

}

Here, config is always null.

What am I doing wrong? I've tried config =(XMLConfiguration) this.getServletContext().getAttribute("config"); but that doesn't work too - I get error-

org.apache.commons.configuration.XMLConfiguration cannot be cast to org.apache.commons.configuration.XMLConfiguration

You have to get the attribute from the ServletContext instead of from the HttpServletRequest

Try this inside your RPC method:

public class MyServiceImpl extends RemoteServiceServlet implements MyService {

  @Override
  public void MyMethod() {
     this.getThreadLocalRequest()
         .getSession().getServletContext()
         .getAttribute("config");
  }

}

Try adjusting your maven dependencies to explicitly include the jar containing the XMLConfiguration class and marking it as provided. This will remove the duplicate in your classpath and resolve the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM