Reputation: 49329
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?
Upvotes: 2
Views: 1487
Reputation: 2572
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....
Upvotes: 0
Reputation: 2367
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)
Upvotes: 3
Reputation: 181270
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.
Upvotes: 1
Reputation: 983
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.
Upvotes: 0
Reputation: 93123
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.
Upvotes: 1
Reputation: 7695
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.
Upvotes: 2
Reputation: 284786
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.
Upvotes: 5