/* 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 javax.servlet.http.*; 

2. import javax.servlet.*; 

3. import java.io.*; 

4.  

5. public class ServUno 

6.     extends HttpServlet 

7.

8.   public void 

9.   doGet ( HttpServletRequest req, HttpServletResponse resp ) 

10.     throws ServletException, IOException 

11.   { 

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

13.  

14.     PrintWriter out = resp.getWriter(); 

15.  

16.     out.println ( "<HTML>" ) ; 

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

18.  

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

20.     out.println ( "Servlets 1" ) ; 

21.     out.println ( "</TITLE>" ) ; 

22.  

23.     out.println ( "</HEAD>" ) ; 

24.  

25.     out.println ( "<BODY>" ) ; 

26.  

27.     out.println ( "<H1>" ) ; 

28.     out.println ( "This HTML has been produced by " ) ; 

29.     out.println ( "servlet sruno (=ServUno.class)" ) ; 

30.     out.println ( "</H1>" ) ; 

31.  

32.     String str = req.getQueryString () ; 

33.     out.println ( "Querystring : " + str + "<BR>" ) ; 

34.  

35.     try

36.  

37.      String val = req.getParameter( "cnr" ) ; 

38.  

39.     int nr = Integer.parseInt( val ) ; 

40.  

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

42.  

43.     for ( int i=1; i <= nr; i++ ) 

44.     { 

45.       out.println( "<LI>" + i ); 

46.     } 

47.  

48.     out.println ( "</UL>" ) ; 

49.     } 

50.     catch ( Exception ex ) 

51.     { 

52.         out.println ( "Query string NOT OK : " + ex ) ; 

53.     } 

54.  

55.     out.println ( "</BODY>" ) ; 

56.     out.println ( "</HTML>" ) ; 

57.  

58.     out.flush (); 

59.     out.close (); 

60.   } 

61.

62. ======================================= 

63.  

64. http://localhost:8080/servtest/sruno?cnr=4 

65.  

66. == servtest.war  ============= 

67.      WEB-INF/web.xml 

68.      WEB-INF/classes/ServUno.class 

69.  

70. ========== web.xml  ========= 

71. <?xml version="1.0" encoding="ISO-8859-1"?> 

72.  

73. <!DOCTYPE web-app 

74.     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 

75.     "http://java.sun.com/dtd/web-app_2_3.dtd"

76. <web-app> 

77.  

78.     <servlet> 

79.         <servlet-name>Suno</servlet-name> 

80.         <servlet-class>ServUno</servlet-class

81.     </servlet> 

82.  

83.     <servlet-mapping> 

84.         <servlet-name>Suno</servlet-name> 

85.         <url-pattern>/sruno</url-pattern> 

86.     </servlet-mapping> 

87.      

88. </web-app> 

89. ======== in web-app_2_3.dtd :  ============ 

90. <!ELEMENT web-app (icon?, display-name?, description?, distributable?, 

91. context-param*, filter*, filter-mapping*, listener*, 

92. servlet*, servlet-mapping*, session-config?,.... 

93. ==== init =========== 

94. public void init( ServletConfig cnf ) 

95.  throws ServletException 

96.

97.     super.init(cnf); 

98. // do init for this shared instance 

99.

100.  

101.