-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathExtraServlet.java
More file actions
255 lines (223 loc) · 8.82 KB
/
ExtraServlet.java
File metadata and controls
255 lines (223 loc) · 8.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package com.endor;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ExtraServlet
*/
@WebServlet("/ExtraServlet")
public class ExtraServlet extends HttpServlet {
//private static final long serialVersionUID = 1L;
static String connectionUrl = "";
static String dbUser = "";
static String dbPassword = "";
static String dbType = "";
static String DB_TYPE_ORACLE = "Oracle";
/**
* @see HttpServlet#HttpServlet()
*/
@Override
public void init() throws ServletException {
super.init();
connectionUrl =System.getProperty("endor_connection_url");
if (connectionUrl == null) {
throw new ServletException("Database connection URL must be provided via endor_connection_url system property");
}
dbUser =System.getProperty("endor_db_user");
if (dbUser == null) {
throw new ServletException("Database user must be provided via endor_db_user system property");
}
dbPassword =System.getProperty("endor_db_password");
if (dbPassword == null || dbPassword.isEmpty()) {
throw new ServletException("Database password must be provided via endor_db_password system property");
}
dbType =System.getProperty("endor_db_type", DB_TYPE_ORACLE);
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = null;
try {
out = response.getWriter();
} catch (Exception e) {
e.printStackTrace();
}
HtmlUtil.printHtmlHeader(response);
HtmlUtil.startBody(response);
HtmlUtil.printMenu(response);
HtmlUtil.openTable(response);
HtmlUtil.openRow(response);
HtmlUtil.openCol(response);
HtmlUtil.printCurrentTitle("SQL", response);
String form = "<form action=\"booklist\">" +
"This URL is testing multi leg features <br>" +
"--------------------------------------<br><br>"+ "</form>";
out.println(form);
HtmlUtil.closeCol(response);
String retVal = "Failed!";
//PreparedStatement execution with input parameter
int len = request.getContentLength();
if (len > 0) {
byte[] input = new byte[len];
//System.out.println("length of input" + len);
ServletInputStream sin = request.getInputStream();
int c, count = 0 ;
while ((c = sin.read(input, count, input.length-count)) != -1) {
count +=c;
}
sin.close();
String inString = new String(input);
int index = inString.indexOf("&");
String lastvalue = inString.substring(0,index);
String restvalue = inString.substring(index+1);
index = restvalue.indexOf("&");
String passvalue = restvalue.substring(0,index);
String multileg_value = restvalue.substring(index+1);
index = lastvalue.indexOf("=");
String last = lastvalue.substring(index+1);
index = passvalue.indexOf("=");
String pass = passvalue.substring(index+1);
index = multileg_value.indexOf("=");
String multileg = multileg_value.substring(index+1);
System.out.println("\nlast=" +last+ "\npass=" +pass+ "\nmultileg=" +multileg);
if(multileg.equalsIgnoreCase("prepared_statement") && executeSQLHelper(last, pass)) {
retVal = "Succeeded";
}
else if(multileg.equalsIgnoreCase("stored_procedure") && getCustomersStoredProc(last, pass)) {
retVal = "Succeeded";
}
HtmlUtil.openCol(response);
out.println("<h2> SQL execution for Input Parameter " + retVal + "</h2>");
HtmlUtil.closeCol(response);
}
//PreparedStatement with hard-coded value
/*
retVal = "Succeeded";
executeSQLHelper("prakash'--", "psmo");
*/
if (retVal.equalsIgnoreCase("Succeeded")){
retVal = HttpURLConnectionExample.sendGET();
}
/*
HtmlUtil.openCol(response);
out.println("<h2> SQL execution for internal values " + retVal + "</h2>");
HtmlUtil.closeCol(response);
*/
HtmlUtil.closeRow(response);
HtmlUtil.closeTable(response);
out.println("</body>");
out.println("</html>");
}
private Connection connect() {
Connection conn = null;
try {
// Create database connection
conn = DriverManager.getConnection(connectionUrl, dbUser, dbPassword);
System.out.println("DB Connection established");
} catch (Exception e) {
System.err.println("ERROR: failed to connect DB");
e.printStackTrace();
return null;
}
return conn;
}
public boolean executeSQLHelper(String name, String pass) {
boolean retVal = false;
Connection conn = connect();
if (conn == null)
return false;
try {
StringBuffer sbuf = new StringBuffer();
String query = new String();
query = "select FIRST, LAST from CUSTOMERS WHERE LAST=\'" + name + "\' AND PASSWORD= \'" + pass + "\'";
System.out.println("Multileg PreparedStatementQUERY:" + query);
PreparedStatement stmt = conn.prepareStatement(query);
ResultSet rs = stmt.executeQuery();
// Loop through the data and print all artist names
while (rs.next()) {
sbuf.append("Customer Name: " + rs.getString("FIRST") + " " + rs.getString("LAST"));
System.out.println("Customer Name: " + rs.getString("FIRST") + " " + rs.getString("LAST"));
sbuf.append("<br>");
retVal = sbuf.toString().length() > 2;
}
// Clean up
stmt.close();
rs.close();
} catch (SQLException e) {
System.err.println(e.getMessage());
} finally {
try {
// Close connection
conn.close();
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
return retVal;
}
public boolean getCustomersStoredProc(String name, String pass) {
Connection conn = connect();
if (conn == null)
return false;
Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
int output = 0;
boolean hasResults = false;
try {
// close the statement as its not a callable statement
stmt.close();
String query;
CallableStatement c = null;
query = "{call verifyuser(?,?,?)}";
c = conn.prepareCall(query);
c.setString(1, name);
c.setString(2, pass);
c.registerOutParameter(3, Types.INTEGER);
System.out.println("Multihub DB stored Proc being called");
System.out.println(query);
c.execute();
output = c.getInt(3);
System.out.println("Customer Count: " + output);
//c.executeQuery();
// Loop through the data and print all artist names
// Clean up
c.close();
} catch (Exception e) {
System.out.println("Exception !");
System.err.println(e.getMessage());
} finally {
try {
// Close connection
conn.close();
} catch (SQLException e) {
System.err.println(e.getMessage());
System.out.println("Exception 2");
}
}
return output > 0;
}
}