Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Html and Servlet to demonstrate invoking a servlet from a html. in Java

  1. Sevlet Code:
  2.  
  3. import java.io.*;
  4.  
  5. import java.util.*;
  6.  
  7. import javax.servlet.*;
  8.  
  9. public class PostParam extends GenericServlet
  10.  
  11. {
  12.  
  13. public void service(ServletRequest request,ServletResponse response) throws
  14. ServletException,IOException
  15.  
  16. {
  17.  
  18. PrintWriter pw = response.getWriter();
  19.  
  20. Enumeration e = request.getParameterNames();
  21.  
  22. while(e.hasMoreElements())
  23.  
  24. {
  25.  
  26. String pname = (String)e.nextElement();
  27.  
  28. pw.print(pname + " = ");
  29.  
  30. String pvalue = request.getParameter(pname);
  31.  
  32. pw.println(pvalue);
  33.  
  34. }
  35.  
  36. pw.close();
  37.  
  38. }
  39.  
  40. }


  1. HTML CODE:
  2.  
  3. <HTML>
  4.  
  5. <head>
  6.  
  7. <TITLE>INVOKING SERVLET FROM HTML</TITLE>
  8.  
  9. </head>
  10.  
  11. <BODY>
  12.  
  13. <CENTER>
  14.  
  15. <FORM name = "PostParam" method = "Post" action="http://localhost:8084/IP/PostParam">
  16.  
  17. <TABLE>
  18.  
  19. <tr>
  20.  
  21. <td><B>Employee </B> </td>
  22.  
  23. <td><input type = "textbox" name="ename" size="25"
  24.  
  25. value=""></td>
  26.  
  27. </tr>
  28.  
  29. <tr>
  30.  
  31. <td><B>Phone </B> </td>
  32.  
  33. <td><input type = "textbox" name="phoneno" size="25"
  34.  
  35. value=""></td>
  36.  
  37. </tr>
  38.  
  39. </TABLE>
  40.  
  41. <INPUT type = "submit" value="Submit">
  42.  
  43. </FORM>
  44.  
  45. </CENTER>
  46.  
  47. </body>
  48.  
  49. </html>