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

Form Based Applications

 Form Based Applications in Servlets:

Generally in many web applications we might have gone through many forms like registration, login, etc.., in those forms we might have entered values and clicked on the buttons like SUBMIT/REGISTER/LOGIN. After we click the button what is happening there. How we are getting the response. Let us see that now.

Form based applications are responsible to capture the data from the end user and send it to the server. Let us see an example, the following is the form that takes the data from the end user.
form base application in servlets
Form based application in servlets that takes the data from webpage
The following is the servlet that captures the data from the form and display the entered values on the webpage.

Form Based Applications
form based application

                                  

Introducing doGet() and doPost() Methods

These two methods acts as Service() method in servlet. In project we can use service() or doGet() or doPost(). Now let us see doGet() and doPost() in detail.

doGet(): 

This method indicates which operation has to be carried out by the server. This is the default method sent by the client to the server. In get(), the data that client wants to send to server will get appended to url. But the url limits to 1024 characters. Let us see with example. When we type the url in the address bar and once if we press Enter, by default get request goes to the server. 

doGet()  example

In the above figure you can see the login form. Since this is get() when we enter the username and password and click on Enter/Login button, both the username and password gets appended to the url as follows.

doGet() example.

You can observe the username and password appended to the url. This is the major disadvantage of get() method. To overcome this we use Post().

doPost():

 This method is also used to execute the resource in a server. When we use Post(), the browser/client sends the data to the server through request body. This is the only difference between Get() and Post(). Post request is highly used for secured web pages.                             



Integrating JDBC in Servlet Programming

Now, let us see how to integrate JDBC in Servlets. Generally in web based application we will come through this type of programs. In some websites you might have come across some registration forms, in such registration form, when you enter the data and once you click on the SUBMIT button the entire data is stored into the database. In such cases these programs are used. 

Now, let us assume that the following is the registration form in which we are ought to enter the details.


Registration form
Registration form
 

Servlet Programming

To simplify the development of servlets, SunMicroSystems has given a predifined classes. They are "GenericServlet" and "HttpServlet". The following figure shows the relationship between classes.


relationship between servlet classes.
relationship between servlet classes.

First Step Towards Servlet Programming

Servlet is an API used to develop web based Application. The following are the important packages available in servlet API. 
  • javax.servlet
  • javax.servlet.http
  • javax.servlet.annotation (Available from servlets 3.0 onwards)

Procedure to develop a Web Application

 There are two types of Web Applications. They are:
  • Static Web Application 
  • Dynamic Web Application
To develop Static web application we use HTML, JavaScript whereas to develop Dynamic web applications we use Servlets and JSP's. If you want to develop a web based application we need to follow certain rules. 

Understanding HTT Protocol

Hyper Text Transfer Protocol is the only protocol used for the communication between the client and the server. HTTP is a specification, majorly it is divided into two parts, they are:
  • HTTP Request Format
  • HTTP Response Format

Introduction to Servlets

           We use Servlets to develop the web based applications. To run a web based application we need a server and a client. In a web based application Client sends the Request to the server, the server receives that request, processes it and send the Response to the client. So, in a web based application both server and client are equally important. The following figure shows you the client and server architecture.