Introduction to Java Web: Servlets
The Java programming language is used in such popular websites as Google, Facebook, YouTube, Amazon, and many others. This proves that Java is good for multiple purposes. In this sample I would like to pay attention to the basics of web technologies bound to the usage of Java.
However, prior to focusing on creating web pages in Java, it is necessary to understand how it works. The web begins from HTTP (Hypertext Transfer Protocol), which is a basic set of rules for data communication. In this protocol data is represented as hypertext. This protocol has seven methods: GET, POST, HEAD, OPTIONS, TRACE, PUT, and DELETE. The most important for now are GET and POST.
These methods are used to transfer data. The difference is that the GET method is used to receive information from some resource, while the POST method is used to transmit data to a server to create or modify a resource. The obvious distinction is that when using the GET method, all the data inputted in the page is reflected in the URL, as submitting the username and password would be a bad practice. Unlike the GET method, the POST request does not change the URL, so no parameters are added to it.
An HTTP request looks like this:
GET /WebProject/HTTP HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
The first word specifies the HTTP method being used, and the host refers to the address of the server. The exact address is specified on the first line after the method, ending with the protocol version being used. User-agent is the parameter specifying the current browser.
HTTP response has the following representation:
HTTP/1.1 200 OK
Server: Apache Content-Language: uk
Content-Type: text/html; charset=UTF-8
Here we see the protocol and its version (an earlier version may be here if the receiver does not support a newer one). Then follows the status code and word explanation.
Web applications are based on the usage of HTTP, so it is important to understand the main principles of functioning. Now it is possible to transfer to the topic of servlets, as they are the primary units which allow you to build a web application in Java.
In simple words, a servlet is a Java class that extends HTTPServlet with the help of which it is possible to enhance the web server’s capacity and provide support for dynamic data processing and exchange. Servlets run on a web server inside a container usually called a servlet container. It is important to admit that besides HTTP protocol, servlets are based on the usage of markup language – HTML, as this is the primary language for designing any web page on the internet.
Servlet advantages are the following:
- For each request a new thread is created. This allows for better performance (compared to the older technology of Common Gateway Interface (CGI), where concurrency was the main issue).
- Servlets are cross-platform.
- Servlets are running inside of the Java Virtual Machine (JVM), which allows you to take advantage of its features (proper memory management, security of applications, etc.). In such a way, it is possible to launch a fully featured web application exploiting (mainly) one programming language along with all its peculiarities.
Each servlet has a life cycle as shown on the figure below. It starts with an initialization and instantiation of a servlet. When an instance of a servlet is created, init() method is called. After that, service() method is executed performing doGet(), doPost() methods which handle HTTP requests. When all operations are completed, method destroy() is executed.
This sample is presented for students who need help with introduction to Java Web. It will help you to understand Java programming language better. One of the ways you can use this Java servlet example is by using it as a tool to deal with your programming assignment. If you visit our blog, you will find other useful samples.
If you want to get professional help with your assignment, AssignmentShark is what you need. Your task will be done by an expert in the discipline you choose. In general, your studies will be much easier with our professional help. Avoid getting low grades by requesting our help.