0% found this document useful (0 votes)
44 views

EmployeeSearchApp Using Init Param Values

This document describes the creation of an Employee search application using servlets and JDBC. It involves 3 main steps: 1. Creating an EmployeeSearchServlet class that initializes database connection parameters from a web.xml file and uses them to connect to an Oracle database and execute a query. 2. Creating a Search.html form to accept employee ID input and submit to the servlet. 3. Configuring a web.xml file to define servlet initialization parameters for the database driver, URL, username, and password, and map the servlet to a URL pattern. When an ID is submitted, the servlet retrieves the matching employee details from the database and displays them.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

EmployeeSearchApp Using Init Param Values

This document describes the creation of an Employee search application using servlets and JDBC. It involves 3 main steps: 1. Creating an EmployeeSearchServlet class that initializes database connection parameters from a web.xml file and uses them to connect to an Oracle database and execute a query. 2. Creating a Search.html form to accept employee ID input and submit to the servlet. 3. Configuring a web.xml file to define servlet initialization parameters for the database driver, URL, username, and password, and map the servlet to a URL pattern. When an ID is submitted, the servlet retrieves the matching employee details from the database and displays them.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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 |---com.nt.servlet
8 | |---->EmployeeSearchServlet.java
9 |--WEB-Content
10 | |----->WEB-INF
11 | |
12 | |---->lib
13 | | |--->OJDBC 6.jar
14 | |
15 | |----->web.xml
16 |
17 |---->Search.html
18 |
19 ==========================================
20 Step 1:Create Package -->com.nt.servlet
21 |
22 |--->EmployeeSearchServlet.java
23
24 //EmployeeSearchServlet.java
25 =========================================
26 package com.nt.servlet;
27
28 import java.io.IOException;
29 import java.io.PrintWriter;
30 import java.sql.Connection;
31 import java.sql.DriverManager;
32 import java.sql.PreparedStatement;
33 import java.sql.ResultSet;
34 import java.sql.SQLException;
35
36 import javax.servlet.ServletConfig;
37 import javax.servlet.ServletException;
38 import javax.servlet.annotation.WebServlet;
39 import javax.servlet.http.HttpServlet;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
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 System.out.println("--init param--");
52 try {
53 //get Access to ServletConfig object
54 ServletConfig sc=getServletConfig();
55 //read init param value from web.xml
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 Class.forName(s1);
63 conn=DriverManager.getConnection(s2,s3,s4);
64 //create jdbc prepared statement

Page 1 of 5
EmployeeSearchApp-Using-init-Param-Value.java 12/22/2020 10:55 PM

65 ps=conn.prepareStatement("select ename,sal from emp where empno=?");


66 }//try
67 catch(Exception e) {
68 e.printStackTrace();
69 }
70 }//init()
71
72 public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
73
74 try {
75 int no=Integer.parseInt(req.getParameter("empno"));
76 //general setting get printWriter object
77 PrintWriter pw=res.getWriter();
78 res.setContentType("text/html");
79 //write jdbc object
80 //set value to parameter query
81 ps.setInt(1,no);
82 //Execute Query
83 ResultSet rs=ps.executeQuery();
84 //process ResultSet object
85 if(rs.next())
86 {
87 pw.println("<h1 style='color:red;text-align:center'>Employee Details are:</h1>");
88 pw.println("<br><b>Emp Name is::</b>"+rs.getString(1));
89 pw.println("<br><b>Emp Salary is::</b>"+rs.getFloat(2));
90
91 }//if
92 else {
93 pw.println("<h1 style='color:green;text-align:center'>Employee Details are Not Found:</h1>"
94 }//else
95 pw.println("<br><br><a href='Search.html'>Home</a>");
96 //close ResultSet object
97 rs.close();
98 pw.close();
99 }//try
100 catch(Exception e) {
101 e.printStackTrace();
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 ps.close();
115 }
116 catch(SQLException se) {
117 se.printStackTrace();
118 }
119 try {
120 if(conn!=null)
121 conn.close();
122 }
123 catch(SQLException se) {
124 se.printStackTrace();
125 }
126 }//destroy()
127
128 }//class

Page 2 of 5
EmployeeSearchApp-Using-init-Param-Value.java 12/22/2020 10:55 PM

129 *******************************************end java class********************************************************


130
131
132 ============================================[Vaibhav Belkhude]===================================================
133 Step 2: Create HTML file inside WEBContent folder::-->Search.html
134 ******************
135 WEBContent:----
136 |
137 |
138 |----->Search.html
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
EmployeeSearchApp-Using-init-Param-Value.java 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 :-->web.xml
204 ******************
205 web.xml
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>com.nt.servlet.DBServlet</servlet-class>
213 <!-- init param value -->
214 <init-param>
215 <param-name>driver</param-name>
216 <param-value>oracle.jdbc.driver.OracleDriver</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>Search.html</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
EmployeeSearchApp-Using-init-Param-Value.java 12/22/2020 10:55 PM

257 ===========================================[Vaibhav Belkhude]====================================================

Page 5 of 5

You might also like