简体   繁体   English

如何访问servlet上下文侦听器中的属性?

[英]How to access attribute inside servlet context listener?

I want to access a attribute that I set up in the context listener from a jsp file. 我想从jsp文件访问在上下文侦听器中设置的属性。 I have set up the servlet listener, then I added the listener into web.xml. 我已经设置了servlet侦听器,然后将侦听器添加到web.xml中。 My servlet listener will be used to connect to the database. 我的servlet侦听器将用于连接数据库。

Here is my context Listener (necessary class are imported): 这是我的上下文侦听器(导入了必需的类):

 @WebServlet("/MyServletContextListener")
  public class MyServletContextListener implements ServletContextListener{

    public void contextDestroyed(ServletContextEvent event) {
        System.out.println("ServletContextListener destroyed");
    }

        //Run this before web application is started
    public void contextInitialized(ServletContextEvent event) {
        System.out.println("ServletContextListener started");   
        String DriverName = "com.mysql.jdbc.Driver";
        String conURL = "jdbc:mysql://localhost/";
        ServletContext context = event.getServletContext();
        String dbName = context.getInitParameter("dbName");
        String user = context.getInitParameter("user");
        String pass = context.getInitParameter("pw");
        Connection conn = null;
        try{
            Class.forName(DriverName);
            conn = DriverManager.getConnection(conURL+dbName, user,pass);
        }catch(ClassNotFoundException ex){

        }catch(SQLException sqle){

        }
        context.setAttribute("conn", conn);
    }
}

From my jsp file I want to access "conn". 从我的jsp文件中,我要访问“ conn”。

This is my xml: 这是我的xml:

 <web-app>
    <listener>
<listener-class>
    Listener.MyServletContextListener
</listener-class>
</listener>

this is my jsp: 这是我的jsp:

    <%
    ServletContext context = getServletContext();
    context.getAttribute("conn");
    System.out.println(context.getAttribute("conn"));
    boolean loginpass = false;
    String login = request.getParameter("login");
    String pw = request.getParameter("password");
    try {
        loginpass = checklogin(login, pw,
                (java.sql.Connection) context.getAttribute("conn"));
    } catch (java.sql.SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
%>

the line System.out.println(context.getAttribute("conn")) prints out : null System.out.println(context.getAttribute("conn"))打印出来: null

I shouldn't be null, it should be connected to the database. 我不应该为null,它应该连接到数据库。 The database password and user name are correct. 数据库密码和用户名正确。 Database password and user name are in web.xml under context-param . 数据库密码和用户名位于web.xml中的context-param How do I get the conn attribute from MyServletContextListener? 如何从MyServletContextListener获取conn属性?

Your connection code may have connection error print the stack trace. 您的连接代码可能有连接错误,请打印堆栈跟踪。 so that you will come to know actual problem. 这样您就会知道实际的问题。

catch(ClassNotFoundException ex){
     System.out.println(ex) 
}
catch(SQLException sqle){
 System.out.println(sqle)
}

Add the mysql connector in the deployment assembly. 在部署程序集中添加mysql连接器。 Project File ->properites -> Deployment Assembly -> add 项目文件->属性->部署程序集->添加

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM