Java, .NET, PHP, PYTHON, ANGULAR, .NET, ML, Data Scince, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

How we take form input in servlet  :-

We can write HTML Element under Servlet tag using double quote and single quote.

public class SwapServlet extends HttpServlet {

   public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException
   {
       response.setContentType("text/html");
     
      
       PrintWriter out = response.getWriter();
       out.write("<form action='#' method='get'>");
       out.write("<input type='text' name='txtnum1' placeholder='enter first number' /><br><br>");
       out.write("<input type='text' name='txtnum2' placeholder='enter second number' /><br><br>");
       out.write("<input type='submit' name='btnsubmit' value='swap' /><br>");
       out.write("</form>");
       try
       {
      
       int a,b;
       a = Integer.parseInt(request.getParameter("txtnum1"));
       b = Integer.parseInt(request.getParameter("txtnum2"));
       out.write("before swap result of a ="+a+" result of b = "+b +"<br>");
       a=a+b;
       b=a-b;
       a=a-b;
       out.write("after swap result of a ="+a+" result of b = "+b +"<br>");
       }
       catch(Exception ex)
       {
           out.print("textfiled can not be null");
       }
   }

}

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post