Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How can I call doGet method?

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can I call doGet method?

    Hi,

    I am beginner to JAVA and I have the following doGet method, and I need to call to from another method

    public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws IOException, ServletException 
        {
            String url = "";
            //if the number is minus n does not exist
            if (Code < 0 ) 
            {
                url = "/Error.jsp";
            }
            //if the number is not included
            else 
            {
                url = "/Error.jsp";
            }
     
            // forward to the view
            RequestDispatcher dispatcher =
                    getServletContext().getRequestDispatcher(url);
            dispatcher.forward(request, response);
        }

    I want to know how to call this method from another method, just like the following.

    public int getCode()
        {
            if (stateName.equals("Aden") || stateName.equals("aden") || stateName.equals("ADEN"))
            {
                Code = 01;
                return (Code);
            }
            else if (stateName.equals("Mukalla") || stateName.equals("mukalla") || stateName.equals("MUKALLA"))
            {
                Code = 02;
                return (Code);
            }
            else
            {
              return doGet(request, response);
            }
        }

    the line
    return doGet(request, response);
    I want to know the correct way of calling?


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How can I call doGet method?

    inside your getCode() method, you have a line as follows:
    return doGet(request, response);
    if request is of type HttpServletRequest and response is of type HttpServletResponse, and both variables are visible from within the getCode() method, then you are "calling" it correctly. However there is another problem present.
    The footprint of your doGet method says there is no return. So even if you did "call" it correctly, you would get nothing back from it to give your getCode method, for your getCode method to return to its caller...

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How can I call doGet method?

    My recommendation is to read about how servlets are instantiated and the methods are called...doGet and doPost methods are called by the Servlet container. If you want to call them directly, then rethink your design

  4. #4
    Member vigneshwaran's Avatar
    Join Date
    Nov 2012
    Location
    Chennai, TamilNadu
    Posts
    35
    My Mood
    Cheerful
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: How can I call doGet method?

     
    // comments no

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: How can I call doGet method?

    doGet() method accepts a GET Request and your servlet container auto creates request and response parameters to the Servlet for use. In request you have everything you requested and after processing you put everything in your response so that could be sent back as a response to the request.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

Similar Threads

  1. How to call a value from a different method to main
    By CrimsonFlash in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 22nd, 2011, 06:29 PM
  2. How do I call a method from the main method?
    By JavaStudent1988 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 19th, 2011, 08:37 PM
  3. call super method outside of 'this'
    By questionmark in forum Object Oriented Programming
    Replies: 1
    Last Post: January 29th, 2011, 11:29 AM
  4. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM
  5. [SOLVED] Why can't I write to file inside a doGet() method?
    By FailMouse in forum Java Servlet
    Replies: 1
    Last Post: July 7th, 2010, 01:15 AM

Tags for this Thread