Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Servlets
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
Eclipse Collections Categorically: Level up your programming game
this week in the
Open Source Projects
forum!
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Ron McLeod
Jeanne Boyarsky
Sheriffs:
Paul Clapham
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Servlets
Endless loop
Ron Ingram
Ranch Hand
Posts: 73
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
All, I'm caught in an endless loop. Can anyone give it a glance over and help me find it or how to fix it?
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package SearchServlets; import Lucene.SearchEngine; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.lucene.document.Document; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopDocs; /** * */ public class Search extends HttpServlet { RequestDispatcher dispatcher = null; String returns = ""; /** * Processes requests for both HTTP * <code>GET</code> and * <code>POST</code> methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //dispatcher = request.getRequestDispatcher("Web/Includes/Search_results.jsp"); //dispatcher.forward(request, response); response.setContentType("text/html;charset=UTF-8"); //response.setContentType("application/json"); PrintWriter out = response.getWriter(); try { out.println(returns); //Use correctly formatted output JSON string for troubleshooting /* out.println("{ \"cols\": [{\"id\": \"task\", \"label\": \"Task\", \"type\": \"string\"}," + "{\"id\": \"hours\", \"label\": \"Hours per Day\", \"type\": \"number\"}]," + "\"rows\": [{\"c\":[{\"v\": \"Work\"}, {\"v\": 11}]}," + "{\"c\":[{\"v\": \"Eat\"}, {\"v\": 2}]}," + "{\"c\":[{\"v\": \"Commute\"}, {\"v\": 2}]}," + "{\"c\":[{\"v\": \"Watch TV\"}, {\"v\":2}]}," + "{\"c\":[{\"v\": \"Sleep\"}, {\"v\":7, \"f\":null}]}]}"); * */ } finally { out.close(); } } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //<editor-fold defaultstate="collapsed" desc="Global Initializers"> //Global Initializers TopDocs hits = null; ScoreDoc[] search_list; Document document = null; //Create a StringBuilder StringBuilder sb_rows = new StringBuilder(); HttpSession session = request.getSession(); //</editor-fold> String search_term = request.getParameter("txt_search"); System.out.println("Search for: " + search_term); //String returns = ""; /* START SEARCH */ SearchEngine search_engine = new SearchEngine(); int scoreDoc_size = 0; try { hits = search_engine.performSearch(search_term); System.out.println("Total docs found: " + hits.totalHits); } catch (IOException ioex) { System.out.println(ioex); } catch (ParseException pex) { System.out.println(pex); } search_list = hits.scoreDocs; scoreDoc_size = search_list.length; System.out.println("Score Doc Size: " + scoreDoc_size); /*-----------------------------------------------------------------------------*/ /* COMPLETED WITH SEARCH */ /* * ******START TO GATHER FIELD NAMES FROM SEARCH DYNAMICALLY ************ */ //Build Arrays // Extract the text between the two title elements ArrayList<String> field_list = new ArrayList(); String[] document_splited = null; String temp = ""; int key = search_list[1].doc; //gets doc id from search_list document = search_engine.docReturn(key); //fields = document.getFields(); //split string at every occurance of < String field = document.toString(); field = field.substring(34); //Split and create a new string at "<" document_splited = (field.split("<")); System.out.println("Document Splited = " + document_splited.length); for (int j = 0; j < document_splited.length; j++) { //iterate 70 times String splited = document_splited[j]; //Truncate each splitted string above everything after : (Can't figure out why adding -1 throws index out of bounds exception. Will need to revisit temp = splited.substring(0, splited.indexOf(":")); //Chop the ":" off temp = temp.substring(0, temp.length()); field_list.add(temp); } /** * ********************** END ADDED FIELD NAMES * ******************************* */ //</editor-fold> //Build column Strings sb_rows.append("\"cols\": ["); //Iterate field_list and build columns for(int i = 0; i < field_list.size(); i++){ String column_value = field_list.get(i); String column_type = ""; if(column_value.contains("Amount") || column_value.contains("TotalAmount") || column_value.contains("Date") || column_value.contains("Num") || column_value.contains("Year") || column_value.contains("DCN") || column_value.contains("Id") || column_value.contains("Number") || column_value.contains("Zip") || column_value.contains("EIN")) { column_type = "number"; } else { column_type = "string"; } if (i == field_list.size() - 1) { sb_rows.append("{\"id\": \"" + column_value + "\", \"label\": \"" + column_value + "\", \"type\": \"" + column_type + "\"}],"); } else { sb_rows.append("{\"id\": \"" + column_value + "\", \"label\": \"" + column_value + "\", \"type\": \"" + column_type + "\"},"); } } //END BUILDING COLUMNS //Example String /*("{ \"cols\": [{\"id\": \"task\", \"label\": \"Task\", \"type\": \"string\"}," + "{\"id\": \"hours\", \"label\": \"Hours per Day\", \"type\": \"number\"}]," + "\"rows\": [{\"c\":[{\"v\": \"Work\"}, {\"v\": 11}]}," + "{\"c\":[{\"v\": \"Eat\"}, {\"v\": 2}]}," + "{\"c\":[{\"v\": \"Commute\"}, {\"v\": 2}]}," + "{\"c\":[{\"v\": \"Watch TV\"}, {\"v\":2}]}," + "{\"c\":[{\"v\": \"Sleep\"}, {\"v\":7, \"f\":null}]}]}"); * */ /* *****************************BUILD ROWS ******************************************/ sb_rows.append("\"rows\":["); /* During initial tests, and since Lucene matches keywords that may exists multiple times within one document, * which will not be acceptable. I will need to add the data returned from TopDocs into a collection * and compare, building a new array to create the return string of 'cleaned' results */ try{ //Iterate all documents (set to 3000 max) for (int i = 0; i < search_list.length; i++) { // Get document key from scoreDoc key = search_list[i].doc; // Call docReturn method from SearchEngine class. This // returns the document based on the key from scoreDoc Document d = search_engine.docReturn(key); /*Strings used to build data in rows. Must not be blank, but can accept value of 'null'. * Use if/else to check for empty space and replace with string value of 'null'. * If null is found, set the string value to "null" so that the google chart will not error out. * The last value in a string array for the google chart api does not allow for empty spaces. * * For the if statements, do not use if/ else because it needs to check all * */ ArrayList row_values = new ArrayList(); sb_rows.append("{\"c\":["); //Get row values for(int j = 0; j < field_list.size(); j++){ String rValue = d.get(field_list.get(j)); if (j == field_list.size() - 1) { sb_rows.append("{\"v\":\"" + rValue + "\"}]} "); } else { sb_rows.append("{\"v\":\"" + rValue + "\"}]}, "); } System.out.println(sb_rows); } } } catch(Exception ex){ System.out.println(ex); } /*********************** END BUILD ROWS ************************************/ sb_rows.append("]"); //combine columns and rows (stringbuilder sb) in order to build literal string used to create google datatable() returns = "{ " + sb_rows + " }"; //returns = "{ " +columns + sb_rows+ ""; //Set request attribute to the string expression needed to build the google chart DataTable() //request.setAttribute("lucene_search_results", returns); //System.out.println("Returns : "+returns); response.setCharacterEncoding("UTF-8"); processRequest(request, response); }}
Tim Cooke
Marshal
Posts: 6036
429
I like...
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Some System.out.println() statements inside your for loops will quickly tell you which one you're stuck in.
Tim Driven Development
| Test until the fear goes away
So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Clipboard not copying right data
Writing a content in excel in incremental way
Pasting image in JTable cells
How to change mouse cursor during drag and drop
Need Help in Modification MultiRowHeaderExample
More...