0

I'm new to Java server-side programming, my question is basically to get to a starting point using Servlets (low level without using spring mvc etc.) and then build my way up from there, coming from node.js background where a route definition would start with a function (app.get(request, response) {}, app.post(request, response) {} etc.), and the function would receive request and response in parameters for one of http methods (GET, POST, PUT, DELETE).

If someone can please help on the starting point of how do I define methods against a route (let's say /users) inside a servlet class that'd map to http methods while providing request and response in it's parameters.

My attempt

public class FirstServlet extends HttpServlet {

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

}
user2727195
  • 7,122
  • 17
  • 70
  • 118
  • Well what have you tried so far? You can use the servlet container configuration (web.xml) to specify which servlet should be invoked for which URL, and then if you just extend `HttpServlet` you can override whichever methods you want. – Jon Skeet Jul 02 '15 at 06:14
  • Well did you look at any documentation to find out? (Delete and put, yes - for patch you'd need to override `service`, and intercept patch calls there, delegating back to the normal implementation if it isn't patch.) – Jon Skeet Jul 02 '15 at 06:19
  • I'm kinda lost here, google search is giving me all sorts of links, will appreciate the direct link to documentation – user2727195 Jul 02 '15 at 06:20
  • A search for "httpservlet documentation" gets straight to useful links... http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServlet.html for example, depending on which version of things you're using... (If you look at the docs for the container you're using, that will probably have the right version.) – Jon Skeet Jul 02 '15 at 06:21
  • thanks, this is enlightenment, it seems like servlet basically takes care of all the REST related needs, can you please tell me then why people use Spring framework that bloats the code with it's own annotations etc. (except MVC thing which is clear to me) – user2727195 Jul 02 '15 at 06:24
  • 1
    I don't think you're really in a position to judge how useful Spring is right now. I haven't used it for a long time myself, and not for this sort of thing, but you should probably read the documentation to say what the authors think the benefits are. There's a lot more to REST than just getting the request to the right method of the right servlet though... – Jon Skeet Jul 02 '15 at 06:25

1 Answers1

5

I believe what you want are Servlet mappings. You can also find a bit more info here

But basically this is the way you tell the webserver (e.g. Tomcat) what servlet to use to answer requests sent to a given url pattern. Thus you map the pattern with the servlet you want to use to serve it.

You can also find more info on the inner workings here.

Edit: If you want to handle all verbs you can use a service. From the first link:

You may have seen other servlet examples implement the doPost() and/or doGet() methods. These methods reply only to POST or GET requests; if you want to handle all request types from a single method, your servlet can simply implement the service() method. (However, if you choose to implement the service() method, you cannot implement the doPost() or doGet() methods, unless you call super.service() at the beginning of the service() method.) The HTTP servlet specification describes other methods used to handle other request types, but all of these methods are collectively referred to as service methods.

All the service methods take the same parameter arguments. An HttpServletRequest provides information about the request, and your servlet uses an HttpServletResponse to reply to the HTTP client. The service method looks like the following:

public void service(HttpServletRequest req, HttpServletResponse res) throws IOException { ... }

Acapulco
  • 3,373
  • 8
  • 38
  • 51
  • this seems like for each method, there's a separate servlet, I want to see something with one servlet handling all methods, I've edited my question with my attempt – user2727195 Jul 02 '15 at 06:21
  • thanks, it's getting clear to me, one last thing and if you can point to some resource for reading, and i commented this above as well, since the servlets are taking care of all rest related tasks why people use Spring framework that bloats the code with it's own annotations etc. (except MVC thing which is clear to me) – user2727195 Jul 02 '15 at 06:27
  • 1
    Using servlets like this is useful when you are going to do simple stuff. But if you try to build, let's say, an auction site, you will probably benefit from a framework. Once you start requiring several tens of different models in the database, different ways of representing those models (lists, grids, combo boxes), being able to query the backend database to get even more objects, etc. you can start to see why a framework might be needed. For example, if you want to access the database from the servlet you will need to use a JDBC driver. – Acapulco Jul 02 '15 at 06:30
  • 1
    And then add your own methods to build queries (prepared statements hopefully), etc. You will also be responsible for closing all connections and streams properly, so you will start to fill your code with double try/catch blocks to avoid having unhnadled exceptions going rogue and consuming resources. Just for that specific part is why you have ORMs, and lots of them. Spring and such already have all those tools integrated and ready to use. Of course, that still means that you get lots of boilerplate code, yes. But you would need to balance the pros and cons to actually make a sound choice – Acapulco Jul 02 '15 at 06:33
  • one more thing, and it's very important, is there a new copy or new instance of servlet instantiated for each request by the runtime? (coming from node.js) where the object gets instantiated and loaded into memory for once and it handles all incoming requests concurrently using the same instance, I hope servlets are done in the similar manner, if not I'll ask another question about creating daemon servlets, will appreciate if you can guide on how to ask the question in a right way as I'm not much familiar with java jargon, and I'd ping you for the new question. – user2727195 Jul 02 '15 at 07:11
  • It depends on how is the servlet container implemented, but apparently most of the time a single servlet is created for several requests. See here: https://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-session-variables-and-multithreading and here: https://stackoverflow.com/questions/6298309/how-many-instaces-if-servlet-class-are-created-by-container-after-loading-it-s – Acapulco Jul 02 '15 at 14:33