• Loading
    • Difference Between JSP and Servlets

      JSP:

      JSP is easy to learn and allow developers to quickly produce web sites and applications in an open and standard way. JSP is based in Java, an object oriented language. JSP offers a robust platform for web development.
      Main reasons to us JSP are :
      • Multi platform
      • component reuse by using Java Beans
      • It also incorporates EJB
      • it has every advantages that java provides

      Servlet:

      A servlet is a java component that can be plugged into Java enabled web server to provide custom services. Servlets are designed to work with in request response processing model. In a Request/Response model, a client sends a request to a server and the server responds by sending back a reply message can come in the form of an
      • HTTP URL
      • FTP URL
      • a custom protocol

      JSP VERSUS SERVLETS
      Before the advent of JSP, the most used java technology that could generate dynamic web page content was Java Servlets. Because JSPs eventually are compiled into Java servlets, you can do as much with JSPs as you can do with Java servlets. However Coding JSPs is easier than coding Java servlets. With Jsp, you place static text by coding HTML tags as opposes to java servlets ,in which you place static text by coding a many println statements. With JSPs, you can change static text by changing HTML;and with servlets you can change static text by modifying a java servlet( this also adds to the compile/ debug cycle)
      Combining Java Code and java cells into an HTML page is more easy than using straigt java code in a Servlet. Jsp syntax gives you a shortcut for coding dynamic Web pages, typically requiring much less code than java Servlet syntax.
      Lets see the example:
      Servlet Code:
      Code:
      import javax.Servlet.*;
      import javax.Servlet.http.*;
      import java.io.*;
      public class Welcome extends HttpServlet{
       public void doGet(HttpServlet Request rq, HttpServlet Response rsp)
      {
         rsp.setContentType("text/html");
         try(
            PrintWriter out= rsp.getWriter();
           out.println("<html>");
           out.println("<head><title>Welcome</title></head>");
           out.println("<body>");
           out.println("<h3>hello</h3>");
           out.println("<p>date +"=new java.util.Date()+".</p>");
           out.println("</body>");
           out.println("</html>");
             }
      catch (IOEXCeption ioe)
        {
         }
      }
      }

      JSP code:


      Code:
      <html>
      <head><title>welcome </title></head>
      <body>
      <h1>hi</h1>
      <p> date is <5= new java.util.Date()%></p>
      </body>
      </html>



    Disclaimer: Users of techforum4u.com are responsible for ensuring that any material they post (article, blog posts, images or other mulitimedia content) does not violate or infringe upon the copyright, patent, trademark, or any personal or proprietary rights of any third party, and is posted with the permission of the owner of such rights.Anyone who violates these rules may have their access privileges removed without warning.