jsp 常常遇到的问题

1.数据库连接文件配置问题

1. resources/jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/kdb1
username=root
password=root

2. dbConn.java

public class dbConn {
    public static Connection getConn() {
        Connection con = null;
        try {
            DataSource source1;
            Properties p = new Properties();
            String path = Thread.currentThread().getContextClassLoader().getResource  ("jdbc.properties").getPath();
            p.load(new FileInputStream(path));
            String driver = p.getProperty("driver");
            System.out.println(driver);
            String url = p.getProperty("url");
            String username = p.getProperty("username");
            String passwd = p.getProperty("password");
            Class.forName(driver); // 加载驱动程序

            con = DriverManager.getConnection(url, username, passwd);
            return con;
        } catch (ClassNotFoundException | FileNotFoundException e) {
            System.out.println("加载驱动程序错误" + e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;

    }
}

2. web.xml 的配置文件

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>


  <servlet>
    <servlet-name>LoginServlet</servlet-name>   <!-- 3 标识名,可随便起对应的  -->
    <servlet-class>cn.ccut.servlet.LoServlet</servlet-class>   <!-- 4 对应本项目  Servlet   -->
  </servlet>

  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>    <!-- 2 标识名 可随便起  -->
    <url-pattern>/userlogin</url-pattern>  <!-- 1 访问的url ,http://127.0.0.1/userlogin       -->
  </servlet-mapping>

3. Servlet 跳转的路径问题

     response.sendRedirect("getusers");//http://localhost:8080/szu/getusers
       //     response.sendRedirect("/getusers");       http://localhost:8080/getusers


posted @ 2020-07-26 20:18  Tony小哥  阅读(289)  评论(0编辑  收藏  举报