此例子是网上书店,包含4个文件search.html,web.xml,CreateDBServlet.java,ListServlet.java
结构如下:
ch08\search.html
    \WEB-INF\web.xml
            \classes\CreateDBServlet.class
                    \ListServlet.class
能够正常访问search.html,说明xml部署应该没有问题,但是无法加载两个类的内容。具体代码如下,请高手们给指点指点!多谢了!
1、search.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>网上书店</title>
<script language="javascript">
<!--
function fsubmit()
{
if(searchForm.rcond[0].checked)
{
searchForm.action="list?cond=all"
}
else if(searchForm.rcond[1].checked)
{
searchForm.action="list?cond=precision"
}
else
{
searchForm.action="list?cond=keyword"
}
}
function hideall()
{
if(searchForm.rcond[0].checked)
{
pre.style.display="none";
key.style.display="none";
}
}
function showpre()
{
if(searchForm.rcond[1].checked)
{
pre.style.display="";
key.style.display="none";
}
else
{
pre.style.display="none";
}
}
function showkey()
{
if(searchForm.rcond[2].checked)
{
key.style.display="";
pre.style.display="none";
}
else
{
key.style.display="none";
}
}
//-->
</script>
</head><body>
<form name="searchForm" action="" method="post" onClick="fsubmit()">
<input type="radio" name="rcond" onClick="hideall()">查看所有图书<p></p>
<input type="radio" name="rcond" onClick="showpre()">精确搜索<p></p>
<table id="pre" style="display:none">
<tr>
<td>书名:</td>
<td><input type="text" name="title"></td>
</tr>
<tr>
<td>作者:</td>
<td><input type="text" name="author"></td>
</tr>
<tr>
<td>出版社:</td>
<td><input type="text" name="bookconcern"></td>
</tr>
</table><p></p>
<input type="radio" name="rcond" onClick="showkey()">关键字搜索<p></p>
<table id=key style="display:none">
<tr>
<td>请输入关键字:</td>
<td><input type="text" name="keyword"></td>
</tr>
</table><p></p>
<input type="reset" value="重新输入">
<input type="submit" value="搜索">
</form>
</body>
</html>2、web.xml
<?xml version="1.0" encoding="gb2312"?><web-app xmlns=""
xmlns:xsi=""
xsi:SchemaLocation=""
version="2.4">

<context-param>
<param-name>driverClass</param-name>
<param-value>com.microsoft.jdbc.sqlserver.SQLServerDriver</param-value>
</context-param>
<context-param>
<param-name>URL</param-name>
<param-value>jdbc:microsoft:sqlserver://localhost:1433;database=pubs</param-value>
</context-param>
<context-param>
<param-name>user</param-name>
<param-value>sa</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>qazwsx</param-value>
</context-param>
<servlet>
<servlet-name>CreateDBServlet</servlet-name> <servlet-class>CreateDBServlet</servlet-class>
<!-- <init-param>
<param-name>driverClass</param-name>
<param-value>com.microsoft.jdbc.sqlserver.SQLServerDriver</param-value>
</init-param>
<init-param>
<param-name>URL</param-name>
<param-value>jdbc:microsoft:sqlserver://localhost:1433;database=pubs</param-value>
</init-param>
<init-param>
<param-name>user</param-name>
<param-value>sa</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>qazwsx</param-value>
</init-param>
-->
</servlet>
<servlet>
<servlet-name>ListServlet</servlet-name> <servlet-class>ListServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CreateDBServlet</servlet-name>
<url-pattern>/createDB</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ListServlet</servlet-name>
<url-pattern>/list</url-pattern>
</servlet-mapping>
</web-app>

解决方案 »

  1.   

    3、CreateDBServlet.java
    import javax.servlet.*;
    import java.io.*;
    import javax.servlet.http.*;
    import java.sql.*;public class CreateDBServlet extends HttpServlet
    {
    private String URL;
    private String user;
    private String password;

    public void init() throws ServletException
    {
    ServletContext sc=getServletContext();
    String driverClass=getInitParameter("driverClass");
    URL=getInitParameter("URL");
    user=getInitParameter("user");
    password=getInitParameter("password");
    try
    {
    Class.forName(driverClass);
    }
    catch(ClassNotFoundException ce)
    {
    throw new UnavailableException("加载数据库驱动失败!");
    }
    }

    public void doGet(HttpServletRequest req,HttpServletResponse resp)
    throws ServletException,IOException
    {
    Connection conn=null;
    Statement stmt=null;
    try
    {
    conn=DriverManager.getConnection(URL,user,password);
    stmt=conn.createStatement();
    stmt.executeUpdate("Create database bookstore");
    stmt.executeUpdate("use bookstore");
    stmt.executeUpdate("Create table bookinfo(id INT not null primarykey,title VARCHAR(50) not null,author VARCHAR(50) not null,bookconcern VARCHAR(100) not null,publish_date DATE not null,price FLOAT(4,2) not null,amount smallint,re VARCHAR(200))engine InnoDB");
    stmt.addBatch("insert into bookinfo values(1,'Java 从入门到精通','张三','张三出版社','2004-6-1',34.00,35,null)");
    stmt.addBatch("insert into bookinfo values(2,'JSP','li si','lisi publishment','2004-10-1',56.00,20,null)");
    stmt.addBatch("insert into bookinfo values(3,'J2EE','ww','ww publishment','2005-3-1',78.00,10,null)");
    stmt.executeBatch();

    PrintWriter out=resp.getWriter();
    out.println("success!");
    out.close();
    }
    catch(SQLException se)
    {
    se.printStackTrace();
    }
    finally
    {
    if(stmt!=null)
    {
    try
    {
    stmt.close();
    }
    catch(SQLException se)
    {
    se.printStackTrace();
    }
    stmt=null;
    }
    if(conn!=null)
    {
    try
    {
    conn.close();
    }
    catch(SQLException se)
    {
    se.printStackTrace();
    }
    conn=null;
    }
    }
    }
    }4、ListServlet
    import javax.servlet.*;
    import java.io.*;
    import javax.servlet.http.*;
    import java.sql.*;public class ListServlet extends HttpServlet
    {
    private String URL;
    private String user;
    private String password;

    public void init() throws ServletException
    {
    ServletContext sc=getServletContext();
    String driverClass=sc.getInitParameter("driverClass");
    URL=sc.getInitParameter("URL");
    user=sc.getInitParameter("user");
    password=sc.getInitParameter("password");
    try
    {
    Class.forName(driverClass);
    }
    catch(ClassNotFoundException ce)
    {
    throw new UnavailableException("加载数据库驱动失败!");
    }
    }

    public void doGet(HttpServletRequest req,HttpServletResponse resp)
    throws ServletException,IOException
    {
    Connection conn=null;
    Statement stmt=null;
    ResultSet rs=null;
    req.setCharacterEncoding("gb2312");
    String condition=req.getParameter("cond");
    if(null==condition || condition.equals(""))
    {
    resp.sendRedirect("search.html");
    return;
    }
    resp.setContentType("text/html;charset=gb2312");
    PrintWriter out=resp.getWriter();
    try
    {
    conn=DriverManager.getConnection(URL,user,password);
    stmt=conn.createStatement();
    if(condition.equals("all"))
    {
    rs=stmt.executeQuery("select * from bookinfo");
    printBookInfo(out,rs);
    out.close();
    }
    else if(condition.equals("precision"))
    {
    String title=req.getParameter("title");
    String author=req.getParameter("author");
    String bookconcern=req.getParameter("bookconcern");

    if((null==title || title.equals("")) && 
    (null==author || author.equals("")) && 
    (null==bookconcern || bookconcern.equals("")))
    {
    resp.sendRedirect("search.html");
    return;
    }

    StringBuffer sb=new StringBuffer("select * from bookinfo where");
    boolean bFlag=false;

    if(!title.equals(""))
    {
    sb.append("title="+"'"+title+"'");
    bFlag=true;
    }
    if(!author.equals(""))
    {
    if(bFlag)
    sb.append("and author="+"'"+author+"'");
    else
    {
    sb.append("author="+"'"+author+"'");
    bFlag=true;
    }
    }
    if(!bookconcern.equals(""))
    {
    if(bFlag)
    sb.append("and bookconcern="+"'"+bookconcern+"'");
    else
    sb.append("bookconcern="+"'"+bookconcern+"'");
    }

    rs=stmt.executeQuery(sb.toString());
    printBookInfo(out,rs);
    out.close();
    }
    else if(condition.equals("keyword"))
    {
    String keyword=req.getParameter("keyword");
    if(null==keyword || keyword.equals(""))
    {
    resp.sendRedirect("search.html");
    return;
    }
    String strSQL="select * from bookinfo where title like '%"+keyword+"%'";

    rs=stmt.executeQuery(strSQL);
    printBookInfo(out,rs);
    out.close();
    }
    else
    {
    resp.sendRedirect("search.html");
    return;
    }
    }
    catch(SQLException se)
    {
    se.printStackTrace();
    }
    finally
    {
    if(rs!=null)
    {
    try
    {
    rs.close();
    }
    catch(SQLException se)
    {
    se.printStackTrace();
    }
    rs=null;
    }
    if(stmt!=null)
    {
    try
    {
    stmt.close();
    }
    catch(SQLException se)
    {
    se.printStackTrace();
    }
    stmt=null;
    }
    if(conn!=null)
    {
    try
    {
    conn.close();
    }
    catch(SQLException se)
    {
    se.printStackTrace();
    }
    conn=null;
    }
    }
    }

    public void doPost(HttpServletRequest req,HttpServletResponse resp)
    throws ServletException,IOException
    {
    doGet(req,resp);
    }

    private void printBookInfo(PrintWriter out,ResultSet rs)
    throws SQLException
    {
    out.println("<html><head>");
    out.println("<title>图书信息</title>");
    out.println("</head><body>");
    out.println("<table border=1><caption>图书信息</caption>");
    out.println("<tr><th>书名</th><th>作者</th><th>出版社</th><th>价格</th><th>发行日期</th>");
    while(rs.next())
    {
    out.println("<tr>");
    out.println("<td>"+rs.getString("title")+"</td>");
    out.println("<td>"+rs.getString("author")+"</td>");
    out.println("<td>"+rs.getString("bookconcern")+"</td>");
    out.println("<td>"+rs.getString("price")+"</td>");
    out.println("<td>"+rs.getString("publish_date")+"</td>");
    out.println("</tr>");
    }
    out.println("</table></body></html>");
    }
    }
      

  2.   

    请高手运行一下,非常感谢!
    java新手,望高手能指点!
      

  3.   

    直接运行下servlet,看看是否有问题
      

  4.   

    我搞不懂的是,它那个CreateDBServlet里的数据库怎么被search.html调用的。因为书上说直接运行search.html就可以找到添加的图书信息了。没有提直接运行servlet的事情,我好像试过直接运行http://localhost:8080/ch08/CreateDBServlet,好像还是报‘类不存在’的错误
      

  5.   

    <servlet-name>xxx</servlet-name>
    <servlet-class>xxx.class</servlet-class>
    我觉得是你的servlet配置信息不完整
      

  6.   

    我之前做的例子里好像在<servlet-class></servlet-class>之间不需要加‘.class’。加了可以解决问题了,我只有明天试试先了
      

  7.   

    我说的不是.class,只是说那里需要写servlet类的完整路径