-2

I am coding for my changes from Struts1 to Struts2. In this we find many instances where the getServlet is being used like the following code snippet. Now, getServlet() is being deprecated. I would like to know what to use instead. I tried looking at google a lot but no luck till now.

protected XXXServlet getYYYActionServlet() {
        try {
            return (XXXServlet)getServlet();
        } catch(ClassCastException ex) {
            return null;
        }
    }

Please provide your suggested course of action with example. Also, please let me know the remediation steps for processActionPerform() in Struts1.

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
Test
  • 91
  • 11
  • 1
    Why do you need a servlet? – Aleksandr M Mar 15 '17 at 20:56
  • @AleksandrM... It is there in the existing Struts1 Code... Please help me understand the question. – Test Mar 16 '17 at 03:22
  • 1
    The question is why do you need a reference to the servlet. There's no servlet in Struts 2. – Dave Newton Mar 16 '17 at 05:34
  • Actually, the reference to Servlet is there in Struts1, and the corresponding code is provided. We need to refactor the code to Struts2. What will be its equivalent code? – Test Mar 16 '17 at 08:06
  • 1
    There *isn't* any equivalent code. The question is *why* you need the servlet, then we can tell you how to implement the functionality you require in S2. The analogy is that you don't actually want electricity, you want the electricity to *do* things with. We're asking what those things you want to do are. – Dave Newton Mar 16 '17 at 13:54
  • @DaveNewton.... Well, the instance of Servlet is obtained in Action and then its corresponding methods are being called. – Test Mar 17 '17 at 03:38
  • Understood. I'm asking why. What functionality from the Struts 1 servlet do you need in an action? So far it just sounds like the app is poorly-structured. I've *never* had to do this in a Struts 1 app. The short answer is that you should structure your S2 app so you can call shared functionality in a way that makes sense in S2. – Dave Newton Mar 17 '17 at 03:45
  • @DaveNewton, and how would that be, would you recommend placing all the methods in a single class and invoking the appropriate methods/would you recommend instantiate the servlet inside the action – Test Mar 17 '17 at 03:54
  • There is no servlet. I can't say it any more clearly. Where the functionality belongs depends on what you're actually doing. – Dave Newton Mar 17 '17 at 13:05

1 Answers1

0

Modified the answer, I am using core java concepts to get the instance of the servlet:-

    private static XXXServlet xxxServlet = null;

    public static XXXServlet getInstance() {
        return xxxServlet; 
    }

    public XXXServlet() {
        super();
        xxxServlet = this;
    }
Test
  • 91
  • 11