/Users/lyon/j4p/src/net/server/servlets/PhoneInsert.java

1    package net.server.servlets; 
2     
3    import javax.servlet.ServletException; 
4    import javax.servlet.http.HttpServlet; 
5    import javax.servlet.http.HttpServletRequest; 
6    import javax.servlet.http.HttpServletResponse; 
7    import java.io.IOException; 
8    import java.io.PrintWriter; 
9     
10   public class PhoneInsert extends HttpServlet { 
11    
12       public static String getSql( 
13               String firstname, 
14               String lastname, 
15               String address1, 
16               String address2, 
17               String phone1, 
18               String phone2, 
19               String phone3) { 
20    
21           return "insert into phonelist" 
22                   + "(firstname,lastname," 
23                   + "address1,address2,phone1,phone2,phone3) values " 
24                   + "(" + quote(firstname) 
25                   + quote(lastname) 
26                   + quote(address1) 
27                   + quote(address2) 
28                   + quote(phone1) 
29                   + quote(phone2) 
30                   + nocommaQuote(phone3) + ")"; 
31       } 
32    
33       public static String nocommaQuote(String s) { 
34           return "'" + s + "'"; 
35       } 
36    
37       public static String quote(String s) { 
38           return "'" + s + "',"; 
39       } 
40    
41       public static String getForm() { 
42           return 
43                   "<HTML>" 
44                   + "<HEAD>" 
45                   + "<TITLE>Phone Book</TITLE></HEAD>" 
46                   + "<BODY>" 
47                   + "<CENTER>" 
48                   + "<H1>Telephone Book</H1>" 
49                   + "<FORM ACTION=HelloWorldExample METHOD=GET><TABLE>" 
50                   + "<TR>" 
51                   + "  <TD>Last Name</TD>" 
52                   + "  <TD><INPUT TYPE=TEXT SIZE=20 NAME=lastname></TD></TR><TR>" 
53                   + "  <TD>First Name</TD>" 
54                   + "  <TD><INPUT TYPE=TEXT SIZE=20 NAME=firstname></TD></TR><TR>" 
55                   + "  <TD>address1</TD>" 
56                   + "  <TD><INPUT TYPE=TEXT SIZE=20 NAME=address1></TD></TR><TR>" 
57                   + "  <TD>address2</TD>" 
58                   + "  <TD><INPUT TYPE=TEXT SIZE=20 NAME=address2></TD></TR><TR>" 
59                   + "  <TD>Phone1</TD>" 
60                   + "  <TD><INPUT TYPE=TEXT SIZE=20 NAME=phone1></TD></TR>" 
61                   + "  <TD>Phone2</TD>" 
62                   + "  <TD><INPUT TYPE=TEXT SIZE=20 NAME=phone2></TD></TR>" 
63                   + "  <TD>Phone3</TD>" 
64                   + "  <TD><INPUT TYPE=TEXT SIZE=20 NAME=phone3></TD></TR>" 
65                   + "</TABLE>" 
66                   + "<INPUT TYPE=Submit NAME=Add VALUE=Add New Entry>" 
67                   + "<INPUT TYPE=Reset VALUE=Reset>" 
68                   + "</FORM><BR>" 
69                   + "</BODY></HTML>"; 
70       } 
71    
72       public void doGet(HttpServletRequest request, 
73                         HttpServletResponse response) 
74               throws IOException, ServletException { 
75           response.setContentType("text/html"); 
76           PrintWriter out = response.getWriter(); 
77           String fn = request.getParameter("firstname"); 
78           String ln = request.getParameter("lastname"); 
79           String a1 = request.getParameter("address1"); 
80           String a2 = request.getParameter("address2"); 
81           String p1 = request.getParameter("phone1"); 
82           String p2 = request.getParameter("phone2"); 
83           String p3 = request.getParameter("phone3"); 
84           out.println("<html>"); 
85           out.println("<body bgcolor=\"white\">"); 
86           out.println("<body>"); 
87           if (fn != null) { 
88               String sql = getSql(fn, ln, a1, a2, p1, p2, p3); 
89               out.println("sql inserted=" + sql); 
90               Execute.statement(sql); 
91           } else 
92               out.println(getForm()); 
93    
94           out.println("</body>"); 
95           out.println("</html>"); 
96       } 
97   } 
98    
99    
100   
101