Advertisement
Guest User

Untitled

a guest
May 29th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.66 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5.  
  6. package com.LBS;
  7.  
  8. import java.io.*;
  9. import java.net.*;
  10. import java.text.*;
  11. import javax.servlet.*;
  12. import javax.servlet.http.*;
  13. import java.sql.*;
  14. import java.util.*;
  15.  
  16.  
  17. /**
  18. *
  19. * @author Administrator
  20. */
  21. public class LBSMain extends HttpServlet
  22. {
  23.  
  24. /**
  25. * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
  26. * @param request servlet request
  27. * @param response servlet response
  28. */
  29. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  30. throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8");
  31. //PrintWriter out = response.getWriter();
  32. try {
  33. response.setContentType("text/plain");
  34. PrintWriter out = response.getWriter();
  35. BufferedReader br = request.getReader();
  36. String buf = br.readLine();
  37.  
  38. char[] tempSentence=buf.toCharArray();
  39. int leng=tempSentence.length;
  40.  
  41. char[][] tempWor=new char[20][10];
  42. int worIndex=0;
  43. int indx=0,wor=0;
  44.  
  45. while(indx<leng)
  46. {
  47.  
  48. while(indx<leng && tempSentence[indx]!=' ')
  49. {
  50. tempWor[wor][worIndex]=tempSentence[indx];
  51. // System.out.print(tempWord[word][wordIndex]);
  52. worIndex++;
  53. indx++;
  54.  
  55. }
  56. indx++;
  57. wor++;
  58. worIndex=0;
  59. }
  60. wor--;
  61. int pos1 = 0 , pos2 = 0;
  62. StringBuffer que11 = new StringBuffer();
  63. StringBuffer loc11 = new StringBuffer();
  64.  
  65. String tempr;
  66. for(int i=0;i<wor;i++)
  67. {
  68. tempr=new String(tempWor[i]);
  69. if ((tempr.equals("query=")))
  70. pos1=i;
  71. if(tempr.equals("loc="))
  72. pos2=i;
  73. }
  74.  
  75. System.out.println(pos1 + pos2 + wor);
  76.  
  77.  
  78. for(int i=(pos1+1);i<pos2; i++)
  79. {
  80. tempr = new String(tempWor[i]);
  81. que11.append(tempr);
  82. que11.append(" ");
  83. }
  84. for (int i = (pos2 + 1); i <= wor; i++)
  85. {
  86. tempr = new String(tempWor[i]);
  87. loc11.append(tempr);
  88. }
  89.  
  90.  
  91. String sentence= que11.toString();
  92. String location= loc11.toString();
  93.  
  94.  
  95. Connection con;
  96.  
  97. Class.forName("com.mysql.jdbc.Driver");
  98. String connectionUrl = "jdbc:mysql://localhost/LocationBasedServices?user=root&password=apple";
  99. con = DriverManager.getConnection(connectionUrl);
  100.  
  101. String s = "Select keyword from fin;";
  102. Statement stmt = con.createStatement();
  103. ResultSet rs = stmt.executeQuery(s);
  104. String[] keywords=new String[10];
  105. String[] categories=new String[10];
  106. int keyCount=0;
  107. while(rs.next())
  108. {
  109. String keyword=rs.getString("keyword");
  110. boolean result=matchKey(keyword,sentence);
  111. if(result==true)
  112. {
  113. keywords[keyCount]=keyword;
  114. keyCount++;
  115. }
  116. }
  117.  
  118. //////////////////////////////////////////////////////
  119. int i=0;
  120. String dynKey="";
  121. while(i<keyCount)
  122. {
  123. dynKey=dynKey+" keyword='"+keywords[i]+"'";
  124. i++;
  125. if(i<keyCount)
  126. {
  127. dynKey=dynKey+" or";
  128. }
  129. }
  130. int j=0;
  131. node h=new node();
  132. h.create("hospitals","emergency");
  133. node c=new node();
  134. c.create("college","education");
  135. node f=new node();
  136. f.create("fun","entertainment");
  137. node a = new node();
  138. a.create("atm", "financial");
  139. node e = new node();
  140. e.create("food", "eat-outs");
  141. Hashtable hs=new Hashtable();
  142. hs.put("h",h);
  143. hs.put("c",c);
  144. hs.put("f",f);
  145. hs.put("a", a);
  146. hs.put("e", e);
  147. while(j<keyCount)
  148. {
  149. String temp=keywords[j].substring(0,1);
  150. node n = (node)hs.get(temp);
  151. n.display(keywords[j]);
  152. //out.println(n.output);
  153. categories[j]=n.output;
  154. j++;
  155. }
  156. /* s = "Select category from fin where"+dynKey+";";
  157. stmt = con.createStatement();
  158. rs = stmt.executeQuery(s);
  159.  
  160. String[] categories=new String[10];
  161. int catcount=0;
  162. while(rs.next())
  163. {
  164. categories[catcount]=rs.getString("category");
  165. catcount++;
  166. }
  167. */
  168.  
  169. int k=0;
  170. String dynCat="";
  171. while(k<keyCount)
  172. {
  173. dynCat=dynCat+" category='"+categories[k]+"'";
  174. k++;
  175. if(k<keyCount)
  176. {
  177. dynCat=dynCat+" or";
  178. }
  179. }
  180.  
  181.  
  182. s = "Select * from fin where ("+dynCat+") and location='"+location+"';";
  183. stmt = con.createStatement();
  184. rs = stmt.executeQuery(s);
  185.  
  186. out.println("Location based search \n Search Results \n");
  187. //t.println("Category\tName\tAddress</td></tr>");
  188. while(rs.next())
  189. {
  190. out.println("category = "+rs.getString("category")+"\n");
  191. out.println("Name = "+rs.getString("servicename")+"\n");
  192. out.println("Address"+rs.getString("serviceaddress")+"\n");
  193. }
  194. //out.println("</table></body>");
  195.  
  196. }
  197.  
  198.  
  199.  
  200. catch (SQLException e) {
  201. System.out.println("SQL Exception: " + e.toString());
  202. } catch (ClassNotFoundException cE) {
  203. System.out.println("Class Not Found Exception: " + cE.toString());
  204. }
  205. }
  206.  
  207. boolean matchKey(String category, String sentence)
  208. {
  209.  
  210. char[] tempsentence = sentence.toCharArray();
  211. int len = tempsentence.length;
  212.  
  213. char[][] tempWord = new char[5][10];
  214. int wordIndex = 0;
  215. int index = 0, word = 0;
  216.  
  217. while (index < len)
  218. {
  219.  
  220. while (index < len && tempsentence[index] != ' ')
  221. {
  222. tempWord[word][wordIndex] = tempsentence[index];
  223. // System.out.print(tempWord[word][wordIndex]);
  224. wordIndex++;
  225. index++;
  226.  
  227. }
  228. index++;
  229. word++;
  230. wordIndex = 0;
  231. }
  232.  
  233. boolean flag = true;
  234. char[] key = category.toCharArray();
  235. for (int j = 0; j < word; j++)
  236. {
  237. flag = true;
  238. for (int i = 0; i < key.length; i++)
  239. {
  240. if (tempWord[j][i] != key[i])
  241. {
  242. flag = false;
  243. }
  244. }
  245. if (flag == true) break;
  246. }
  247.  
  248. if (flag == true)
  249. return true;
  250. else
  251. return false;
  252. }
  253.  
  254. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  255. /**
  256. * Handles the HTTP <code>GET</code> method.
  257. * @param request servlet request
  258. * @param response servlet response
  259. */
  260. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  261. throws ServletException, IOException
  262. {
  263. processRequest(request, response);
  264. }
  265.  
  266. /**
  267. * Handles the HTTP <code>POST</code> method.
  268. * @param request servlet request
  269. * @param response servlet response
  270. */
  271. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  272. throws ServletException, IOException
  273. {
  274. processRequest(request, response);
  275. }
  276.  
  277. /**
  278. * Returns a short description of the servlet.
  279. */
  280. public String getServletInfo()
  281. {
  282. return "Short description";
  283. }
  284. // </editor-fold>
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement