简体   繁体   中英

Passing value of variable from one method to another

This seems so easy, but I don't know why I'm having such difficulty with it...So in the getURL, I return the String "total". I'm trying to return the SAME value "total" already has in the method handleRequest. Suggestions? Thanks in advance!

public class Multiply implements Controller {

    static int product;
    private static String total;

    public static String getURL(HttpServletRequest req) {

        String scheme = req.getScheme();            // http
        String serverName = req.getServerName();    // hostname.com
        int serverPort = req.getServerPort();       // 80
        String contextPath = req.getContextPath();  // /mywebapp
        String servletPath = req.getServletPath();  // /servlet/MyServlet
        String pathInfo = req.getPathInfo();        // /a/b;c=123
        String queryString = req.getQueryString();  // d=789

        String[] item = queryString.split("&");
        product = 1;
        for (int i = 0; i < item.length; i++) {
            String[] s = item[i].split("=");

            String name = s[0];
            String value = s[1];
            int numValue = Integer.parseInt(value);
            product = product * numValue;

        }
        total = "" + product;
        return total;

    }

    public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        String Mess = total;

        ModelAndView modelAndView = new ModelAndView("hello");
        modelAndView.addObject("message", Mess);

        return modelAndView;
    }
}

total is a member variable to the class Multiply . You should have no problems accessing it from either method. Keep in mind that it is uninitialized. So unless you call getURL before handleRequest then total will not be assigned to any String and will give you an error.

Also, I would watch your capitalization in several areas.

edit: Just for clarification, you technically aren't passing a value from one method to another. You are accessing a shared member variable between two methods.

You have a number of problems with your implementation of this. First, by declaring total static, all instances of this class will have the same value of total . If you're using a framework that creates your controller and reuses it, this could lead to problems because all instances of the class will be referring to and updating the same member field.

What you want is to have your getURL method to return the value of total and then call it from your handleRequest . getURL can be static because it relies on no non-static member fields. You should really rename getURL to getTotal or getTotalFromURL because that is what you're doing. What you're asking getURL to do is actually a side effect , and should be avoided as a practice.

public class Multiply implements Controller {

    public static String getURLTotal(HttpServletRequest req) {
        String scheme = req.getScheme();            // http
        String serverName = req.getServerName();    // hostname.com
        int serverPort = req.getServerPort();       // 80
        String contextPath = req.getContextPath();  // /mywebapp
        String servletPath = req.getServletPath();  // /servlet/MyServlet
        String pathInfo = req.getPathInfo();        // /a/b;c=123
        String queryString = req.getQueryString();  // d=789

        String[] item = queryString.split("&");
        int product = 1;
        for (int i = 0; i < item.length; i++) {
            String[] s = item[i].split("=");

            String name = s[0];
            String value = s[1];
            int numValue = Integer.parseInt(value);
            product = product * numValue;

        }
        return Integer.toString(product);
    }

    public ModelAndView handleRequest(HttpServletRequest request,
                                      HttpServletResponse response)
                 throws ServletException, IOException 
    {
        String Mess = Multiply.getURLTotal(request);

        ModelAndView modelAndView = new ModelAndView("hello");
        modelAndView.addObject("message", Mess);

        return modelAndView;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM