从具有相同参数的两个单独函数中调用时,同一函数的工作方式不同 [英] same function working differently when called from two separate functions with same parameters

查看:98
本文介绍了从具有相同参数的两个单独函数中调用时,同一函数的工作方式不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从一个函数调用时,相同的函数originalStrutsPortletAction.processAction(originalStrutsPortletAction,portletConfig,actionRequest,actionResponse)可以正常工作,但从另一个函数调用时则不能工作,尽管两个调用中的参数值相同.代码如下:-

The same function originalStrutsPortletAction.processAction( originalStrutsPortletAction, portletConfig, actionRequest, actionResponse) is working fine when called from one function but not working when called from another function.Although the parameter values are same in both calls. Th code is as follows:-

public class ExampleLoginAction extends BaseStrutsPortletAction implements Serializable{

    public static int flag=0;
    public  static StrutsPortletAction originalStrutsPortletAction=null;
    public  static PortletConfig portletConfig=null;
    public  static ActionRequest actionRequest=null;
    public  static ActionResponse actionResponse=null;
    String email;
    @Override
    public void processAction(StrutsPortletAction originalStrutsPortletAction,
            PortletConfig portletConfig, ActionRequest actionRequest,
            ActionResponse actionResponse) throws Exception {
        /**
         * This is the custom process action
         * Once you try to login this method will be invoked
         * We can write our own logic here
         * Invoke the original struts action at the end
         */
         ExampleLoginAction.actionRequest=actionRequest;
         ExampleLoginAction.originalStrutsPortletAction=originalStrutsPortletAction;
         ExampleLoginAction.portletConfig=portletConfig;
         ExampleLoginAction.actionResponse=actionResponse;

        System.out.println("#############ExampleLoginAction###############");
        //System.out.println(ParamUtil.getString(arg0, "login"));
        // TODO Auto-generated method stub
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        //SecurityManager security = System.getSecurityManager();
        //System.out.println("Security Manager" + security);
        Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("abhijain.cse@gmail.com","a17432475J@");
                }
            });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("adesh.pathak01@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(ParamUtil.getString(actionRequest, "login")));
            message.setSubject("Testing Subject");
            int otp=new Random().nextInt(10000);
            message.setText("Your OTP is " + otp);
            OTP otpdemo=OTPLocalServiceUtil.createOTP(CounterLocalServiceUtil.increment());
            otpdemo.setEmail(ParamUtil.getString(actionRequest, "login"));
            otpdemo.setPassword(String.valueOf(otp));
            OTPLocalServiceUtil.addOTP(otpdemo);
            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

        //portletConfig.getPortletContext().getRequestDispatcher("/jsp/otp.jsp").forward(actionRequest, actionResponse);
        //actionResponse.setRenderParameter("email",ParamUtil.getString(actionRequest, "login"));
        //actionRequest.setAttribute("password",ParamUtil.getString(actionRequest, "password"));
        String email=ParamUtil.getString(actionRequest, "login");
        this.email=email;
        actionRequest.getPortletSession().setAttribute("email",email);
        HttpServletRequest request=PortalUtil.getHttpServletRequest(actionRequest);
        HttpSession session1=request.getSession();
        session1.setAttribute("loginAction", this);
        actionResponse.sendRedirect("/otpsample-hook/jsp/otp.jsp?email="+email+"&originalStrutsPortletAction="+originalStrutsPortletAction+"&actionRequest="+actionRequest+"&actionResponse="+actionResponse+"&portletConfig="+portletConfig+"&ExampleLoginAction="+this);
        System.out.println("In processAction"+originalStrutsPortletAction);
        System.out.println("In processAction"+portletConfig);
        System.out.println("In processAction"+actionRequest);
        System.out.println("In processAction"+actionResponse);
        System.out.println("bye:::::::::");
        //Thread.sleep(1000);
        System.out.println(flag);
        if(flag==1)
        {
            System.out.println("hello:::::::::");
        originalStrutsPortletAction.processAction(
            originalStrutsPortletAction, portletConfig, actionRequest,
           actionResponse);
        //flag=0;

        }
        //new MVCPortlet().getPortletContext().getRequestDispatcher("/jsp/otp.jsp").forward(actionRequest, actionResponse);


    }

    public String render(
            StrutsPortletAction originalStrutsPortletAction,
            PortletConfig portletConfig, RenderRequest renderRequest,
            RenderResponse renderResponse)
        throws Exception {

        /**
         * Our own render method
         * This method is for rendering the view
         * At the end call the original struts action
         */

        System.out.println("##########Rendering view############");
        renderResponse.getWriter().println("custom rendering of page");
       // return null;
        return originalStrutsPortletAction.render(
           null, portletConfig, renderRequest, renderResponse);
       // return null;

    }

    public static void check() throws Exception
    {
        System.out.println(":::::hi::::::");
        System.out.println(originalStrutsPortletAction);
        System.out.println(portletConfig);
        System.out.println(actionRequest);
        System.out.println(actionResponse);
        originalStrutsPortletAction.processAction(originalStrutsPortletAction,
                 portletConfig, actionRequest,
                        actionResponse);

    }
}

函数

originalStrutsPortletAction.processAction(originalStrutsPortletAction,
                 portletConfig, actionRequest,
                        actionResponse)

当从processAction()调用时效果很好,但是当在check()方法内部调用时,我得到了空指针异常,尽管两者的参数值相同.我已经通过在两个函数中打印它们来检查了参数值.它们是相同的.那么错误可能在哪里呢?我在其中停留了一天,但找不到解决方案.堆栈跟踪如下:-

when called from processAction() works fine but when it is called inside check() method I am getting null pointer exception although the parameter values for both are same. I have checked the parameter values by printing them in both functions.They are same. So where could be the mistake? I got stuck in it for one day but could not find the solution.The stack trace is follows:-

java.lang.NullPointerException
    at com.liferay.portlet.login.action.LoginAction.login(LoginAction.java:196)
    at com.liferay.portlet.login.action.LoginAction.processAction(LoginAction.java:93)
    at com.liferay.portal.struts.StrutsPortletActionAdapter.processAction(StrutsPortletActionAdapter.java:60)
    at com.liferay.portal.kernel.struts.BaseStrutsPortletAction.processAction(BaseStrutsPortletAction.java:44)
    at com.abhishek.ExampleLoginAction.check(ExampleLoginAction.java:196)
    at org.apache.jsp.jsp.otpCheck_jsp._jspService(otpCheck_jsp.java:90)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at sun.reflect.GeneratedMethodAccessor482.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.liferay.portal.kernel.bean.ClassLoaderBeanHandler.invoke(ClassLoaderBeanHandler.java:67)
    at com.sun.proxy.$Proxy642.doFilter(Unknown Source)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDirectCallFilter(InvokerFilterChain.java:165)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:96)
    at com.liferay.portal.kernel.servlet.PortalClassLoaderFilter.doFilter(PortalClassLoaderFilter.java:74)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:204)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:109)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter.doFilter(InvokerFilter.java:96)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

推荐答案

这可能是因为某些依赖关系在每种情况下的解析方式不同,具体取决于谁调用此方法.

That's probably because there are some dependencies that are resolved differently in each case, depending on who is calling this method.

在某些框架中,您会有类似状态的信息,当请求到来时,服务器会初始化一些变量,例如,特定用户的会话对象,您可以在其中存储一些信息.如果您从其他线程调用相同的方法而没有针对任何用户解析会话,那么您根本就没有会话,并且可能会失败.

In some frameworks, you have something like a state, when request come then the server initializes some variables, for instance a session object for the particular user in which you can store some information. If you call the same method from other Thread without the session being resolved against any user then you have no session at all and it could fail.

但这不限于会话,您正在使用的其他变量可能会根据调用该方法的人而被初始化(或未初始化).

But this is not restricted to session, there could be other variables you are using that are initialized (or not) depending on who is calling the method.

这篇关于从具有相同参数的两个单独函数中调用时,同一函数的工作方式不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆