Wednesday, 15 February 2012

three tier application using servlet for displaying student mark list

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
/**
 *
 * @author rational
 */
@WebServlet(name = "Student", urlPatterns = {"/Student"})
public class Student extends HttpServlet {
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String r1=request.getParameter("regno");
            int r2=Integer.parseInt(r1);
            //out.println(r2);
            //out.println("hai");
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
             Connection c=DriverManager.getConnection("jdbc:odbc:student","","");
             Statement st=c.createStatement();
             ResultSet rs=null;
            
             rs =st.executeQuery("select * from student where regno="+r2);
            //ResultSet rs=st.getResultSet();
            while(rs.next())
             {
            int reg=rs.getInt(1);
            String name=rs.getString(2);
            String sub1=rs.getString(3);
            String pass1=rs.getString(4);
            String grade1=rs.getString(5);
            String sub2=rs.getString(6);
            String pass2=rs.getString(7);
            String grade2=rs.getString(8);
            String sub3=rs.getString(9);
            String pass3=rs.getString(10);
            String grade3=rs.getString(11);
            String sub4=rs.getString(12);
            String pass4=rs.getString(13);
            String grade4=rs.getString(14);
            String sub5=rs.getString(15);
            String pass5=rs.getString(16);
            String grade5=rs.getString(17);
            String sub6=rs.getString(18);
            String pass6=rs.getString(19);
            String grade6=rs.getString(20);
            out.println("<center>");
            out.println("<h1>Name:");
            out.println(name);
            out.println("</h1>");
            out.println("<h2>Register Number:");
            out.println(reg);
            out.println("</h2>");
            out.println("<table border=1");
            out.println("<tr>");
            out.println("<td>");
            out.println("<p>Subject Name");
            out.println("</p>");
            out.println("</td>");
             out.println("<td>");
            out.println("<p>Grade");
            out.println("</p>");
             out.println("</td>");
              out.println("<td>");
            out.println("<p>PASS/FAIL");
            out.println("<p>");
            out.println("</td>");
            out.println("</tr>");
            out.println("<tr>");
            out.println("<td>");
            out.println("<p>"+sub1);
            out.println("</p>");
            out.println("</td>");
            out.println("<td>");
            out.println("<p>"+grade1);
            out.println("</p>");
             out.println("</td>");
              out.println("<td>");
            out.println("<p>"+pass1);
            out.println("</p>");
            out.println("</td>");
            out.println("</tr>");
            out.println("<tr>");
            out.println("<td>");
            out.println("<p>"+sub2);
            out.println("</p>");
             out.println("</td>");
              out.println("<td>");
            out.println("<p>"+grade2);
            out.println("</p>");
             out.println("</td>");
              out.println("<td>");
            out.println("<p>"+pass2);
            out.println("</p>");
            out.println("</td>");
            out.println("<td>");
            out.println("</tr>");
           out.println("<tr>");
            out.println("<td>");
            out.println("<p>"+sub3);
            out.println("</p>");
             out.println("</td>");
              out.println("<td>");
            out.println("<p>"+grade3);
            out.println("</p>");
             out.println("</td>");
              out.println("<td>");
            out.println("<p>"+pass3);
            out.println("</p>");
            out.println("</td>");
            out.println("<td>");
            out.println("</tr>");
            out.println("<tr>");
            out.println("<td>");
            out.println("<p>"+sub4);
            out.println("</p>");
             out.println("</td>");
              out.println("<td>");
            out.println("<p>"+grade4);
            out.println("</p>");
             out.println("</td>");
              out.println("<td>");
            out.println("<p>"+pass4);
            out.println("</p>");
            out.println("</td>");
            out.println("<td>");
            out.println("</tr>");
            out.println("<tr>");
            out.println("<td>");
            out.println("<p>"+sub5);
            out.println("</p>");
             out.println("</td>");
              out.println("<td>");
            out.println("<p>"+grade5);
            out.println("</p>");
             out.println("</td>");
              out.println("<td>");
            out.println("<p>"+pass5);
            out.println("</p>");
            out.println("</td>");
            out.println("<td>");
            out.println("</tr>");
            out.println("<tr>");
            out.println("<td>");
            out.println("<p>"+sub6);
            out.println("</p>");
             out.println("</td>");
              out.println("<td>");
            out.println("<p>"+grade6);
            out.println("</p>");
             out.println("</td>");
              out.println("<td>");
            out.println("<p>"+pass6);
            out.println("</p>");
            out.println("</td>");
            out.println("<td>");
            out.println("</tr>");
        }
          
            
           
           
           
           
           
           
           
           
           
          
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Student</title>"); 
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet Student at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {           
            out.close();
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
     @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Student.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Student.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Student.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Student.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
  /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}


    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}