我写的一个程序代码如下:JDBCServlet.javpackage com.jspdev.ch7;import java.sql.*;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;public class JDBCServlet extends HttpServlet
{

private String driver;
private String url;


public  void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException
{

response.setContentType("text/htm;charset=gb2312");
PrintWriter out=response.getWriter();
try
{

Connection con=getConnection();
Statement stmt=con.createStatement();
ResultSet rst=stmt.executeQuery("select * from mydb2");


while(rst.next())
{

out.println(rst.getString("name"));
out.println("<br>");

}

rst.close();
stmt.close();
con.close();


}

catch(SQLException e)
{

e.printStackTrace();

}

}


public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException
{

doGet(request,response);

}

public void init() throws ServletException
{

driver=getInitParameter("DRIVER");
url=getInitParameter("URL");
}
private Connection getConnection()
{

Connection con=null;
try
{

Class.forName(driver);
con=DriverManager.getConnection(url);

}

catch(Exception e)
{

e.printStackTrace();

}

return con;

}




}编译已经通过。
web.xml代码如下:
<?xml version="1.0" encoding="ISO-8859-1"?> 
<!DOCTYPE web-app 
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
"http://java.sun.com/dtd/web-app_2_3.dtd"> 
<web-app> 
<display-name>My Web Application</display-name> 
<description> 
A application for test. 
</description> <servlet>
<servlet-name>JDBCServlet</servlet-name>
<display-name>JDBCServlet</display-name> 
<description>A test Servlet</description> 
<servlet-class>com.jspdev.ch7.JDBCServlet</servlet-class>
<init-param>
<param-name>DRIVER</param-name>
<param-value>org.gjt.mm.mysql.Driver</param-value>
</init-param>
<init-param>
<param-name>URL</param-name>
<param-value>jdbc:mysql://localhost:3306/myjsp?user=root&password=123</param-value>
</init-param>

</servlet>
<servlet-mapping>
<servlet-name>JDBCServlet</servlet-name>
<url-pattern>/init_servlet</url-pattern>
</servlet-mapping>
</web-app> 运行后提示
type Status reportmessage /myjsp/init_servletdescription The requested resource (/myjsp/init_servlet) is not available.
我的文件都在/myjsp下。请问各位大侠是什么原因????

解决方案 »

  1.   

    用 http://.../init_servlet
      

  2.   

    我的文件都在/myjsp下
    ====================
    跟包名根本不符,当然访问不到,将JDBCServlet放入  你的应用名/classes/com/jspdev/ch7下
    然后通过    应用名/init_servlet
      

  3.   

    我用其它的程序试过,换了个web.xml程序通过,不知道上面的程序为什么不能通过????
    郁闷!!!
      

  4.   

    <url-pattern>/init_servlet</url-pattern>
    这样设置的话
    如果你的网站是合同http://www.xxx.com
    那么这个servlet应该是这样访问http://www.xxx.com/init_servlet,
    从根目录开始,而不是myjsp
      

  5.   

    你的web.xml中有这么一句有问题:
    <param-value>jdbc:mysql://localhost:3306/myjsp?user=root&password=123</param-value>“&”符号在xml中是有特殊意义的,表示实体,需要对这个符号进行转义:
    用“&amp;”来代替“&”试一下推荐用好的IDE来开发,可以发现或避免很多问题。我用WSAD发现这个问题的。;)
      

  6.   

    是你初始化错误public void init(ServletConfig config) throws ServletException {
            super.init(config);
            config.getInitParameter("emailuser")
      
        }没有super.init(config);是不行的查看相关治疗
      

  7.   

    按说改正了&号后应该可以的<url-pattern>/init_servlet</url-pattern>
    这句话的/是指相对于Web应用的根,与你的目录myjsp没有关系
    访问地址应该是http://xxhost:xxport/xxapp/init_servletxxapp是你web应用的文档根
      

  8.   

    我在WSAD已经可以正确地调用你给的东东