Advanced Java Programming Practical 22

Question 1
Develop servlet program to retrieve data from List and Radio Button using HTML Forms.
EmployeeData.java
Java
				import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class EmployeeData extends HttpServlet 
{
    public void service(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
		String name = request.getParameter("ename");
		String gender=request.getParameter("gender");
		String[] lang=request.getParameterValues("lang");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Employee Information</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Employee Information</h1>");
		out.println("<h3>Name: "+name+"</h3>");
		out.println("<h3>Gender: "+gender+"</h3>");
		out.print("<h3>Languages: <ol>");
		for(int i=0;i<lang.length;i++)
			out.print("<li>"+lang[i]+"</li>");
		out.print("</ol></h3>");
		out.println("</body>");
        out.println("</html>");
    }
}
			
EmployeeData.html
HTML
				<html>
<head>
<title>Employee Data</title>
</head>
<body>
<center>
<h2>Enter Employee Information</h2>
<form name="Form1" method="post" action="http://localhost:8080/examples/servlets/servlet/EmployeeData">
<table>
<tr>
<td><B>Employee Name:</td>
<td><input type=text name="ename"></td>
</tr>
<tr>
<td><B>Employee Gender:</td>
<td>
<input type="radio" name="gender" value="Male">Male
<input type="radio" name="gender" value="Female">Female
</td>
</tr>
<tr>
<td><B>Communication Languages:</td>
<td>
<select name="lang" multiple>
    <option value="Marathi">Marathi</option>
    <option value="English">English</option>
    <option value="Hindi">Hindi</option>
    <option value="Tamil">Tamil</option>
    <option value="Gujrathi">Gujrathi</option>
	<option value="Punjabi">Punjabi</option>
  </select>
</td>
</tr>
<tr>
<td></td>
<td>
<input type=submit value="Submit">
</td>
</tr>
</table>
</form>
</body>
</html>
			
Output
Question 2
Develop a program to receive student subject marks through HTML forms TextField and send the response as passed or Failed in Examination.
MarksDemo.java
Java
				import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class MarksDemo extends HttpServlet {

    public void service(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
		String name = request.getParameter("sname");
		int marks=Integer.parseInt(request.getParameter("smarks"));
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Student Marks Example</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Student Marks Example</h1>");
		out.println("<h2>Wlecome "+name+"</h2>");
		if(marks>=40)
			out.println("<h2>Your have passed the examination</h2>");
		else
			out.println("<h2>Your have failed the examination</h2>");
		out.println("</body>");
        out.println("</html>");
    }
}
			
MarksDemo.html
HTML
				<html>
<head>
<title>Students Marks</title>
</head>
<body>
<center>
<h2>Enter Student Marks</h2>
<form name="Form1" method="post" action="http://localhost:8080/examples/servlets/servlet/MarksDemo">
<table>
<tr>
<td><B>Student Name:</td>
<td><input type=text name="sname"></td>
</tr>
<tr>
<td><B>Student Marks:</td>
<td><input type=number name="smarks"></td>
</tr>
</table>
<input type=submit value="Submit">
</form>
</body>
</html>
			
Output

Leave a Comment

Your email address will not be published. Required fields are marked *