PROGRAM IN SERVLET TO REMOVE A CHARACTER FROM THE GIVEN STRING

INDEX.HTML

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
       
        <form method="get" action="removal">
            Enter any string:<input type="text"  name="s1"><br>
            Enter the character to remove from the string:<input type="text" name="c1">><br>
            <input type="submit" value="click me">
        </form>
       
    </body>
</html>


SERVLET PROGRAM:
REMOVAL.JAVA

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

public class removal extends HttpServlet {

       protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet removal</title>");           
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet removal at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }
 
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
     
         response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            String s2=request.getParameter("s1");
            String c2=request.getParameter("c1"); 
           
            int ind=0;
            int leng=s2.length();
            char ch1 = c2.charAt(0);
            for(int i=1;i<=leng;i++){
                char ch=s2.charAt(i);
                if (ch1 == ch ){
                    ind=i;
                    out.println("its found in the index"+i);
                    break;
                }
            }
            out.println("<html> <body> <br> </body> </html>");
            out.println(" String before removal of given character:"+s2);
            out.println("<html> <body> <br> </body> </html>");
            out.println(" string after removal is   "+s2.substring(0,ind)+s2.substring(ind+1));
         
           
        }
     

    }




OUTPUT:




No comments:

Post a Comment