Saturday, May 22, 2010

MVC with mysql Servlet and JSP

In this example I will be creating a simple application in which I will transfer the control from main page to controller servlet where it will establish database connection and store the result to a bean class.It will put the object to the session attribute and then transfer the control to jsp page where the result will be displayed.

Lets create a Home Page called a.jsp under WebContents which contains a link. This link will redirect the flow to a.java file.
a.jsp
-----------------------------------------------------------------------------------------------------------









<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="a">click me</a>
</body>
</html>    
                                          


------------------------------------------------------------------------------------------------------------



 a.java

------------------------------------------------------------------------------------------------------------
package mit.mvc;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class a extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
{
       PrintWriter out = response.getWriter();
       Connection con;
       ResultSet rs;
       DbBean bb=new DbBean();
       int[] id=new int[10];
      
       try
       {             con=DriverManager.getConnection("jdbc:mysql://localhost/student","root", "");
             
              Statement st=con.createStatement();
              rs=st.executeQuery("select * from stud");
              int i=0;
              while(rs.next())
              {             id[i]=Integer.parseInt(rs.getString("id"));
                     i++;
              }
             
              bb.setid(id);
       }catch(Exception e)
       {}
       HttpSession s=request.getSession();
       s.setAttribute("result", bb);
       RequestDispatcher dispatcher = request.getRequestDispatcher("c.jsp");
       dispatcher.forward(request, response);
}

public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
{
       doGet(request,response);
}
}              
-------------------------------------------------------------------------------------------------------

Explanation : For this application to work we need to create a student database with stud table. stud table have id and name column.
This file simply creates database connection and creates and walk through the stud table and puts the id that is  fetched from the database into integer array id. After that DbBean class is initialized and its id attribute is set to the the id attribute of the servlets class. The initialized bean object is putted into session and the control is transfered to c.java

Lets see DbBean.java
------------------------------------------------------------------------------------------------------------------------









package mit.mvc;
import java.io.*;
public class DbBean {
     
      private int[] id=new int[10];
      public void setid(int[] id)
      {
            this.id=id;
      }
      public int getid(int i)
      {
            return id[i];
      }    
}          
----------------------------------------------------------------------------------------------------
c.java
----------------------------------------------------------------------------------------------------









<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page language="java" import="java.sql.*" %>

<%@page import="mit.mvc.DbBean"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<jsp:useBean id="result" scope="session" type="mit.mvc.DbBean"/>

<%=result.getid(1) %>

</body>
</html>

No comments:

Post a Comment