String query = request.getParameter("query");
String restrict = request.getParameter("restrict");
boolean isValid = (query == null || query.length() < 1) ? false : true;
//set the MIME type of the response, "text/html"
response.setContentType("text/html");
//use a PrintWriter send text data to the client who has requested the servlet
java.io.PrintWriter out = response.getWriter();
//Begin assembling the HTML content
out.println("<html><head>");
out.println("<title>Google results</title></head><body>");
if (! isValid){
out.println("<h2>Sorry, the query parameter was either empty or null</h2>");
} else {
out.println("<h2>Here are your search results</h2>");
GoogleBean gb = new GoogleBean();
gb.setFilter(true);
//For the web:
gb.setLineSep("<br /><br />");
if (restrict != null && restrict.length() > 0)
gb.setRestrict(restrict);
gb.setQuery(query);
try {
out.println( gb.getSearchResults() );
} catch (Exception e){
throw new ServletException( e.getMessage() );
}
}
out.println("</body></html>");
out.close();