source file name: Fact.html

<html>

    <head>

        <title>Factorial  program for Servlet</title>

        

    </head>

    <body>

        <form action="http://localhost:8080/ServletPrograms/FactServlet" method="post">

            enter any no:

            <input type="text" name="t1" >

            <br>

            <input type="submit" >

        </form>

    </body>

</html>

output:



source file name: FactServlet.java



import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet(urlPatterns = {"/FactServlet"})
public class FactServlet extends HttpServlet {

   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
           int n=Integer.parseInt(request.getParameter("t1"));
           int f=1; 
           out.println("given no is: "+n);
        
        for(;n>0;n--)
            f=f*n;
           out.println("<br> Factorial value  is: "+f);
        }
    }

    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

   
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

  
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

output: