Aim
To implement the three-tier application using servlets to display Student mark list in CS1404 INTERNET PROGRAMMING LABORATORY perform by the jdbc:odbc connection
Algorithm
Start the three-tier application in servlets
Create the student.html
Use the Form post method
In the Form when click submit it is link to
http://localhost:8080/Student/Student
Use the textbox get the student Roll no
Then create java coding Student.java for getting the data from the database
Use jdbc:odbc:Student connect to the database
And list out the data
Finally terminate the program for three-tier application inservlet
Note important files:
Student.html ,student.java,web.xml,student.war CS1404 INTERNET PROGRAMMING LABORATORY
Source code in html programming three tier application using servlets Displaying Student Details
<!—Student.html -->
<HTML>
<BODY>
<CENTER>
<FORM name = "students" method = "post"
action="http://localhost:8080/Student/Student">
<TABLE>
<tr>
<td><B>Roll No. </B> </td>
<td><input type = "textbox" name="rollno" size="25"
value=""></td>
</tr>
</TABLE>
<INPUT type = "submit" value="Submit">
</FORM>
<CENTER>
</BODY>
</HTML>
Source code in java programming connection to the database using jdbc.odbc
import javax.servlet.http.*;
import java.io.*;
import javax.servlet.*;
import java.sql.*;
public class Student extends HttpServlet {
Connection dbConn ;
public void doPost(HttpServletRequest req,
HttpServletResponse res)
throws IOException, ServletException
{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
dbConn =
DriverManager.getConnection("jdbc:odbc:Student","","") ;
}
catch(ClassNotFoundException e)
{
System.out.println(e);
}
catch(Exception e)
{
System.out.println(e);
}
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String mrollno = req.getParameter("rollno") ;
try {
PreparedStatement ps =
dbConn.prepareStatement("select * from stud where
rollno = ?") ;
ps.setString(1, mrollno) ;
ResultSet rs = ps.executeQuery() ;
out.println("<html>");
out.println("<body>");
out.println("<head>");
out.println("<title>Hello World!</title>");
out.println("</head>");
out.println("<body>");
out.println("<table border = 1>");
BB / REC - 55
while(rs.next())
{
out.println("<tr><td>Roll No. : </td>");
out.println("<td>" + rs.getString(1) +
"</td></tr>");
out.println("<tr><td>Name : </td>");
out.println("<td>" + rs.getString(2) +
"</td></tr>");
out.println("<tr><td>Branch : </td>");
out.println("<td>" + rs.getString(3) +
"</td></tr>");
out.println("<tr><td>10th Mark : </td>");
out.println("<td>" + rs.getString(4) +
"</td></tr>");
out.println("<tr><td>12th Mark : </td>");
out.println("<td>" + rs.getString(5) +
"</td></tr>");
}
out.println("</table>");
out.println("</body>");
out.println("</html>");
}
catch (Exception e)
{
System.out.println(e);
}
}
}
Database Structure 􀃆 Student.mdb
Records
D:\Student\WEB-INF\classes>javac Student.java
D:\PostParam\WEB-INF>type web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application
2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<!-- JSPC servlet mappings start -->
<servlet>
<servlet-name>Student</servlet-name>
<servlet-class>Student</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Student</servlet-name>
<url-pattern>/Student</url-pattern>
</servlet-mapping>
<!-- JSPC servlet mappings end -->
</web-app>
D:\PostParam>jar –cvf Student.war .
D:\Student>jar -cvf Student.war .
added manifest
adding: Student.html(in = 299) (out= 204)(deflated 31%)
adding: WEB-INF/(in = 0) (out= 0)(stored 0%)
adding: WEB-INF/classes/(in = 0) (out= 0)(stored 0%)
adding: WEB-INF/classes/Student.class(in = 2658) (out=
1358)(deflated 48%)
adding: WEB-INF/classes/Student.java(in = 1849) (out=
636)(deflated 65%)
adding: WEB-INF/classes/Student.mdb(in = 139264) (out=
7251)(deflated 94%)
adding: WEB-INF/web.xml(in = 666) (out= 312)(deflated 53%)
BB / REC - 58
Step 1: Create ODBC connection for the database
Step 2: Open Web Browser and type
Step 3: http://localhost:8080
Step 4: Select Tomcat Manager
Step 5: Deploy the war file and Run