5

We know that JSPs gets converted to Servlets, Servlets are for the Buisines logic and JSPs for the view etc... But theoretically, you can do most of the things you do with JSP with a servlets. Same thing the other way round.

But Is there anything that you can implement with Servlets and not with JSPs or vice versa?

I got this question some time back in an interview, but did not find any answer after lots of googling.

pushy
  • 9,535
  • 5
  • 26
  • 45
ManuPK
  • 11,623
  • 10
  • 57
  • 76

6 Answers6

5

But Is there anything that you can implement with Servlets and not with JSPs or vice versa?

Technically, there's nothing which can be implemented by only either of them.

Functionally, there's a huge difference with regard to maintainability when you follow the MVC design pattern and implement the controller part in the servlet and the view part in the JSP. The Servlet API offers a much clearer abstraction to hook on specific HTTP methods and control the request/response before any bit is written to the response. JSP in turn is part of the response which can make some tasks harder to implement, such as changing the response in case of an exception.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • The `jspInit()` method can be used for this in combination with `` in `web.xml`. But this is discouraged, together with *scriptlets*. You should be using a Servlet for this. Or better, a `ServletContextListener`. It's after all about proper design and using the right tools for the job. – BalusC Nov 14 '11 at 14:20
  • 1
    But the question isn't about whether you *should*, it's about capabilities. – Dave Newton Nov 14 '11 at 14:23
  • The above was a comment-reply on the question whether a JSP can be initialized on application's startup, but the OP removed the comment. @Dave: read the 1st paragraph of my answer. Must I expand on that? – BalusC Nov 14 '11 at 14:23
  • I did (the sentence), then the next para which didn't ;) – Dave Newton Nov 14 '11 at 14:28
  • @Dave: It's an interview question. If the interviewer would deny you for elaborating on the normal/best practices, you'd be happy he did because you wouldn't like to work for such a company. – BalusC Nov 14 '11 at 14:30
  • 1
    ... Whatever. My point remains; as asked, the OP's post is about capabilities, not wisdom. – Dave Newton Nov 14 '11 at 14:33
  • I also understood this question as merely technical capabilities rather than good practices. – Piotr Nowicki Nov 14 '11 at 15:18
  • @Piotr: I have already posted the answer. I've even edited it to emphasize it more. You all also know, an oneliner answer without going in depth is not my style of answering. – BalusC Nov 14 '11 at 15:34
  • @BalusC sure - it was just about sharing how the OP's question can be understood; not about accusing that your posts are not in depth. – Piotr Nowicki Nov 14 '11 at 15:46
2

There are some subtile differences between the two:

The lifecycle is different, JSPs are translated to a document form (from JSP-file form), and then to Java class file (.java file). From here on end they are treated like servlets (compiled, etc).

JSPs also have the Page scope which is not available to Servlets.

In terms of reusability JSPs are different then Servlets. JSPs can use TagFile and TagLibraries as reusable units of logic, something a servlet cannot do.

Shivan Dragon
  • 15,004
  • 9
  • 62
  • 103
  • Not true. The different lifecycle doesn't change the technical capabilities. The page scope can be implemented using method-local variables in servlet method. The tagfiles/taglibraries can be implemented using `RequestDispatcher#include()` delegating to a different and reuseable servlet which writes to the response directly. See also for example http://stackoverflow.com/questions/3024949/how-do-i-execute-a-sequence-of-servlets – BalusC Nov 14 '11 at 15:21
  • Yes agreed, and if you want to you can make your own servlet engine that translates Servlets to JSP code, this way they both work the same. I was talking about differences that exist without doing hacks. Look at the Play framework, there you don't even have the notion of compiled class (.class file) though it does work with (some parts of) the Servlet API. – Shivan Dragon Nov 14 '11 at 15:29
  • That "hacks" are possible doesn't mean that it's impossible to do the one the wrong way. You were however suggesting that it is not possible with servlets. – BalusC Nov 14 '11 at 15:30
  • 1
    Sir, you really like arguing these issues, as I've seen before. I'm not gonna do it this time, it's a time waster. My answer is there, so are both our comments, I think anyone can draw the necessary conclusion. Under your argument the fact that under the version 3 specs the servlets can be configured via annotations while the JSPs cannot still wouldn't constitute a difference as you can byte code modify the classes obtained from JSP compilation and weave-in the annotation configuration as well because "it's possible" – Shivan Dragon Nov 14 '11 at 15:36
  • JSPs doesn't need to be annotated. They're already compiled/constructed automatically. – BalusC Nov 14 '11 at 15:41
  • So, in other words, they're functionally different because of the different lifecycles. – Shivan Dragon Nov 14 '11 at 15:43
  • 1
    Definitely there are functional differences, I've even elaborated that in detail in my answer, however OP asked for technical differences. "What is possible with JSP what isn't possible with Servlet or vice versa". Well, *nothing*. – BalusC Nov 14 '11 at 15:43
0

The only difference is JSP's can be written by document writers, who have no knowledge about Java.

Apart from that no differences.

Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50
0

Although there is technically nothing to stop you from doing so, using jsp's when there is no UI would strike me as a lesson in futility. A servlet can be used for more than just delivering web pages, it can also serve as a simple remoting mechanism via things like Apache HttpClient and Spring remoting.

Robin
  • 24,062
  • 5
  • 49
  • 58
0

What has been said (that there is technically nothing that servlets can do that JSPs can't, and vice versa) is true, but I believe the answer that the interviewer was looking for was that JSPs are only suitable for text output, whereas servlets can easily output text or binary data.

Mansoor Siddiqui
  • 20,853
  • 10
  • 48
  • 67
  • **JSPs are only suitable for text output* but you can easily output binary data using **JSP** inside **jsp scriptlet**. – ManuPK Nov 24 '11 at 16:50
  • You're right, you can use a JSPs to return binary data, but that's not the intended use. Hence my use of the word "suitable". :) I could be wrong, but my guess is that the interviewer was asking which of the two are appropriate in which situations, with bonus points to candidates who know that ultimately they're both technically capable of the same thing. – Mansoor Siddiqui Nov 24 '11 at 17:33
0

As far as i know JSP is specifically for HTTPprotocol. we cant make use of jsp for any other protocol implementation. But Protocol specific servlet we can create just inheriting(implement/extend) the correspond base protocol clasess. In normal case(HTTPprotocol implementation) we can achieve all comfort given by servlet through JSP as well.