在servlet的doGet方法中创建配置对象给我异常

时间:2016-09-25 15:34:32

标签: java hibernate jsp servlets

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    String name=req.getParameter("name");
    String email=req.getParameter("email");
    int marks=Integer.parseInt(req.getParameter("marks"));

    Student st=new Student(0,name,email,marks);

    System.out.println("Inside get method before configuration object generation");
    Configuration cfg=new Configuration();
    System.out.println("After configuration initialization");
    cfg.configure("Resourses/hibernate.cfg.xml");
    System.out.println("after giving hibernate config file");
    sf=cfg.buildSessionFactory();

}

我在输出中收到此错误

在init方法中 在打电话给超级 在配置对象生成之前的内部get方法

Sep 25, 2016 8:56:41 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Controller.StudentRegistrationServlet] in context with path [/StudentRegistration] threw exception
java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "createClassLoader")
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:457)
    at java.security.AccessController.checkPermission(AccessController.java:884)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)

1 个答案:

答案 0 :(得分:0)

默认的Java Runtime Security设置不允许运行时权限'createClassLoader',这是ArcSDE Java API所必需的。

更新C:\Program Files\Java\jdk1.7.0_79\jre\lib\security\java.policy 文件  将以下行添加到默认权限“grant ... {...}”块中的 java.policy文件中:

Code:
// default permissions granted to all domains
grant {
     permission java.lang.RuntimePermission "createClassLoader";
     permission java.util.PropertyPermission "NLS_LANG", "read";
     //....rest of code as it is. 
     }

注意 servlet容器可能有自己的安全策略,该策略会覆盖JRE范围的策略。例如,Tomcat使用TOMCAT_HOME/conf/catalina.policy中的那个,而不是您正在修改的文件。

对于 Tomcat 服务器,在WEB APPLICATION PERMISSIONS旁边的C:\ Program Files \ Apache Software Foundation \ Tomcat 7.0 \ conf \ catalina.policy文件中添加权限,如下所示:

// ========== WEB APPLICATION PERMISSIONS =====================================
grant {
     permission java.lang.RuntimePermission "createClassLoader";
     permission java.util.PropertyPermission "NLS_LANG", "read";
     //....rest of code as it is. 
     }