Register now or log in to join your professional community.
Basic Difference
Servlet is HTML in Java
JSP is Java with HTML
When to use which one?
When your main concern is manipulating data only then use servlet,
When you concern is to display data properly on Front end then use JSP.
Agree to Danial Asad answer.Yes, One the most important difference. Adding to tha, It was difficult for servlet developer to design the page (html) and then stuff them in servlet. To tackle this problem, they came up with this JSP solution. When jsp is compiled, its create a servlet in the backend.
Servlets are the basis of JSP, a JSP gets translated and compiled by the container into HTTP Servlet, although anything that can be done by JSP can be done by Servlet too but the purpose of JSP is to save you from writing raw HTML to the response stream; so in an MVC architecture the controller is a servlet that forwards the requets to the appropriate JSP (the view) which is resposible for sending the HTML to the browser. hence the servlet should be used for backend developers and JSP for front-end developers.
A servlet provides the base for handling the 7 HTTP request methods, while JSP is an extension to servlerts in the sense that it separates the HTML response layout from application data.
Basic differences are
JSPs and Servlets are used to create dynamic pages
JSP is an HTML page with TAG to use and/or run Java properties/code. Servlets are Java classes to create HTML pages; so JSP are used in front-end side of WebApp and Servlet are used in Backend side.
Generally is more simple to design a dynamic page using JSP.
Main deference between servlet and jsp is ,if we modify some code on our class using servlet so we need to restart the web server ,every time it will take server restart but in case of jsp there is no such type of problem
JSP is Java server Pages which allows us to embed Java codes along with UI or Html.Besides Jsp pages are processed as servlets in the background in the web containers like tomcat or other servers to process data.
Where as Servlets can allow Html ui part to be written within Java codes but it makes it complicated So JSP is the advanced version as per approach .
SERVLET JSP
1.Does not support tag-based programming. 1.Support
2.Does not gives the implicit object. 2.Given implicit object
3.Strong java knowledge is required to work with the servlet. 3.Not Required
4.Need servlet container to execute servlet code. 4.Need JSP container to execute JSP code.
5.Exception Handling is Mandatory. 5.optional.
6.Servlet is identified with it's URL pattern. 6.jsp is identified with it's name or URL pat.
7.cofiguration of the servlet in web.xml the file is mandatory. 7.optional
Jsp recommended to use for view and servlet is used for bussiness logic
JSP is a webpage scripting language that can generate dynamic content. Servlets are Java programs that are already compiled which also creates dynamic web content. JSP run slower compared to Servlet as it takes compilation time to convert into Java Servlets. It's easier to code in JSP than in Java Servlets.