注册 登录
编程论坛 J2EE论坛

大神帮帮忙看看,一个连接数据库比读取数据的 servlet,运行出现 405 错误,求指点!!!

我爱敲代码 发布于 2014-05-30 00:32, 2344 次点击
package com.sun.java.lesson10;
public class Contants {
   public static final String driver="com.mysql.jdbc.Driver";
   public static final String url="jdbc:mysql://localhost:3306/javaweb";
   public static final String user="root";
   public static final  String password="504979454";
}


package com.sun.java.lesson10;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import *;
import com.sun.java.lesson10.Contants;
public class JdbcServlet extends HttpServlet {
    public void init() throws ServletException {
        super.init();
        try{
            Class.forName(Contants.driver).newInstance();
        }catch(Exception e){
            e.printStackTrace();
    }
    }
    protected void Server(HttpServletResponse response,HttpServletRequest request) throws ServletException, IOException, InstantiationException, IllegalAccessException {
        Connection conn=null;
           Statement  st=null;
            ResultSet re=null;
            response.setContentType("text/html;charset=gb2312");
        PrintWriter out=response.getWriter();
         out.println("<html>");
            out.println("<head><title>网页标题</title></head>");
            out.println("<body>");
            out.println("网页内容");
            out.println("</body></html>");
        try{
            conn=DriverManager.getConnection(Contants.url,Contants.user,Contants.password);
        }catch(SQLException e){
        out.println("连接数据库失败");
        }
        try{
            st=conn.createStatement();
            String str="select * from user";
            re=st.executeQuery(str);
            while(re.next()){
                out.print("Id:"+re.getInt(1));
                 out.print("  用户名:"+re.getString(2));
                 out.print("  密码:"+re.getString(3));
                 out.println("<br>");
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        try{
            while(re!=null){
            re.close();
            re=null;
            }
            while(st!=null){
            st.close();
            st=null;
            }
            while(conn!=null){
            conn.close();
            conn=null;
            }
            }catch(SQLException e){
            out.println("数据库关闭失败");
            }
    }
    public void destroy() {
        
    }
}


运行报错如下:

HTTP Status 405 - HTTP method GET is not supported by this URL

--------------------------------------------------------------------------------

type Status report

message HTTP method GET is not supported by this URL

description The specified HTTP method is not allowed for the requested resource.


--------------------------------------------------------------------------------

Apache Tomcat/6.0.39
1 回复
#2
shaw802014-05-31 14:34
405是service方法名称错误吧,你的方法名写错了
1