Open In App

How to Create Servlet in MyEclipse IDE?

Servlets are Java programs that extend the capabilities of a Web Server. It runs on a Java-enabled web server like Apache Tomcat server, to receive and respond to the client’s requests. With Servlets, we can create Enterprise-grade applications with database access, session management, and content generation. It is primarily used for HTTP requests and responses like GET, POST, PUT, and DELETE.

In this article, we will create a Servlet in the MyEclipse IDE and deploy it to an Apache Tomcat Web Server.



Prerequisites:

There are some prerequisites that should be done to follow this tutorial. They are,

  • MyEclipse IDE should be installed on your system. You can download and set it up by visiting the below link: MyEclipse
  • Any Java-enabled Web Server should be installed, it can be Apache Tomcat, GlassFish, etc. In this example, we will be using the popular option, Apache Tomcat to serve our Servlet. You can download and set it up easily by visiting the below link: Apache Tomcat
  • MyEclipse IDE should be configured to use the Apache Tomcat Local Installation done in Prerequisite 2 for publishing the developed artifacts.

Once you have done all the above steps, we can start creating our Servlet in MyEclipse IDE.



Creating Servlet in MyEclipse

We will be creating a basic Servlet that hosts a simple Login page and we will display the entered information on the next page using the developed Servlet.

Step 1: Create a Dynamic Web Project

Step 2: Create Servlet Class

Step 3: Defining the servlet logic.




<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My Login page</title>
<style>
.row {
    margin: 1rem 0;
}
</style>
</head>
  
<body>
    <h1 class="row">Welcome to GeeksForGeeks!!!</h1>
    <div style="padding:20px">
        <form action="Login">
            <div class="row">
                <h3>Enter your username</h3>
                <input name="userName" type="text">
            </div>
            <div class="row">
                <h3>Enter your password</h3>
                <input name="password" type="password">
            </div>
            <div class="row">
                <button type="submit">Login</button>
            </div>
        </form>
    </div>
</body>
</html>
  
<!-- This code is contributed by ragul21 -->




package com.example;
  
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
  
@WebServlet("/Login")
public class Login extends HttpServlet {
    private static final long serialVersionUID = 1L;
         
    public Login() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        String userName=request.getParameter("userName");
        String password=request.getParameter("password");
        out.print("<html><body>");
        out.print("<h1>Hello!!!</h1>");
        out.printf("<h3>User Name with : %s </h3><h3>Your password is : %s</h3>",userName,password);
        out.print("</body></html>");
    }
}

Step 4: Running our project.

Output

The result for the specific username and password.


Article Tags :