Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.LBS;
- import java.io.*;
- import java.net.*;
- import java.text.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import java.sql.*;
- import java.util.*;
- /**
- *
- * @author Administrator
- */
- public class LBSMain extends HttpServlet
- {
- /**
- * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
- * @param request servlet request
- * @param response servlet response
- */
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8");
- //PrintWriter out = response.getWriter();
- try {
- response.setContentType("text/plain");
- PrintWriter out = response.getWriter();
- BufferedReader br = request.getReader();
- String buf = br.readLine();
- char[] tempSentence=buf.toCharArray();
- int leng=tempSentence.length;
- char[][] tempWor=new char[20][10];
- int worIndex=0;
- int indx=0,wor=0;
- while(indx<leng)
- {
- while(indx<leng && tempSentence[indx]!=' ')
- {
- tempWor[wor][worIndex]=tempSentence[indx];
- // System.out.print(tempWord[word][wordIndex]);
- worIndex++;
- indx++;
- }
- indx++;
- wor++;
- worIndex=0;
- }
- wor--;
- int pos1 = 0 , pos2 = 0;
- StringBuffer que11 = new StringBuffer();
- StringBuffer loc11 = new StringBuffer();
- String tempr;
- for(int i=0;i<wor;i++)
- {
- tempr=new String(tempWor[i]);
- if ((tempr.equals("query=")))
- pos1=i;
- if(tempr.equals("loc="))
- pos2=i;
- }
- System.out.println(pos1 + pos2 + wor);
- for(int i=(pos1+1);i<pos2; i++)
- {
- tempr = new String(tempWor[i]);
- que11.append(tempr);
- que11.append(" ");
- }
- for (int i = (pos2 + 1); i <= wor; i++)
- {
- tempr = new String(tempWor[i]);
- loc11.append(tempr);
- }
- String sentence= que11.toString();
- String location= loc11.toString();
- Connection con;
- Class.forName("com.mysql.jdbc.Driver");
- String connectionUrl = "jdbc:mysql://localhost/LocationBasedServices?user=root&password=apple";
- con = DriverManager.getConnection(connectionUrl);
- String s = "Select keyword from fin;";
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery(s);
- String[] keywords=new String[10];
- String[] categories=new String[10];
- int keyCount=0;
- while(rs.next())
- {
- String keyword=rs.getString("keyword");
- boolean result=matchKey(keyword,sentence);
- if(result==true)
- {
- keywords[keyCount]=keyword;
- keyCount++;
- }
- }
- //////////////////////////////////////////////////////
- int i=0;
- String dynKey="";
- while(i<keyCount)
- {
- dynKey=dynKey+" keyword='"+keywords[i]+"'";
- i++;
- if(i<keyCount)
- {
- dynKey=dynKey+" or";
- }
- }
- int j=0;
- node h=new node();
- h.create("hospitals","emergency");
- node c=new node();
- c.create("college","education");
- node f=new node();
- f.create("fun","entertainment");
- node a = new node();
- a.create("atm", "financial");
- node e = new node();
- e.create("food", "eat-outs");
- Hashtable hs=new Hashtable();
- hs.put("h",h);
- hs.put("c",c);
- hs.put("f",f);
- hs.put("a", a);
- hs.put("e", e);
- while(j<keyCount)
- {
- String temp=keywords[j].substring(0,1);
- node n = (node)hs.get(temp);
- n.display(keywords[j]);
- //out.println(n.output);
- categories[j]=n.output;
- j++;
- }
- /* s = "Select category from fin where"+dynKey+";";
- stmt = con.createStatement();
- rs = stmt.executeQuery(s);
- String[] categories=new String[10];
- int catcount=0;
- while(rs.next())
- {
- categories[catcount]=rs.getString("category");
- catcount++;
- }
- */
- int k=0;
- String dynCat="";
- while(k<keyCount)
- {
- dynCat=dynCat+" category='"+categories[k]+"'";
- k++;
- if(k<keyCount)
- {
- dynCat=dynCat+" or";
- }
- }
- s = "Select * from fin where ("+dynCat+") and location='"+location+"';";
- stmt = con.createStatement();
- rs = stmt.executeQuery(s);
- out.println("Location based search \n Search Results \n");
- //t.println("Category\tName\tAddress</td></tr>");
- while(rs.next())
- {
- out.println("category = "+rs.getString("category")+"\n");
- out.println("Name = "+rs.getString("servicename")+"\n");
- out.println("Address"+rs.getString("serviceaddress")+"\n");
- }
- //out.println("</table></body>");
- }
- catch (SQLException e) {
- System.out.println("SQL Exception: " + e.toString());
- } catch (ClassNotFoundException cE) {
- System.out.println("Class Not Found Exception: " + cE.toString());
- }
- }
- boolean matchKey(String category, String sentence)
- {
- char[] tempsentence = sentence.toCharArray();
- int len = tempsentence.length;
- char[][] tempWord = new char[5][10];
- int wordIndex = 0;
- int index = 0, word = 0;
- while (index < len)
- {
- while (index < len && tempsentence[index] != ' ')
- {
- tempWord[word][wordIndex] = tempsentence[index];
- // System.out.print(tempWord[word][wordIndex]);
- wordIndex++;
- index++;
- }
- index++;
- word++;
- wordIndex = 0;
- }
- boolean flag = true;
- char[] key = category.toCharArray();
- for (int j = 0; j < word; j++)
- {
- flag = true;
- for (int i = 0; i < key.length; i++)
- {
- if (tempWord[j][i] != key[i])
- {
- flag = false;
- }
- }
- if (flag == true) break;
- }
- if (flag == true)
- return true;
- else
- return false;
- }
- // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
- /**
- * Handles the HTTP <code>GET</code> method.
- * @param request servlet request
- * @param response servlet response
- */
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException
- {
- processRequest(request, response);
- }
- /**
- * Handles the HTTP <code>POST</code> method.
- * @param request servlet request
- * @param response servlet response
- */
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException
- {
- processRequest(request, response);
- }
- /**
- * Returns a short description of the servlet.
- */
- public String getServletInfo()
- {
- return "Short description";
- }
- // </editor-fold>
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement