I just started working with my first Struts 2 project and I’m currently working with a registration-action.
The registration is going fine except when you access Register.action directly from the url, when you do that the class-variables isn’t instantiated (for example String password) and I get nullpointer-exceptions when using them. Register.action is the action that processes the register.jsp form.
Is there a way to stop users from accessing Register.action without coming from register.jsp?
Or do I need to nullcheck everything?
First, you shouldn’t be accessing a JSP page directly, except under rare circumstances, when using an MVC framework: all activity should take place through an action.
Second, as Aleksandr says, validation should be handled through the framework, either through XML, annotation, or Java-based means. In this case, accessing the processing action directly should result in a validation failure, since the username/password fields are required.
Once validation fails (depending on your configuration, but for example) the framework will forward to the
"input"result, likely the registration JSP page.