0 Replies Latest reply on Nov 24, 2010 11:37 AM by wayman

    jndi naming, @javax.annotation.Resource(mappedName versus name)

    wayman Newbie

      public class jg extends HttpServlet {
          @Resource(name = "java:/comp/env/jdbc/sisds")
          DataSource sistestds;
          protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                  throws ServletException, IOException {
              PrintWriter out = response.getWriter();
              try {
                  response.setContentType("text/html;charset=UTF-8");
                  // obtaining connection
                  Connection conn = sistestds.getConnection();
                  // preparing and executing statement
                  PreparedStatement st = conn.prepareStatement("SELECT ip FROM jail");
                  st.execute();
                  // printing resultset
                  ResultSet rs = st.getResultSet();
                  while (rs.next()) {
                      out.println(rs.getString("ip"));
                  }
              } catch (SQLException ex) {
                  Logger.getLogger(jg.class.getName()).log(Level.SEVERE, null, ex);
              } finally {
                  out.close();
              }
          }
      Dear JBoss community members,

       

      I am really complete newbie to JBoss technologies. The very first thing a have tried was simple servlet with JDBC DataSource Resource injection... Something like...

       

      public class jg extends HttpServlet {

       

          @Resource(name = "java:/comp/env/jdbc/sisds")

          DataSource sistestds;

       

          protected void processRequest(HttpServletRequest request, HttpServletResponse response)

                  throws ServletException, IOException {

              PrintWriter out = response.getWriter();

              try {

                  response.setContentType("text/html;charset=UTF-8");

       

                  // obtaining connection

                  Connection conn = sistestds.getConnection();

       

                  // preparing and executing statement

                  PreparedStatement st = conn.prepareStatement("SELECT ip FROM jail");

                  st.execute();

       

                  // printing resultset

                  ResultSet rs = st.getResultSet();

                  while (rs.next()) {

                      out.println(rs.getString("ip"));

                  }

              } catch (SQLException ex) {

                  Logger.getLogger(jg.class.getName()).log(Level.SEVERE, null, ex);

              } finally {

                  out.close();

              }

          }

       

      ... (the method gets called from appropriate get/post... etc. calls)...

      I have simple table containing list of some virtual servers (jails) and want to list their IP addresses. This snippet of code would work in GlassFish, ... but JBoss reports that mappedName should be used... in the @Resource injection...
      In fact... it functions with the mappedName... but I want my application to be as portable as possible and I'd like to avoid using platform specific things like mappedName....
      Should mention that I am using JBoss AS 5.0.1 GA on Linux...
      Any help is welcome...
      Martin Vejmelka