What is a Web Application
A web application is computer software that can be accessed using any web browser. Usually, the frontend of a web application is created using the scripting languages such as HTML, CSS, and JavaScript, supported by almost all web browsers. In contrast, the backend is created by any of the programming languages such as Java, Python, Php, etc., and databases. Unlike the mobile application, there is no specific tool for developing web applications; we can use any of the supported IDE for developing the web application.
A web application is computer software that can be accessed using any web browser. Usually, the frontend of a web application is created using the scripting languages such as HTML, CSS, and JavaScript, supported by almost all web browsers. In contrast, the backend is created by any of the programming languages such as Java, Python, Php, etc., and databases. Unlike the mobile application, there is no specific tool for developing web applications; we can use any of the supported IDE for developing the web application.
Web Server and Client
The web server is a process that handles the client's request and responds. It processes the request made by the client by using the related protocols. The main function of the webserver is to store the request and respond to them with web pages. It is a medium between client and server. For example, Apache is a leading webserver.
A client is a software that allows users to request and assist them in communicating with the server. The web browsers are the clients in a web application; some leading clients are Google Chrome, Firefox, Safari, Internet Explorer, etc.
The web server is a process that handles the client's request and responds. It processes the request made by the client by using the related protocols. The main function of the webserver is to store the request and respond to them with web pages. It is a medium between client and server. For example, Apache is a leading webserver.
A client is a software that allows users to request and assist them in communicating with the server. The web browsers are the clients in a web application; some leading clients are Google Chrome, Firefox, Safari, Internet Explorer, etc.
HTML and HTTP
The HTML stands for HyperText Markup Language; it is a common language for Web Server and Web Client communication. Since both the web server and web client are two different software components of the web, we need a language that communicates between them.
The HTTP stands for HyperText Transfer Protocol; it is a communication protocol between the client and the server. It runs on top of the TCP/IP protocol.
Some of the integral components of an HTTP Request are as following:
The HTML stands for HyperText Markup Language; it is a common language for Web Server and Web Client communication. Since both the web server and web client are two different software components of the web, we need a language that communicates between them.
The HTTP stands for HyperText Transfer Protocol; it is a communication protocol between the client and the server. It runs on top of the TCP/IP protocol.
Some of the integral components of an HTTP Request are as following:
HTTP Method: The HTTP method defines an action to be performed; usually, they are GET, POST, PUT, etc.
URL: URL is a web address that is defined while developing a web application. It is used to access a webpage.
Form Parameters: The form parameter is just like an argument in a Java method. It is passed to provide the details such as user, password details on a login page.
URL: URL is a web address that is defined while developing a web application. It is used to access a webpage.
Form Parameters: The form parameter is just like an argument in a Java method. It is passed to provide the details such as user, password details on a login page.
http://localhost:8080/SimpleWebApplication/
Where,
http or https: It is the starting point of the URL that specifies the protocol to be used for communication.
Localhost: The localhost is the address of the server. When we run our application locally, it is called localhost; if we deployed our project over the web, then it is accessed by using the domain name like "javapoint.com". The domain name maps the server to IP address.
Where,
http or https: It is the starting point of the URL that specifies the protocol to be used for communication.
Localhost: The localhost is the address of the server. When we run our application locally, it is called localhost; if we deployed our project over the web, then it is accessed by using the domain name like "javapoint.com". The domain name maps the server to IP address.
8080: This is the port number for the local server; it is optional and may differ in different machines. If we do not manually type the port number in the URL, then by default, the request goes to the default port of the protocol. Usually, the port no between 0 to 1023 are reserved for some well-known services such as HTTP, HTTPS, FTP, etc.
We have discussed all the major components of a web application. Let's move towards our main motive How to build a web application in Java.
First, understand servlet:
We have discussed all the major components of a web application. Let's move towards our main motive How to build a web application in Java.
First, understand servlet:
What is Servelet ?
A Servlet is a Java program that runs within a web server; it receives the requests and responds to them using related protocols (Usually HTTP). The Servlets are capable enough to respond to any type of request; they are commonly used to make the application functional.
We can create a static website using only HTML and CSS, but when it comes to dynamic, we need a server-side programming language. For these applications, Java provides Servlet technology, which contains HTTP-specific servlet classes.
The javax.servlet and javax.servlet.http packages contain interfaces and classes for creating servlets. All servlets should implement the Servlet interface, which defines life-cycle methods. To implement a generic service, we can use the GenericServlet class by extending it. It provides doGet and doPost methods to handle HTTP-specific services.
A Servlet is a Java program that runs within a web server; it receives the requests and responds to them using related protocols (Usually HTTP). The Servlets are capable enough to respond to any type of request; they are commonly used to make the application functional.
We can create a static website using only HTML and CSS, but when it comes to dynamic, we need a server-side programming language. For these applications, Java provides Servlet technology, which contains HTTP-specific servlet classes.
The javax.servlet and javax.servlet.http packages contain interfaces and classes for creating servlets. All servlets should implement the Servlet interface, which defines life-cycle methods. To implement a generic service, we can use the GenericServlet class by extending it. It provides doGet and doPost methods to handle HTTP-specific services.
Why are servelets Useful ?
Web servers are capable enough to serve static HTML requests, but they don't know how to deal with dynamic requests and databases. So, we need a language for dynamic content; these languages are PHP, Python, Java, Ruby on Rails, etc. In Java, there are two technologies Servlet and JSPs, that deals with dynamic content and database. Java also provides frameworks such as Spring, Spring Boot, Hibernate, and Struts to use the servlet and JSP easily.
The Servlets and JSPs are server-side technologies that extend the functionality of a web server. They support dynamic response and data persistence. We can easily create a web application using these technologies.
Let's create our first web applications:
Web servers are capable enough to serve static HTML requests, but they don't know how to deal with dynamic requests and databases. So, we need a language for dynamic content; these languages are PHP, Python, Java, Ruby on Rails, etc. In Java, there are two technologies Servlet and JSPs, that deals with dynamic content and database. Java also provides frameworks such as Spring, Spring Boot, Hibernate, and Struts to use the servlet and JSP easily.
The Servlets and JSPs are server-side technologies that extend the functionality of a web server. They support dynamic response and data persistence. We can easily create a web application using these technologies.
Let's create our first web applications:
Step 3: Create a Servelet
Now, create a servlet by right-clicking on the Java Resources/src folder. To create a servlet right click on the src folder and navigate to the New-> Servlet menu. Here, provide the Servlet name:
Click on the Finish button. It will create a TestServlet as specified. You can choose any of your Servlet names.
Now, create a servlet by right-clicking on the Java Resources/src folder. To create a servlet right click on the src folder and navigate to the New-> Servlet menu. Here, provide the Servlet name:
Click on the Finish button. It will create a TestServlet as specified. You can choose any of your Servlet names.
import java.io.IOException;
import javax.servelet.ServeletException;
import javax.servelet.annotation.WebServelet;
import javax.servelet.HttpServelet;
import javax.servelet.http.HttpServeletRequest;
import javax.servelet.http.HttpServeletResponse;
/
* Servelet implementation class TestServelet
*/
@WebServelet("/TestServelet")
public class TestServelet extends HttpServlet {
private static final long serialVersionUID = 1L;
/
* @see HttpServelet#HttpServelet()
*/
public TestServelet() {
super()'
// TODO Aut-generated constructor stub
}
import javax.servelet.ServeletException;
import javax.servelet.annotation.WebServelet;
import javax.servelet.HttpServelet;
import javax.servelet.http.HttpServeletRequest;
import javax.servelet.http.HttpServeletResponse;
/
* Servelet implementation class TestServelet
*/
@WebServelet("/TestServelet")
public class TestServelet extends HttpServlet {
private static final long serialVersionUID = 1L;
/
* @see HttpServelet#HttpServelet()
*/
public TestServelet() {
super()'
// TODO Aut-generated constructor stub
}
/
* @see HttpServelet#doGet(HttpServeletRequest request, HttpServeletResponse
*/
protected void doGet(HttpServeleyRequest request, HttpServeletResponse response) throws ServeletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/
* @see HttpServelet#doPost(HttpServelet request, HttpServeletResponse respons)
*/
protected void doPost(HttpServeletRequest request, HttpServeletResponse response) throws ServeletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
* @see HttpServelet#doGet(HttpServeletRequest request, HttpServeletResponse
*/
protected void doGet(HttpServeleyRequest request, HttpServeletResponse response) throws ServeletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/
* @see HttpServelet#doPost(HttpServelet request, HttpServeletResponse respons)
*/
protected void doPost(HttpServeletRequest request, HttpServeletResponse response) throws ServeletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Step4: Add the Serveler jar file
We can see our servelet is displaying lots of errors it is because we have not added the servelet-api jar file yet. To add the jar. right-click on the project and select the configuration option by navigation to built path -> configure build path option. Now, click on the Add External JARs option.
We can see our servelet is displaying lots of errors it is because we have not added the servelet-api jar file yet. To add the jar. right-click on the project and select the configuration option by navigation to built path -> configure build path option. Now, click on the Add External JARs option.