Switch to full style
Project under GPL source codes are posted here
Post a reply

Guest Book Application Using Servlets

Fri Oct 17, 2008 1:20 am

* Project Name:   Guest Book Application Using Servlets
* Programmer:   Satish.K
* Type:   Algorithms
* Technology:  Java
* IDE:   Any
Code:
// guestbk.java //

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class guestbk extends httpservlet
{
   Connection con;
   public void doPost(HttpServletRequest req,HttpServletResponse res)
throws
IOException,ServletException
   {
      PrintWriter out=new PrintWriter(res.getOutputStream());
      int ctr=0;
      res.setContentType("text/html");
   try
   {
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         con=DriverManager.getConnection("jdbc:odbc:booksrc","","");
         Statement stmt=con.createStatement();
         ResultSet rs=stmt.executeQuery("select * from gbook");
         out.println("<Html>");
      out.println("<Head>");
         out.println("<Title>GuestBook</Title>");
         out.println("</Head>");
         out.println("<Table border=1 align=center >");
         out.println("<H4><B><center>GUEST BOOK DETAILS
</center></B></H4><BR>");
         out.println("<TR><TD><b>E-Mail
Address</b></TD><TD><b>Comment</b></TD></TR>");
      while(rs.next())
      {
          ctr++;
          String email=rs.getString("emailid");
          String cmt=rs.getString("comment");
          out.println("<TR><TD>"+email+"</TD><TD>"+cmt+"</TD></TR>");
   }
          out.println("</Table>");
   if(ctr==0)
         out.println("<BR>No Records Found<BR>");
   stmt.close();
       con.close();
     }
         catch(SQLException SQLExcep)
      {
          System.out.println("error");
      }
         catch(ClassNotFoundException CNFE)
      {
          System.out.println("error");
      }
         out.println("</Html>");
         out.close();
     }
}
Code:
// submit.java //
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class submit extends HttpServlet
{
   Connection con;
   public void doPost(HttpServletRequest req,HttpServletResponse res)
throws
IOException,ServletException
{
   String str1,str2;
   str1=req.getParameter("emailid");
   str2=req.getParameter("comment");
   System.out.println(str1);
   System.out.println(str2);
   res.setContentType("text/html");
   PrintWriter out=res.getWriter();
   out.println("<html><h2>Guest Book is Successfully
Updated</h2></html>");
   try
     {
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      con=DriverManager.getConnection("jdbc:odbc:booksrc","","");
      Statement stmt=con.createStatement();
      ResultSet rs;
      int i= stmt.executeUpdate("insert into gbook
values('"+str1+"','"+str2+"')");
        if(i==1)
      {
         System.out.println("ok");
      }
      stmt.close();
      con.close();
      }
         catch(SQLException SQLExcep)
     {
              System.out.println("error");
      }
         catch(ClassNotFoundException CNFE)
      {
          System.out.println("error");
      }}
}

//guestbk.htm//
<Html>
<Head>
<Title>GUEST BOOK</Title>
</Head>
<Body>
<P>
<Center>
<Form Action= "http:\localhost:8080servletguestbk" Method= "POST">
<Input Type="Submit" Value="VIEW THE GUEST BOOK">
</Form>
<BR>
<Form action= "http:\localhost:8080servletsubmit" method= "Post">
ENTER YOUR EMAIL ADDRESS<Br>
<Input Type="text" Name="emailid" Size="40"><BR>
COMMENTS ARE ALWAYS WELCOME<BR>
<TextArea Name="comment" Rows="8" Cols="65"></TextArea>
</Center>
<P>
<P>
<BR>
<P>
<Center>
<Input Type="Submit" Value="Submit">
</Form>
<Input Type="Reset" Value="Reset">
</Center>
<BR>
</Body>
</Html>




Post a reply
  Related Posts  to : Guest Book Application Using Servlets
 Editing guest message in PHPbb3     -  
 I want to hide add to cart button for guest users. Need help     -  
 Connecting Java Application to C++ Application from Code     -  
 material of jsp and servlets     -  
 How does JSP differ from Servlets?!!!     -  
 Servlets how to upload Files     -  
 Displaying Images using servlets     -  
 Login using jsp,Servlets and Database following MVC     -  
 help for making web pages using servlets     -  
 What is the terms applets and servlets mean?     -  

Topic Tags

Servlets/JSP, Java Projects