Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Java EE Servlets actually used directly?

I'm just trying to get started with Java EE and related concepts. However, I'm having some trouble understanding the relations between some technologies and the roles they play.

As far as I understand, a Java EE Servlet is a Java class that runs inside a server, and generates responses to requests (typically HTML responses to HTTP requests, though Servlets can theoretically serve any protocol).

My questions:

  • As far as I understand, I can either write a Servlet class directly, or I can use some technology like JSP or JSF, which will then generate/provide a Servlet for me. At any rate, the Java EE web container (e.g. Apache Tomcat) where I ultimately run my app will only see Servlets, and will not care how they were created (so Servlets are kind of a low level plumbing). Is that true?
  • If Servlets are kind of low-level, is there any reason to actually use Servlets directly? I have seen many tutorials that explain how to write a Servlet, but that seems rather unpractical. Is there any situation where writing a Servlet directly is better/preferred to using JSP or similar?
  • Finally, Servlets need a server to run in (e.g. Apache Tomcat). When reading about servers in that context, I've seen various names such as (Java EE) web container, or servlet container, or JSP container, or just Java EE server. Do these terms all mean the same, or is there a difference?

Thanks for helping me get started!

like image 801
sleske Avatar asked May 12 '11 23:05

sleske


People also ask

Is Java Servlet still relevant?

Servlets are still used as the basis for web frameworks (though many are moving to netty now). Jsp is less useful unless you have to work with old code, newer templating frameworks are just better.

Are servlets Java EE?

A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers.

Are servlets and JSP still used?

One of the original Java web technologies, JSP is still widely used with servlets and JSTL.


1 Answers

When not using a MVC framework like JSF, Spring MVC, Struts, etc, you still need a servlet to do the basic request/response controlling job. The JSPs -while under the covers indeed being compiled to servlets- should be used as view only, not as controller.

I think your confusion is caused by the relatively huge amount of low quality tutorials about servlets where they are been used to print plain HTML by out.print() statements. This is in view of MVC utterly wrong. I'd suggest to start at our wiki page: https://stackoverflow.com/tags/servlets/info

like image 163
BalusC Avatar answered Oct 13 '22 11:10

BalusC