Wednesday, 8 October 2014

Allowing user to download data in Excel Sheet format in Servlet




Form.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
      <form action="StudentCls">
            Enter your roll number : <input tpye="text" name="rollno">
            <input type="submit" value="Submit Roll No">
      </form>
</body>
</html>

/********************************************************************/

StudentCls.java
package StudentPkg;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class StudentCls
 */
@WebServlet("/StudentCls")
public class StudentCls extends HttpServlet {
      private static final long serialVersionUID = 1L;
      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudentCls() {
        super();
        // TODO Auto-generated constructor stub
    }

      /**
       * @see Servlet#init(ServletConfig)
       */
      public void init(ServletConfig config) throws ServletException {
            // TODO Auto-generated method stub
            try {
                  Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
           
      }

      /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      {
            int rollNo= Integer.parseInt(request.getParameter("rollno"));
            String name="";
            PrintWriter pw=response.getWriter();
            try
            {
                  Connection cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/college", "root", "your_password");
                  PreparedStatement pst=cn.prepareStatement("select*from marks where rollno=?");
                 
                  PreparedStatement pst1=cn.prepareStatement("select *from student where rollno=?");
                  pst.setInt(1,rollNo);
                  pst1.setInt(1,rollNo);
                 
                  ResultSet rs=pst.executeQuery();
                  ResultSet rs1=pst1.executeQuery();
                  response.setContentType("application/vnd.ms-excel");
                 
                  if(rs1.next())
                  {
                        name=rs1.getString("name");
                  }
                  pw.println("RollNo : "+rollNo);
                  pw.println("Name   : "+name);
                 
                  pw.println("Term Number"+"\t"+"Marks1"+"\t"+"Marks2"+"\t"+"Marks3"+"\t"+"Marks4");
                  while(rs.next())
                  {
                        pw.println(rs.getInt("semno")+"\t"+rs.getInt("marks1")+"\t"+rs.getInt("marks2")+"\t"+rs.getInt("marks3")+"\t"+rs.getInt("marks4"));
                  }
            } catch (SQLException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
      }

      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
      }

}

No comments:

Post a Comment