📄 searchservlet.java
字号:
// Fig. 16.29: GetDescriptionServlet.java
// Retrieves descripion of an item from database
package cartXML;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.text.*;
import java.sql.*;
import org.w3c.dom.*;
public class SearchServlet extends HttpServlet {
private Database database;
public void init( ServletConfig config )
throws ServletException
{
super.init( config );
database = new Database( "jdbc:odbc:catalog", "anonymous",
"guest" );
database.connect();
}
public void service( HttpServletRequest request,
HttpServletResponse response )
throws ServletException, IOException
{
try {
ServletContext sc = getServletConfig().
getServletContext();
String query;
String search = request.getParameter( "search" );
String searchString = request.getParameter( "searchString" );
if ( search.equals( "title" ) )
query = "SELECT title FROM products WHERE title LIKE '%" + searchString + "%'";
else
query = "SELECT title FROM products WHERE pubdate LIKE '%" + searchString + "%'";
ResultSet rs = database.get( query );
XMLCreator xmlCreator = new XMLCreator();
Node searchNode = xmlCreator.initialize( "search" );
int results = 0;
while( rs.next() ) {
Node productNode = xmlCreator.addChild( searchNode,
"book" );
xmlCreator.addTextNode( xmlCreator.addChild(
productNode, "title" ),
rs.getString( "title" ) );
results++;
System.out.println("in loop");
}
if ( results == 0 )
xmlCreator.addAttribute( searchNode, "total", "0" );
else
xmlCreator.addAttribute( searchNode, "total", String.valueOf( results ) );
Processor processor = new Processor();
response.setContentType( "text/vnd.wap.wml" );
PrintWriter output = response.getWriter();
processor.process( xmlCreator.getDocument(), "C:/jakarta-tomcat/webapps/chapter16/searchResults.xsl", output );
}
catch( SQLException sqlex ){}
}
public void destroy()
{ database.shutDown(); }
}
/*
**************************************************************************
* (C) Copyright 2001 by Deitel & Associates, Inc. and Prentice Hall. *
* All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -