📄 advancedsearchservlet.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package com.horizongroup.mcompanion.web.server;import com.horizongroup.mcompanion.web.db.AdvancedSearchManager;import java.io.*;import java.net.*;import javax.servlet.*;import javax.servlet.http.*;/** * * @author Administrator */public class AdvancedSearchServlet extends HttpServlet { /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * @param request servlet request * @param response servlet response */ HttpSession savepreference ; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String str = request.getParameter("input"); // out.println("Message Frm Client :"+str); String[] params = str.split("#"); AdvancedSearchManager AdSeMa; AdSeMa = new AdvancedSearchManager(); String advaceHospitals =""; // copied from test for(int i= 0 ; i< params.length ; i++) { System.out.println(params[i]); AdSeMa = new AdvancedSearchManager(); String substr = params[i]; String paramSub[] = params[i].split(":"); System.out.println(paramSub[0]); if(paramSub[0].equals("Hospital")) { String field[] ={"name","specialities"}; String query1=search(field, paramSub); System.out.println(query1); advaceHospitals += AdSeMa.SearchHospital(query1); // System.out.println("RESULTS"); } else if(paramSub[0].equals("Restaurant")) { String field[] ={"name","menu"}; String query1=search(field, paramSub); System.out.println(query1); advaceHospitals += AdSeMa.SearchRestaurant(query1); } else if(paramSub[0].equals("Institution")) { String field[] ={"name","courses"}; String query1=search(field, paramSub); System.out.println(query1); advaceHospitals += AdSeMa.SearchInstitution(query1); } else if(paramSub[0].equals("Religious_spots")) { String field[] ={"mythology","history"}; String query1=search(field, paramSub); System.out.println(query1); advaceHospitals += AdSeMa.SearchReligiousSpot(query1); } else if(paramSub[0].equals("Location")) { String field[] ={"name","speciality"}; String query1=search(field, paramSub); System.out.println(query1); advaceHospitals += AdSeMa.SearchLocation(query1); } } out.println(advaceHospitals); } public String search(String fields[],String paramSub[]) { String result=""; String sqlquery="select * from "+paramSub[0];//+" where "; int count=0; for(int j=0;j<paramSub.length;j++) { if(j==1 && (!paramSub[j].trim().equals(""))) { sqlquery +=" where "+fields[0]+" like'%"+ paramSub[j]+"%'"; count++; } else if(j==2 &&(!paramSub[j].trim().equals(""))) { if(count>0) sqlquery +="and " ; sqlquery +=fields[1]+" like'%"+ paramSub[j]+"%'"; } } System.out.println(result); return sqlquery; }/* for(int i= 0 ; i< params.length ; i++) { String substr = params[i]; String paramSub[] = params[i].split(":"); if(paramSub[0].equals("hospital")) { String field[] ={"name","specialities"}; String query1=search(field, paramSub); advaceHospitals += AdSeMa.SearchHospital(query1); //out.println("RESULTS"); } else if(paramSub[0].equals("Restaurant")) { String field[] ={"name","menu"}; String query1=search(field, paramSub); advaceHospitals += AdSeMa.SearchRestaurant((query1)); //out.println("RESULTS"); //t.println(advaceHospitals); } else if(paramSub[0].equals("Institution")) { // AdSeMa = new AdvancedSearchManager(); String field[] ={"name","courses"}; String query1=search(field, paramSub); //String query1 = "select * from Restaurant" ; System.out.println(query1); advaceHospitals += AdSeMa.SearchInstitution(query1); //out.println("RESULTS"); //t.println(advaceHospitals); } } out.println(advaceHospitals); } public String search(String fields[],String paramSub[]) { String result=""; String sqlquery="select * from "+paramSub[0];//+" where "; int count=0; for(int j=0;j<paramSub.length;j++) { if(j==1 && (!paramSub[j].trim().equals(""))) { sqlquery +=" where "+ fields[0]+" like'%"+ paramSub[j]+"%'"; count++; } else if(j==2 &&(!paramSub[j].trim().equals(""))) { if(count>0) sqlquery +="and " ; sqlquery +=fields[1]+" like'%"+ paramSub[j]+"%'"; } } System.out.println(sqlquery); return sqlquery; }*/ // <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>}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -