I have created a custom interceptor MyInterceptor.java
in Struts 2, which takes the parameters value username
and password
from index.jsp
page and convert the String
in these parameters to uppercase.
Below is my interceptor code, in which I am getting java.lang.NullPointerException
on Line no. 14
MyInterceptor.java:
package com.interceptor;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.ActionInvocation;
public class MyInterceptor implements Interceptor{
public void init(){}
@Override
public String intercept(ActionInvocation ai) throws Exception {
ValueStack stack = ai.getStack();
String s1 = stack.findString("name");
String s2 = stack.findString("password");
String s3 = s1.toUpperCase();
String s4 = s2.toUpperCase();
stack.set("name", s3);
stack.set("password", s4);
return ai.invoke();
}
@Override
public void destroy() {
}
}
Here is my index.jsp
page:
which takes username
and password
"ravi"
:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
</head>
<body>
<h1>Login Here</h1>
<s:form action="login">
<s:textfield name="name" label="Name"></s:textfield>
<s:textfield type="password" name="password" label="Password"></s:textfield>
<s:submit label="Login"></s:submit>
</s:form>
</body>
</html>
Here is action class MyAction.java
:
package com.ravi.action;
import com.opensymphony.xwork2.ActionSupport;
public class MyAction extends ActionSupport{
public String name;
public String password;
public void setName(String name){
this.name= name;
}
public void setPassword(String password){
this.password= password;
}
public String getName(){
return name;
}
public String getPassword(){
return password;
}
public String execute()throws Exception{
if(name.equals("RAVI") && password.equals("RAVI")){
return "success";
}
else {
return "error";
}
}
}
And here is my struts.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<interceptors >
<interceptor name="uppername" class="com.interceptor.MyInterceptor"></interceptor>
</interceptors>
<action name="login" class="com.ravi.action.MyAction">
<interceptor-ref name="uppername"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success" >welcome.jsp</result>
<result name="error">Error.jsp</result>
</action>
</package>
</struts>
And the Stacktrace is as follows:
java.lang.NullPointerException
com.interceptor.MyInterceptor.intercept(MyInterceptor.java:14)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:567)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2522)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2511)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:1)
java.lang.Thread.run(Unknown Source)
I guess, the problem lies in accessing the request parameters in the MyInterceptor.java
. The ValueStack
's method findString()
is not returning the values of parameters name
and password
.
I think the problem should be because of the order of the interceptors. Your interceptor is the last interceptor, so when an action is requested,
new instance
of your actionparameter interceptor
(which is in default stack
) populate the action properties.