0

I am new to eclipse and servlet. I am trying to connect database(mysql). It works when I run the code in a .java application, but it doesn't work when I put the code in a servlet. I have looked throught the questions similar to mine, but the answers cannot solve my problem. Please give me any idea how to make my code works. thank you. my code is as follow:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  response.getWriter().append("Served at: ").append(request.getContextPath());
 }

 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

   Connection conn = null;

   try{
   Class.forName("com.mysql.jdbc.Driver");

   conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1","root","jeff800205");

    }
   catch(SQLException e){
              System.out.println("@@@Error occured at barDAO->select_all()");
              e.printStackTrace();
          } catch (ClassNotFoundException e) {
           e.printStackTrace();
           System.out.println("%%%Error occured at barDAO->select_all()");
             
    //} catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
 }

the problem is as follow:

SEVERE: Servlet.service() for servlet [helloBar2] in context with path [/echarts2] threw exception [null] with root cause
java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "setContextClassLoader")
 at java.security.AccessControlContext.checkPermission(Unknown Source)
 at java.security.AccessController.checkPermission(Unknown Source)
 at java.lang.SecurityManager.checkPermission(Unknown Source)
 at java.lang.Thread.setContextClassLoader(Unknown Source)
 at com.mysql.jdbc.AbandonedConnectionCleanupThread$1.newThread(AbandonedConnectionCleanupThread.java:50)
 at java.util.concurrent.ThreadPoolExecutor$Worker.<init>(Unknown Source)
 at java.util.concurrent.ThreadPoolExecutor.addWorker(Unknown Source)
 at java.util.concurrent.ThreadPoolExecutor.execute(Unknown Source)
 at java.util.concurrent.Executors$DelegatedExecutorService.execute(Unknown Source)
 at com.mysql.jdbc.AbandonedConnectionCleanupThread.<clinit>(AbandonedConnectionCleanupThread.java:54)
 at java.lang.Class.forName0(Native Method)

I am using tomcat 8.5,mysql5.7,have tried both mysql-connector-java 5.1.43-bin.jar and mysql-connector-java 5.0.8.jar. I have put mysql-connector.jar into the lib folder under WEB_INF, and I have put mysql-connector.jar into the lib in Tomcat too. But the problem still exists.

Here is my log file in tomcat:

127.0.0.1 - - [28/Aug/2017:09:12:01 +0800] "GET / HTTP/1.1" 200 11452
0:0:0:0:0:0:0:1 - - [28/Aug/2017:09:12:10 +0800] "GET /echarts/test.jsp HTTP/1.1" 200 604
0:0:0:0:0:0:0:1 - - [28/Aug/2017:09:12:16 +0800] "POST /echarts/bar.do HTTP/1.1" 500 2834
127.0.0.1 - - [28/Aug/2017:12:35:06 +0800] "GET / HTTP/1.1" 200 11452
0:0:0:0:0:0:0:1 - - [28/Aug/2017:12:35:16 +0800] "GET /echarts/test.jsp HTTP/1.1" 200 604
0:0:0:0:0:0:0:1 - - [28/Aug/2017:12:35:25 +0800] "POST /echarts/bar.do HTTP/1.1" 500 2834

Here is my port imformation: enter image description here

jeff
  • 9
  • 2

1 Answers1

0

Check java security manager options.

You can try to run tomcat without Security Manager to find out the problem. See here how to disable Security Manager

Mike Adamenko
  • 2,944
  • 1
  • 15
  • 28
  • I have added this into java.policy, is that correct?seems doesn't help. "permission java.net.SocketPermission “127.0.0.1:3306〃,”connect,resolve”; – jeff Aug 28 '17 at 10:42
  • 1
    The permission you need is `java.lang.RuntimePermission "setContextClassLoader"`, not `java.net.SocketPermission`. – Christopher Schultz Aug 28 '17 at 13:18