-1

I've been reading quite a bit about doPost(), doGet() and service() and REST but I've come to a question I wasn't able to find an answer to. This question may be more on a subjective note as it is related to URL aesthetics rather than convention.

The thing is when organising the structure of a project, sometimes if it's small enough I'd rather have one single servlet with the different methods for the WebSite asigned by a "GET" parameter say

http://localhost/Servlet?option=METHOD

However, this brings up a conflict when trying to separate "GET" from "POST". Now what I'm asking here is if having a "nice" (subjective opinion) URL means having it with that format, would implement service() method be the best way to use?

I'm open to suggestions as is using for example

http://localhost/Servlet/Option

But how would I go about implementing such format also being able to pass get Parameters as

http://localhost/Servlet/Option?param=value 
alexhg11
  • 165
  • 1
  • 2
  • 15

1 Answers1

1

You can just make one call the other to implement both:

public void doPost(HttpServletRequest request, HttpServletResponse  response) throws IOException, ServletException
{
    doGet(request, response);
}

public void doGet(HttpServletRequest request, HttpServletResponse  response) throws IOException, ServletException
{
  .......
}

As for service() I don't know why people override it. The documentation (http://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpServlet.html) plainly says:

There's almost no reason to override the service method. service handles standard HTTP requests by dispatching them to the handler methods for each HTTP request type (the doXXX methods listed above).

EDIT: I see now the question is really about having many many methods in one servlet differentiated by a parameter. Yes, that is bad practice. You should separate different operations into different servlets. And in the UI its better to use different forms for different operation rather than one form with many submit buttons. Its much easier to break a spaghetti code servlet controlled by a bunch of if-statements when editing.

developerwjk
  • 8,619
  • 2
  • 17
  • 33
  • I do this currently to address this problem, but I've read in another question here in SO that it is considered bad practice. I'm looking for different ways to look at it that could be considered good practices. – alexhg11 Jan 10 '14 at 00:18
  • They're undoubtedly talking about some made up and impractical rule like "POST should only be used for servlets that change data and GET should always be used when retrieving data." But in real life, I tend to use POST almost exclusively for both, because I don't want the parameters showing in the address bar. When I do want to be able to make links with the parameter in there, I tend to implement both as above. – developerwjk Jan 10 '14 at 00:21
  • There is good reason to not implement GET for servlets that change data however. Only implementing POST for those will weed out a few script kiddies who only know how to hack from the address bar. – developerwjk Jan 10 '14 at 00:24
  • Yeah I apply it the same way that you do it. I basically use it as a way to assign different parts of my website and using post for anything that changes data. However, don't you reckon the correct way of achieving the behavior you posted would be overriding the service() method? – alexhg11 Jan 10 '14 at 02:26
  • Service is there to route different request types to their functions, GET to doGet, POST to doPost, DELETE to doDelete, PUT to doPut. doHead, doOptions, doTrace. Do you want your servlet to answer to DELETE and PUT requests? Head, Options, Trace? I also think you may be somewhat confusing URL rewriting that makes the parameters part of the URL with this concept. – developerwjk Jan 10 '14 at 16:36
  • See http://stackoverflow.com/questions/950497/is-there-a-url-rewriting-engine-for-tomcat-java about URL rewriting for Tomcat. – developerwjk Jan 10 '14 at 16:42
  • Yeah, I found a similar article yesterday and it's pretty much what I needed to know. – alexhg11 Jan 10 '14 at 20:27