Saturday, 13 September 2014

Servlet vs JSP

Servlet is java code and it is used to create dynamic web pages, which deployed in server (e.g tomcat, jboss).
see below simple servlet example -

import java.io.*;

import javax.servlet.http.*;
import javax.servlet.*;

public class MyServlet extends HttpServlet { 
 
  public void doGet (HttpServletRequest 
                       req,HttpServletResponse res)
    throws ServletException, IOException
  {
    PrintWriter out = res.getWriter();

    out.println("Basic servlet example");
    out.close();
  }
}
 
Here we have created MyServlet class which extends HttpServlet. we
have overrided here doGet method which will invoked by GET method 
of http request.

Servlet class before putting to the server,need to compile it 
manually. To know more about servlet click here 
 
JSP a scripting language and it is used to create web pages, which 
deployed in server. It is easy to understand.This is different than 
other scripting language.
 
JSP class before putting to server, no need to compile it. Because 
JSP container will compile it automatically and convert it into 
servlet classes. 
 
JSP has some components-
1) Declaration - <%! declaration; [ declaration; ]+ ... %>
2) Scriptlet - <% code fragment %>
3) Expression - <%= expression %>
4) Directive-  <%@ directive attribute="value" %> 
 
JSP provide by default implicit Object e.g.(out, page, request, 
response, session and so on..) 

No comments:

Post a Comment