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

Contact: java.response.email@gmail.com
Download Telegram
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.
<!DOCTYPE html>
<html>
<head>
<meta charset=<em>"UTF-8"</em>>
<title>First Web Application</title>
</head>
<body>
<h1>Welcome</h1>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">News</a></li>
<li><a href="#">Contact</a></li>
<li><a href="#">About</a></li>
</ul>
</body>
</html>
Step6: Map the File

Now, map this file in the web.xml file. The web.xml is a deployment descriptor for the Servlet applications. Since, Servlet 3.0, we can use annotations instead of the deployment descriptor.

To map a servlet, we have to provide the servlet details such as Servlet name and class. Consider the below code:
<servlet>
<servlet-name>MyHttpTestServlet</servlet-name>
<servlet-class>TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyHttpTestServlet</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
We can also define our welcome file; a welcome file is the first file of the project that initiates the project, also known as Home. We can define multiple welcome files.

Consider the below code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>BasicWebApplication</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>MyHttpTestServlet</servlet-name>
<servlet-class>TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyHttpTestServlet</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
From the above code, we can see by default the servlet defines several welcome files. If you want to use any file other than the listed files, you can define that here.

Now, our first web application is ready.

Step7: Run the Application

To run the application, right-click on the project and run it on the server by selecting Run-> Run on Server option.
It will take some time to load the application