tags:

views:

518

answers:

7

I use java mostly for gui programming and php for web programming but i really don't like not having a statically typed language. For my next project i would like to use java instead of php. What is the difference between jsp and servlets?

+3  A: 

JSP basically allows you to write your Java code around HTML, superficially seeming like PHP or ASP. The result is just compiled to a servlet though.

Matthew Flaschen
+2  A: 

I would really recommend reading through the first few sections of the Java EE 5 Tutorial. It really does a good job explaining the different Java technologies.

In short, servlets are an abstraction of an HTTP server that allow you to implement logic based on the HTTP request coming in.

JSP is more on the view side of things, allowing to mix in code with your html view and you'll find it similar to PHP or classic ASP.

You can use servlets without JSP and you can use JSP without servlets (kinda, they're still used in the background), but most often you'll want to use a good MVC controller with the Servlet filling the controller role, and the JSP filling the view role.

tschaible
+2  A: 

If you use mvc JSP would be the view, while the servlet would be the controller. Althought JSP can contain java code, the lesser the better.

To compare it to PHP world,Zend Framework, JSP == .phtml and serlet == .php.

Macarse
This is wrong. The JSP is also a servlet.
Matthew Flaschen
A: 

Servlets are in java and allow http responses to programmed using Java strings. They are most useful for computation work.

Jsps as mostly html with small snippets of Java code, this is much more like PHP and is more useful for website

P.s. Have a look a google app engine, it's great for hosting basic Java apps.

MattChurchy
A: 
  • Servlets are classes that you can use to process any kind of request, but mostly HTTP requests. You write servlets by writing classes that extends javax.servlet.http.HttpServlet
  • JSP is a newer technology than servlets. It is used to combine HTML code with Java Code. At the end of the day, a JSP page is used to generate (automatically) a HttpServlet.

Usually what people do is, write the business logic portion of the WebSite on servlets, and then, forwarding control to a JSP page (similar to what's accomplished with MVC).

But, nowadays, a lot of people would use a framework like JSF or Spring on top of Servlet+JSP technology. So you might want to take a look at one of those frameworks as well.

Pablo Santa Cruz
A: 

Under the covers JSP and Servlets are esentially the same, both compile to servlets and both execute as Java code. The difference between them is in authoring and usage. You author Servlets as Java code, i.e. you write a Java class that derives from HttpServlet and override the appropriate methods. JSPs on the other hand are authored using a template based language, this looks a lot like HTML with code snippets thrown in, similar to many other template based languages out there.

If you are building a web application in Java it is considered (very) good practice to use an MVC style architecture with Servlets as the controller and JSPs providing the view (and POJOs as the model)

Kevin Jones
A: 

JSP follow the MVC model. The main difference between jsp and php at run time.. 1.When a jsp page call's first time it converted as servlet class and than the servlet class is called every time ,it makes the jsp faster then php. 2.you can use the bean(Simple java classes) in the jsp page for business logic implementation.And make out put for that in jsp pages ,like a simple static html page. There are more feature with jsp....

prakash.panjwani
JSP does not necessarily follow the MVC model. You as being the developer have it all in your hands. If you adhere only one rule: "do NOT use scriptlets", then JSP will indeed force you easily to write code according the MVC ideology.
BalusC