/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. import java.sql.*; 

2. import java.io.*; 

3. import javax.servlet.*; 

4. import javax.servlet.http.*; 

5.  

6. public class DBSurf extends HttpServlet 

7.

8.   public void doPost( 

9.   HttpServletRequest req, HttpServletResponse response ) 

10.       throws ServletException, IOException 

11.   { 

12.     response.setContentType("text/html"); 

13.     response.setHeader("Pragma", "no-cache"); 

14.     response.setHeader("Cache-Control", "no-cache"); 

15.     response.setDateHeader("Expires", 0); 

16.     PrintWriter out = response.getWriter(); 

17.     out.println("<html>"); 

18.     out.println("<head><title>DBSurf servlet result page</title></head>"); 

19.     out.println("<body>"); 

20.     out.println("DBSurf Version 1.1<br>"); 

21.  

22.     String fname =   req.getParameter ( "firstname" ) ; 

23.  

24.     Connection con = null

25.     ResultSet rs = null

26.     PreparedStatement stm=null

27.     try

28.     con = DriverManager.getConnection( 

29.      "jdbc:hsqldb:hsql://localhost:1476"

30.           "sa", ""   ); 

31.     stm = con.prepareStatement 

32.     ("SELECT * FROM FMTEST WHERE FNAME=?" ); 

33.     stm.setString( 1, fname ); 

34.     rs = stm.executeQuery() ; 

35.     if ( !rs.next() ) 

36.       out.println("Nobody by that name"); 

37.     else 

38.     { 

39.       out.println("<UL>"); 

40.       boolean somemore = true

41.       while( somemore ) 

42.       { 

43.         out.print( "<LI>" + rs.getString("FNAME"

44.         + " " + rs.getString("LNAME"

45.         + " " + rs.getString("PHONE") ); 

46.         somemore = rs.next() ; 

47.       } 

48.     } 

49.     } 

50.     catch ( SQLException sex ) 

51.     { 

52.       out.println( sex ); 

53.     } 

54.     finally 

55.     { 

56.       try 

57.       { if (stm != null)   stm.close(); } 

58.         catch (Exception e) {} 

59.       try 

60.       { if (con != null)  con.close(); } 

61.         catch (Exception e){} 

62.     } 

63.     out.println("</body></html>"); 

64.     out.flush(); 

65.     out.close(); 

66.    } 

67.  

68.   public void init( ServletConfig cfg ) 

69.    throws ServletException 

70.   { 

71.     super.init(cfg); 

72.     try

73.     Class.forName("org.hsqldb.jdbcDriver"); 

74.     } 

75.     catch( ClassNotFoundException ex ) 

76.     { throw new ServletException(ex);  } 

77.   } 

78.

79. /*===  test.html in sdb.war ====== 

80. <HTML

81. <HEAD><TITLE

82. Servlets DBSurf FORM 

83. </TITLE></HEAD

84. <BODY

85.  

86. <FORM ACTION="http:dbsurf" METHOD="POST"

87.  

88. Search for

89.  

90. <INPUT TYPE="TEXT" NAME="firstname" MAXLENGTH="30"

91. <INPUT TYPE="SUBMIT" VALUE="Send"

92.  

93. </FORM

94.  

95. </BODY

96. </HTML

97. ===== sdb.war/WEB-INF/web.xml  ==== 

98. ... 

99. <web-app> 

100.  

101. <servlet> 

102.     <servlet-name>DBSurf</servlet-name> 

103.     <servlet-class>DBSurf</servlet-class

104. </servlet> 

105. <servlet-mapping> 

106.     <servlet-name>DBSurf</servlet-name> 

107.     <url-pattern>/dbsurf</url-pattern> 

108. </servlet-mapping> 

109. </web-app> 

110. === FMTEST table ==== 

111. FNAME  LNAME   PHONE 

112. ------ ------- ---------- 

113. Donald Xalatl  0752123456 

114. Donald Doobie  0123456789 

115. Jane   Joyride 0203333333 

116. */