EmployeeSearchApp-Using-init-Param-Value.
java 12/22/2020 10:55 PM
1 //EmployeeSearchApp-Using-init-Param-Value
2 /**
3 **@Auther (Vaibhav Belkhude)
4 */
5 ==========================================
6 |
7 |---[Link]
8 | |---->[Link]
9 |--WEB-Content
10 | |----->WEB-INF
11 | |
12 | |---->lib
13 | | |--->OJDBC [Link]
14 | |
15 | |----->[Link]
16 |
17 |---->[Link]
18 |
19 ==========================================
20 Step 1:Create Package -->[Link]
21 |
22 |--->[Link]
23
24 //[Link]
25 =========================================
26 package [Link];
27
28 import [Link];
29 import [Link];
30 import [Link];
31 import [Link];
32 import [Link];
33 import [Link];
34 import [Link];
35
36 import [Link];
37 import [Link];
38 import [Link];
39 import [Link];
40 import [Link];
41 import [Link];
42
43
44 //@WebServlet("/DBServlet")
45 public class EmployeeSearchServlet extends HttpServlet {
46 private static final String GET_EMP_DETAILS="SELECT EMPNO,ENAME,JOB,SAL FROM EMP WHERE EMPNO=?";
47
48 Connection conn;
49 PreparedStatement ps;
50 public void init() {
51 [Link]("--init param--");
52 try {
53 //get Access to ServletConfig object
54 ServletConfig sc=getServletConfig();
55 //read init param value from [Link]
56 String s1=getInitParameter("driver");
57 String s2=getInitParameter("dburl");
58 String s3=getInitParameter("duser");
59 String s4=getInitParameter("dpwd");
60
61 //create jdbc conn object
62 [Link](s1);
63 conn=[Link](s2,s3,s4);
64 //create jdbc prepared statement
Page 1 of 5
[Link] 12/22/2020 10:55 PM
65 ps=[Link]("select ename,sal from emp where empno=?");
66 }//try
67 catch(Exception e) {
68 [Link]();
69 }
70 }//init()
71
72 public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
73
74 try {
75 int no=[Link]([Link]("empno"));
76 //general setting get printWriter object
77 PrintWriter pw=[Link]();
78 [Link]("text/html");
79 //write jdbc object
80 //set value to parameter query
81 [Link](1,no);
82 //Execute Query
83 ResultSet rs=[Link]();
84 //process ResultSet object
85 if([Link]())
86 {
87 [Link]("<h1 style='color:red;text-align:center'>Employee Details are:</h1>");
88 [Link]("<br><b>Emp Name is::</b>"+[Link](1));
89 [Link]("<br><b>Emp Salary is::</b>"+[Link](2));
90
91 }//if
92 else {
93 [Link]("<h1 style='color:green;text-align:center'>Employee Details are Not Found:</h1>"
94 }//else
95 [Link]("<br><br><a href='[Link]'>Home</a>");
96 //close ResultSet object
97 [Link]();
98 [Link]();
99 }//try
100 catch(Exception e) {
101 [Link]();
102 }
103
104 }
105
106 public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
107 doGet(req, res);
108 }
109 public void destroy() {
110 //close jdbc object
111
112 try {
113 if(ps!=null)
114 [Link]();
115 }
116 catch(SQLException se) {
117 [Link]();
118 }
119 try {
120 if(conn!=null)
121 [Link]();
122 }
123 catch(SQLException se) {
124 [Link]();
125 }
126 }//destroy()
127
128 }//class
Page 2 of 5
[Link] 12/22/2020 10:55 PM
129 *******************************************end java class********************************************************
130
131
132 ============================================[Vaibhav Belkhude]===================================================
133 Step 2: Create HTML file inside WEBContent folder::-->[Link]
134 ******************
135 WEBContent:----
136 |
137 |
138 |----->[Link]
139 =================================================================================================================
140 <body bgcolor="pink">
141 <h1 style="color:red ;text-align:center">EmployeeSearchApp (Servlet TO DB S/W Communication)</h1>
142 <form action="dburl" method="POST" >
143 <table boader="1" align="center" bgcolor="cyan" ">
144 <tr>
145 <td>Employee Details are:</td>
146
147 <td><input type="text" name="empno" placeholder="Search Employee ID" required></td><br><br>
148 </tr>
149 <tr>
150 <td><input type="submit" name="Get EmployeeDetails"></td>
151 <td><input type="reset" name="Reset"></td>
152
153
154 </tr>
155 </table>
156 </form>
157 </body>
158 *********************************end HTML file****************************************************************
159 ===================================================[Vaibhav Belkhude]============================================
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
Page 3 of 5
[Link] 12/22/2020 10:55 PM
193
194
195
196 =================================================[Vaibhav Belkhude]==============================================
197 Step 3: Create XML file inside WEBContent --
198 |
199 |
200 WEB-INF
201 |
202 |
203 :-->[Link]
204 ******************
205 [Link]
206 =================================================================================================================
207 <?xml version="1.0" encoding="UTF-8"?>
208 <web-app>
209 <display-name>EmployeeSearchApp-Using_initParam_value</display-name>
210 <servlet>
211 <servlet-name>DB</servlet-name>
212 <servlet-class>[Link]</servlet-class>
213 <!-- init param value -->
214 <init-param>
215 <param-name>driver</param-name>
216 <param-value>[Link]</param-value>
217 </init-param>
218
219 <init-param>
220 <param-name>dburl</param-name>
221 <param-value>jdbc:oracle:thin:@localhost:1521:ORCL</param-value>
222 </init-param>
223
224 <init-param>
225 <param-name>duser</param-name>
226 <param-value>scott</param-value>
227 </init-param>
228 <init-param>
229 <param-name>dpwd</param-name>
230 <param-value>vaibhav92</param-value>
231 </init-param>
232 <load-on-startup>1</load-on-startup>
233 </servlet>
234
235 <servlet-mapping>
236 <servlet-name>DB</servlet-name>
237 <url-pattern>/dburl</url-pattern>
238 </servlet-mapping>
239 <welcome-file-list>
240 <welcome-file>[Link]</welcome-file>
241 </welcome-file-list>
242 </web-app>
243 ******************************end EmployeeSearchApp****************************************
244 =================================================================================================================
245 ********************************************************
246 input: Employee Details Are [ 7902 ]
247
248 [Submit]
249 ********************************************************
250
251 Generated Output:
252 Employee Details are:
253 Emp Name is::ADAMS
254 Emp Salary is::1100.0
255
256 Home(hyperlink)
Page 4 of 5
[Link] 12/22/2020 10:55 PM
257 ===========================================[Vaibhav Belkhude]====================================================
Page 5 of 5