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
Code :
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.
Code :
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?
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...
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
Re: How can I call doGet method?
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.