by Dinesh Thakur Category: introduction to Servlet

Before creating the first servlet, you need to understand the Servlet API and Tomcat Servlet container. The Servlet API provides interfaces and classes that are required to built servlets. These interfaces and classes are group into the following two packages :



 
by Dinesh Thakur Category: introduction to Servlet

The Common Gateway Interface (CGI) is the first technology used to generate dynamic contents. It allows a web client to pass data to the application running on the web server so that a dynamic web page can be returned to the client according to the input data. For example, when you use a search engine, buy a book at an online store; get a stock quote etc., your browser uses CGI to communicate with a server side application.



 
by Dinesh Thakur Category: introduction to Servlet

Java servlets provide web developers with a simple consistent mechanism for extending the functionality of a web server and provide dynamic behaviour for web applications. Servlets are Java classes that function like CGI programs. They accept a request from a client (usually a web browser), process that request and return a response to the client. All servlets are loaded and executed by a servlet container that can run by itself or as a component of a web server. The servlet container is also referred to as a servlet engine in the early days of servlet technology.



 
by Dinesh Thakur Category: introduction to Servlet

• The logic and data of stand alone, desktop applications are specific to that computer where these applications are running.

• The logics and data of client-server applications, two-tier applications like JDBC applications are specific to one network where they are running. In the above said two-tier, client-server applications the server allows only known clients (recognized clients).



 
by Dinesh Thakur Category: Basic Servlet Examples

Like doGet () method, the do Post () method is invoked by server through service () method to handle HTTP POST request. The doPost () method is used when large amount of data is required to be passed to the server which is not possible with the help of doGet () method.

 
by Dinesh Thakur Category: Basic Servlet Examples

The doGet () method is invoked by server through service () method to handle a HTTP GET request. This method also handles HTTP HEAD request automatically as HEAD request is nothing but a GET request having no body in the code for response and only includes request header fields. To understand the working of doGet () method, let us consider a sample program to define a servlet for handling the HTTP GET request.



 
by Dinesh Thakur Category: Basic Servlet Examples

The GenericServlet class implements the Servlet and ServletConfig interfaces. Since service () method is declared as an abstract method in GenericServlet class, it is an abstract class. The class extending this class must implement the service () method. It is used to create servlets which are protocol independent.



 
by Dinesh Thakur Category: Basic Servlet Examples

The process of selecting file from the client machine file system and sending that file to the server machine system is known as file uploading. File uploading and downloading using servlet-api is a complex process, that is why industry uses JavaZoom 3rd party API.

 
by Dinesh Thakur Category: Basic Servlet Examples

HttpServletResponse is a predefined interface present in javax.servlet.http package. It can be said that it is a mirror image of request object. The response object is where the servlet can write information about the data it will send back. Whereas the majority of the methods in the request object start with GET, indicating that they get a value, many of the important methods in the response object start with SET, indicating that they change some property. Note that these interfaces adhere to the usual naming conventions for beans.



 
by Dinesh Thakur Category: Basic Servlet Examples

HttpServletRequest is an interface and extends the ServletRequest interface. By extending the ServletRequest this interface is able to allow request information for HTTP Servlets. Object of the HttpServletRequest is created by the Servlet container and, then, it is passed to the service method (doGet(), doPost(), etc.) of the Servlet.



 
by Dinesh Thakur Category: Basic Servlet Examples

Before handling HTTP get requests and HTTP post requests, you should have some knowledge about HTML forms.

 
by Dinesh Thakur Category: Basic Servlet Examples

By default every web resource program generated output/response content will be stored in the buffer before it is getting displayed on browser window as web page content. Due to this browser window may show old output collected from the buffer even though the web resource program of web application is capable of generating new and updated output.   

 
by Dinesh Thakur Category: Basic Servlet Examples

We can gather the miscellaneous information from HttpRequest by calling various getxxx methods on request object as shown below.

* Use user-agent request header that holds browser software name as shown below.



 
by Dinesh Thakur Category: Basic Servlet Examples

No, Because Servlet container uses 0-param constructor of our Servlet class that create the object of our Servlet class. If 0-param constructor (default constructor) not available then the Servlet container throws: java.lang.InstantiationException.



 
by Dinesh Thakur Category: Basic Servlet Examples

Once the Tomcat is installed and configured, you need to perform the following steps to create and run a servlet.



 
by Dinesh Thakur Category: Servlet with MySql Database

Some of major classes and interfaces which are frequently used while developing a Java application are as follows:

 
by Dinesh Thakur Category: Servlet with MySql Database

The JDBC specification related API in the form of java.sql package.



 
by Dinesh Thakur Category: Servlet with MySql Database

This function Will Returns the year and week.

 
by Dinesh Thakur Category: Servlet with MySql Database

This Function will Returns the year.

 
by Dinesh Thakur Category: Servlet with MySql Database

This function will show to the result according to Where Query in mysql.

 

Page 2 of 12



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.