Total members 11895 |It is currently Sun Dec 22, 2024 2:57 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka






* 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>




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  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







Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com