Search This Blog

Showing posts with label Servlets. Show all posts
Showing posts with label Servlets. Show all posts

Wednesday, 15 February 2017

Java Servlet Life Cycle

A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes.
The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets. All servlets must implement the Servlet interface, which defines life-cycle methods. When implementing a generic service, you can use or extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet and doPost, for handling HTTP-specific services.

Servlet Life Cycle

The life cycle of a servlet is controlled by the container in which the servlet has been deployed. When a request is mapped to a servlet, the container performs the following steps.
1.      If an instance of the servlet does not exist, the web container
a.      Loads the servlet class.
b.      Creates an instance of the servlet class.
c.       Initializes the servlet instance by calling the init method.
2.      Invokes the service method, passing request and response objects.
3.      If the container needs to remove the servlet, it finalizes the servlet by calling the servlet’s destroy method.

Lets try to understand the Servlet Life Cycle in details

1.      A web browser sends an HTTP request to a web server by any one of the following ways.
a.      A user clicks on a hyperlink displayed in an HTML page.
b.      A user fills out a form in an HTML page and submits it.
c.       A user enters a URL in the browser’s address field and presses Enter.

2.      The container "sees" that the request is for a servlet, so the web container creates two objects.
a.      HttpServletRequest
b.      HttpServletResponse

3.      The Web Container finds the correct servlet based on the URL in the request , the container then creates or allocate a thread for that request and calls the service() method of the servlet, passing the request and response objects as arguments.

4.      The service() method figures out which servlet method to call based on HTTP method sent by the client.If the client sent GET , then corresponding doGet() method is called.

5.      The servlet uses the response object to write the response to the client.

6.      The service() method completes, the threads dies or returns to  back to the pool.The request and response object are garbage collected.The client gets the response.



Servlet Life Cycle

Servlet Life Cycle


Monday, 23 January 2017

Difference Between Servlet and JSP

This post highlights the difference between JSP and Servlet technologies.

Java Servlet technology and JavaServer Pages (JSP pages) are server-side technologies that become the standard way to develop web applications.

Most importantly, if used effectively by following best practices, servlets and JSP pages help separate presentation from content.

Servlets support a request and response programming model. When a client sends a request to the server, the server sends the request to the servlet. The servlet then constructs a response that the server sends back to the client. When a client request is made, the service method is called and passed a request and response object. The servlet first determines whether the request is a GET or POST operation. It then calls one of the following methods: doGet or doPost. The doGet method is called if the request is GET, and doPost is called if the request is POST.

Adding more, Servlets are Java classes that can generate dynamic HTML content using print statements.
out.println("<html><head><title>"+thisIsMyMessage+"</title></head>");
So, you can embed HTML code into Java code.

A JSP page is basically a web page with traditional HTML and bits of Java code. The file extension of a JSP page is .jsp rather than .html or .htm, which tells the server that this page requires special handling that will be accomplished by a server extension. When a JSP page is called, it will be compiled (by the JSP engine) into a Java servlet.

Importantly, we can add dynamic content or Java Code inside an HTML Tag using JSP’s Scriplets.
And scriptlets, this is code fragments like:
<% String message = "Hello !"%>
<H2> Welcome and  <%=message%>! </H2>
So, you can embed Java code into HTML code.





SERVLET
JSP
A servlet is a server-side program and written purely on Java.
JSPs are HTML pages with .jsp extension. JSP’s are extension of servlets to minimize the effort of developers to write User Interfaces using Java programming.
Executes inside a Web server, such as Tomcat
A JSP program is compiled into a Java servlet before execution. Once compiled into a servlet, it's life cycle will be same as of servlet. But, JSP has it's own API for the lifecycle.
Servlets run faster than JSP
JSP runs slower because it has the transition phase for converting from JSP page to a Servlet file.
Servlet has the life cycle methods init(), service() and destroy()
JSP has the life cycle methods of jspInit(), _jspService() and jspDestroy()
Difficult to write as one has to write HTML tags within quotes(“<HTML>”) in Java. Mixing HTML content inside Java is tedious.
Easier to write than servlets as it is similar to HTML
Written in Java, with a few additional APIs specific to this kind of processing. Since it is written in Java, it follows all the Object Oriented programming techniques.
One of the key advantage is we can build custom tags using JSP API ,  which can be available as the re-usable components with lot of flexibility
In MVC architecture Servlet acts as controller.
In MVC architecture JSP acts as view.
Servlet advantages include:
·  Performance: get loaded upon first request and remains in memory indefinitely.
·  Simplicity: Run inside controlled server environment.
·  Session Management : overcomes HTTP's stateless nature
·  Java Technology : network access, Database connectivity, j2ee integration
JSP Provides an extensive infrastructure for:
·         Tracking sessions.
·         Managing cookies.
·         JSP is Extensible: Can create custom tags to extend the JSP functionality.
·         Separation of roles: Clearly identifies separation of roles for Developers, Content Authors/ Graphic Designers/ Web Masters.

Thursday, 22 December 2016

What is the difference between doGet () and doPost ()?

While using doGet() or doPost() prefer using doPost() because it is secured and it can send much more information to the server.

GET or doGet()

GET is the simples HTTP method, and its main purpose is to ask a server to get a resource and send it back. The resource can be an HTML page, an image , a PDF etc.

  • The request parameters are transmitted as a query string appended to the request.
  • All the parameters get appended to the URL in the address bar.
    http://example.com/search?emp_name=sanjay 
  • Allows browser bookmarks but not appropriate for transmitting private or sensitive information. 
  • In an HTML you can specify as follows:
    <form name=”myForm” method=”GET” >
  • GET was originally intended for static resource retrieval.
  • GET is not appropriate when large amounts of input data are being transferred.



POST or doPost()

HTTP Post request is more powerful and used by the browser to make complex requests on the server. For example the user fills up a form and submit the data , which is then supposed to be inserted into a database.

The request parameters are passed with the body of the request.

More secured. In HTML you can specify as follows:
<form name=” myForm” method=”POST” >
POST was intended for form submits where the state of the model and database are expected to change.

Since POST sends information through a socket back to the server and it won’t show up in the URL address bar, it can send much more information to the server.



Unlike doGet(), it is not restricted to sending only textual data. It can also send binary data such as serialized Java objects.