7. Develop Servlet application to accept HTNO of a student from client and display the memorandum of marks from the server

STUDENT MARKLIST USING SERVLET
AIM:
To create a three tier application for displaying the student marklist.
ALGORITHM:
Client:
Step1: In index.html on the client side declare the contents that you like to transfer to the
server using html form and input type tags.
Step2: create a submit button and close all the included tags.
Servlet:
Step 1: Import all necessary packages
Step 2: Define a class that extends servlet
Step 3: In the doPost() method, do the following:
i) Set the content type of the response to "text/html"
ii) connect with the database which has the student marklist
iii) query the data to the database
Step 4: Display the student marklist.
PROGRAM:
SERVLET CODE:
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class serv extends HttpServlet
{
String message,Reg_no;
Connection connect;
Statement stmt=null; ResultSet
rs=null;
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
try
{
String url="jdbc:odbc:NEO";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connect=DriverManager.getConnection(url," "," "); message="Mark List";
}
catch(ClassNotFoundException cnfex){
cnfex.printStackTrace();
}
catch(SQLException sqlex)
{
sqlex.printStackTrace();
}
catch(Exception excp)
{
excp.printStackTrace();
}
Reg_no=request.getParameter("regno");
response.setContentType("text/html"); PrintWriter
out=response.getWriter(); out.println("<html>");
out.println("<head>"); out.println("</head>");
out.println("<body bgcolor=cyan>");
out.println("<center>");
out.println("<h1>"+message+"</h1>\n");
try
{
Statement stmt=connect.createStatement();
String query=new String("SELECT * FROM MarkList WHERE regno= "+Reg_no);
rs=stmt.executeQuery(query);
boolean b=rs.next();
out.println("Regsitration No:"+rs.getInt(1)); out.println("<br>"+"Name:"+rs.getString(2));
out.println("<table
border=5>"); out.println("<th>"+"cs01"+"</th>"); out.println("<th>"+"cs02 "+"</th>");
out.println("<th>"+"cs03"+"</th>"); out.println("<th>"+"status"+"</th>");
while(b)
{
out.println("<tr>"); out.print("<td>"+rs.getString(3)+"</td>");
out.print("<td>"+rs.getString(4)+"</td>");
out.print("<td>"+rs.getString(5)+"</td>");
out.print("<td>"+rs.getString(6)+"</td>"); out.println("</tr>");
b=rs.next();
}
out.println("</table>");
}
catch(SQLException ex){
out.println("error in connection");
}
finally
{
try
{ if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(connect!=null)
connect.close();
}
catch(SQLException e){ }
}
out.println("</center>");
out.println("</body></html>");
}
}
HTML CODE:
<html>
<head><title> mark sheet</title></head>
<body><center>
<h1>Student Mark Sheet</h1>
</center><form action="serv" method="POST">
registration number:<input type="text" name="regno">
<input type="submit" value="Submit"><br><br>
</form></body></html>
OUTPUT:
Result:
Thus the given program was coded and executed successfully