1

I am working on a mixed situation where i am using Servlets & Struts2.

I am calling a HTTPServlet and where i am processing on data.

Then I need to make a call to struts API to insert data in DB.

In that i am using HTTPSession also.

So I am calling

ServletActionContext.getRequest()

To get HttpRequest and then session in that struts action class.

Struts Action class is getting called by Servlet.

But

ServletActionContext.getRequest()

always returns NULL.

Is it because that it is not getting called by web.

Arne Evertsson
  • 19,693
  • 20
  • 69
  • 84
Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96

1 Answers1

5

First, ServletActionContext and ActionContext both use a ThreadLocal to store per-request objects (such as the request and response). This is handled by Struts2. If you attempt to call those from a Servlet, they'll return null, since the request was routed to your servlet, not to Struts2 (and thus Struts2 did not create an action context for the request).

With that said, Struts2 is a higher level abstraction over the Servlet API. The fact that you are invoking a Struts2 action from within a servlet sounds really wrong.

If you need to perform some common process in both a servlet and an action, then create a separate class to handle the process (i.e., inserting data in a database) and then call that class from both your servlet and your action.

If that isn't what you're trying to do, then please provide more details, along with an example of your servlet and action code.

Steven Benitez
  • 10,936
  • 3
  • 39
  • 50
  • Hi Steven.. Thanks a ton for clarifying my concepts. I am going to post my scenario why i was calling struts from servlet. – Hardik Mishra Feb 08 '11 at 05:56
  • I am working on Smartgwt & Struts2. I am using struts2 as a controller and smartgwt as front end where i am generating dynamic pages. I have a situation where i need to generate response back to my client browser to notify abount finished task. But, I am not able to do that using Struts2 as I do not have any page name where i can redirect using Struts2. So, i have used Servlet to generate response. – Hardik Mishra Feb 08 '11 at 06:02
  • @HardikMishra Go ahead and use struts2 httpheader result, so that you can only return header, no page required. I always use it for my ajax requests which don't return any page. http://struts.apache.org/release/2.3.x/docs/httpheader-result.html – coding_idiot Apr 21 '13 at 01:33
  • @Steven I'm also getting NullPointerException in the interceptor, but it was working fine with struts2.1.8 The error started coming when I upgraded to struts2.3.12 (just went ahead and changed version in POM.xml) – coding_idiot Apr 21 '13 at 01:35
  • @XCoder: Thanks but this thread is more than 2 years old I no longer work with Struts2 + GWT.. :). – Hardik Mishra Apr 22 '13 at 03:18
  • @XCoder If you have a question, then post a separate question, rather than asking for help in a comment of an old, resolved question. – Steven Benitez Apr 22 '13 at 16:05