163 subscribers
3.42K photos
24 videos
39 files
371 links
Link: @java_posts

Contact: java.response.email@gmail.com
Download Telegram
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.
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:
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.
Where is URL
URL stands for Universal Resource Locator used to locate the server and resource. It is an address of a web page. Every web page on a project must have a unique name.

A URL looks like as follows:
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.
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:
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.
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:
First Web Application Using Java Servelet
To create a web application, we need the following tools:

Java

IDE ( Eclipse or Netbeans)

Database (Oracle or Mysql)

Server (Tomcat)
Before Creating any web application, ensure that all of the above tools are properly installed on your system.

Now, follow the below steps to develop a web application:

Step1: Open Eclipse Create a Dynamic Web Project

Open the Eclipse IDE, navigate to File-> New-> Dynamic Web Project.
if the dynamic web project is not listed in your IDE, then go to the other option and search for it. Click on it to continue.
Step2: Provide Project Name
Now, enter the project name and click Next to continue.

Follow the prompt and tick the generate web.xml deployment descriptor.
Now, our project is ready; the project structure will look as follows:
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.
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
}
/
* @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.
Click Open to continue.

Now select Apply and Close option. It will add the jar file to our project.

Step5: Create a HTML or JSP file

Now, our first web application is almost ready. We can create HTML pages that we want to display on our website.

To create an HTML page, right-click on the WebContent folder and select the New HTML file option from the New-> HTML File menu with the name index.html.