Practical 21: Write a program to demonstrate the use of HttpServlet as a parameterized Servlet

1) Develop servlet program to print Hello MSBTE in browser window.

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

public class HelloHttp extends HttpServlet
{
 public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
 {
  System.out.println("Http Servlet Program under Execution");
  
  response.setContentType("text/html");
  PrintWriter pw = response.getWriter(); 

  pw.println("Website shifted to codingatharva.blogspot.com");
  pw.close(); 
 }
}


Web.xml :
<web-app>
    <servlet>
        <servlet-name> simple </servlet-name>
        <servlet-class> HelloHttp </servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name> simple </servlet-name>
        <url-pattern>/check </url-pattern>
    </servlet-mapping>

</web-app>
2) Develop a program to receive the parameter through HTML forms and send back received parameter to browser.

Program:

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;
import java.util.*;

public class StudentSrv extends HttpServlet {

 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
        
        Enumeration en = request.getParameterNames();
        
        while(en.hasMoreElements())
        {
            String paramname = (String) en.nextElement();
            String paramvalue[] =  request.getParameterValues(paramname);
            out.println("
"+paramname);
            
            for(int i = 0; i < paramvalue.length; i++) {
               out.println(paramvalue[i]+"
");
            }
        }   
        
  out.close();
 }
 public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      
      doGet(request, response);
   }

}



web.xml :
<web-app>
    <servlet>
        <servlet-name> StudentSrv </servlet-name>
        <servlet-class>StudentSrv </servlet-class>
    
    </servlet>

    <servlet-mapping>
        <servlet-name> StudentSrv </servlet-name>
        <url-pattern> /StudentSrv </url-pattern>
    </servlet-mapping>
</web-app>




StudentRegistration.html:

<html>

<body bgcolor="#ccc">

    <form action="./StudentSrv" style="border:2px solid #e600e6">
        <center>
            <h1 style="border:4px solid #e600e6;border-style: outset"> Student Registration Form </h1>
            <h2>
                <u> Enter Name:</u> <input type="text" name="name" /> <br>

                <br>
                <u> Enter About YourSelf:</u>
                <textarea rows="4" cols="50" name="YourSelf"> </textarea><br>

                <br>
                <u> Select Gender:<br></u>
                <input type="radio" name="gender" value="male"> Male<br>
                <input type="radio" name="gender" value="female"> Female<br>
                <input type="radio" name="gender" value="other"> Other<br>
                <br>

                <u> Select Qualification:</u> <select>
                    <option value="10">10th</option>
                    <option value="12">12th</option>
                    <option value="Diploma">Diploma</option>
                    <option value="Btech">Btech</option>
                </select><br />

                <br />
                <u> Check All Programming Language Knows:</u> <br />
                <input type="checkbox" name="lang1" value="C"> C Programming<br>
                <input type="checkbox" name="lang2" value="Java"> Java Programming<br>
                <input type="checkbox" name="lang3" value="Python" checked> Python Programming<br>
            </h2>
            <input type="submit"/>
            <input type="reset"/>
        </center>
    </form>
</body>

</html>

Previous
Next Post »