0% found this document useful (0 votes)
62 views4 pages

Servlet

The document describes two servlets - one to display a form for entering department details and submitting to a database, and another to process the form submission and display existing records. The first servlet, Form.java, displays an HTML form with fields for department number, name, and location, and a submit button. It submits the form to the second servlet. The second servlet, Process.java, gets the submitted form data, inserts it into the DEPT database table using JDBC and PreparedStatement. It then queries the table and displays all existing records in an HTML table.

Uploaded by

Kailas Lokhande
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views4 pages

Servlet

The document describes two servlets - one to display a form for entering department details and submitting to a database, and another to process the form submission and display existing records. The first servlet, Form.java, displays an HTML form with fields for department number, name, and location, and a submit button. It submits the form to the second servlet. The second servlet, Process.java, gets the submitted form data, inserts it into the DEPT database table using JDBC and PreparedStatement. It then queries the table and displays all existing records in an HTML table.

Uploaded by

Kailas Lokhande
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Servlet + JDBC

Q.Write two servlets in which one servlet will display a form in which data entry can
be done for the field’s dept-no, dept-name and location. In the same form place a button
called as submit and on click of that button this record should be posted to the table
called as DEPT in the database. This inserting of record should be done in another
servlet. The second servlet should also display all the previous record entered in the
database?

"Servlet Form.java"

import javax.servlet.*;
import java.io.*;

public class ServletForm extends GenericServlet


{

public void service(ServletRequest req, ServletResponse res)


throws IOException, ServletException
{
PrintWriter pw=res.getWriter();
res.setContentType("text/html");

pw.println("<html>");
pw.println("<body bgcolor=lightgreen>");
pw.println("<font color=red><h1> Department Details </h1></font>");
pw.println("<form method=post
action=http://localhost:8080/examples/servlet/ServletProcess>");
pw.println("<table>");
pw.println("<tr>");
pw.println("<td>Enter Dept. Number - ");
pw.println("<td><input type=text name=deptno size=20>");
pw.println("</tr>");

pw.println("<tr>");
pw.println("<td>Enter Dept. Name - ");
pw.println("<td><input type=text name=deptnm size=20>");
pw.println("</tr>");

pw.println("<tr>");
pw.println("<td>Enter Dept. Location - ");
pw.println("<td><input type=text name=loc size=20>");
pw.println("</tr>");

pw.println("<tr>");
pw.println("<td><input type=submit value=Submit>");
pw.println("<td> <input type=reset value=Reset>");
pw.println("</tr>");

pw.println("</table></form></body></html>");

"SevletProcess.java"

import javax.servlet.*;
import java.io.*;
import java.sql.*;

public class ServletProcess extends GenericServlet


{
private Connection con;
private PreparedStatement pst;
private Statement stmt;
private ResultSet rs;

public void init(ServletConfig sc)


{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:ServletDSN");
pst=con.prepareStatement("insert into dept values (?, ?, ?)");
stmt=con.createStatement();
}
catch(Exception ex)
{

public void service(ServletRequest req, ServletResponse res)


throws IOException, ServletException
{
PrintWriter pw=res.getWriter();
res.setContentType("text/html");

String dno=req.getParameter("deptno");
String dnm=req.getParameter("deptnm");
String dloc=req.getParameter("loc");

try
{
pst.setString(1,dno);
pst.setString(2,dnm);
pst.setString(3,dloc);

pst.executeUpdate();

rs=stmt.executeQuery("select * from dept");

pw.println("<table border=1>");
pw.println("<tr><th>Department Number</th><th>Department
Name</th><th>Location</th></tr>");
while(rs.next())
{
pw.println("<tr><td>"+rs.getString(1)+"<td>"+rs.getString(2)+"<td>"+rs.getString(3)+"
</tr>");
}
pw.println("</table>");
}

catch(Exception ex)
{
pw.println(""+ex);
}

You might also like