简体   繁体   中英

Calling Java Servlet with GET but processing request with POST - calling doPost via doGet technically still a GET action?

I have a link on a JSP page which calls a servlet, via GET, like so:

https://myserver.com/servlet/ServletTest

There are no parameters to pass, but it will be inserting and possibly updating data on the back-end. By convention, I should be using POST. Within the servlet, ServletTest , is calling doPost via doGet technically still a GET action?

public void doGet(HttpServletRequest req, HttpServletResponse resp)
{
    doPost(req,resp);
}

public void doPost(HttpServletRequest req, HttpServletResponse resp)
{
    //process the incoming request
}

As I said, I'm not pushing any parameters from the client, therefore, it may not make much sense to create an empty Form on the JSP page. Ultimately, I want to perform a POST action without having to use a Form.

The servlet will ultimately be creating a token to be used later on for a REST API call. Thus, a POST might be the right action to use, not GET. I didn't think this would be necessary information; I apologize for not including it earlier.

Thank you.

There are no problems with doing that. You can do the code in doGet since your servlet is handling a get request. The conventions are there to be broken :)

If you are designing a REST Api for example you should obey the convention and if you need such method usually is due to bad design. Otherwise if it is just an url you want to call to perform an action there is no problem to make it with a get request or call one method from the other so you can accept both kinds of request.

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