java ee - @Resource not injected in CDI managed bean, but works fine in web servlet -


i'm trying read out lot of first names database using jdbc, , working fine in servlet named helloservlet. able respond request bunch of names.

@webservlet(name = "helloservlet", value = "/hello") public class helloservlet extends httpservlet {     @resource(lookup = "java:global/employeesds")     datasource ds;      @override     protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {         list<string> list = new arraylist<>();          try(connection connection = ds.getconnection()) {             statement statement = connection.createstatement();             resultset result = statement.executequery("select first_name employees");              while(result.next()) {                 list.add(result.getstring(1));             }         } catch (sqlexception e) {             e.printstacktrace();         }          printwriter pw = resp.getwriter();         for(string name : list) {             pw.write(name + " " );         }     } } 

when try same code in cdi managed bean, ds remains null, causing nullpointerexception trouble:

@named("datafetchbean") @requestscoped public class datafetchbean {     @resource(lookup = "java:global/employeesds")     datasource ds;     list<string> questions;      public list<string> getquestions() {         try(connection connection = ds.getconnection()) {             statement statement = connection.createstatement();             resultset result = statement.executequery("select first_name employees");              while(result.next()) {                 questions.add(result.getstring(1));             }         } catch (sqlexception e) {             e.printstacktrace();         }          return questions;     } } 

in case that's relevant, web.xml file:

<?xml version="1.0" encoding="utf-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"          xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"          xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"          version="3.1">     <data-source>         <name>java:global/employeesds</name>         <class-name>com.mysql.jdbc.jdbc2.optional.mysqldatasource</class-name>         <server-name>localhost</server-name>         <port-number>3306</port-number>         <database-name>employees</database-name>         <user>root</user>         <password />     </data-source>      <servlet>         <servlet-name>faces servlet</servlet-name>         <servlet-class>javax.faces.webapp.facesservlet</servlet-class>         <load-on-startup>1</load-on-startup>     </servlet>     <servlet-mapping>         <servlet-name>faces servlet</servlet-name>         <url-pattern>*.xhtml</url-pattern>     </servlet-mapping> </web-app> 

this index.xhtml document in i'm trying use datafetchbean cdi managed bean:

<?xml version="1.0" encoding="utf-8"?> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"         "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"       xmlns:h="http://xmlns.jcp.org/jsf/html"       xmlns:ui="http://xmlns.jcp.org/jsf/facelets"       xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:c="http://java.sun.com/jsp/jstl/core"> <h:head>     <title>hello, jdbc!</title> </h:head> <h:body>     <table>         <tr>             <td>first name</td>         </tr>         <c:foreach items="#{datafetchbean.questions}" var="question">             <tr>                 <td>                     #{question}                 </td>             </tr>         </c:foreach>     </table> </h:body> </html> 

i'm using wildfly 10.0.10. in advance!

the solution create non-xa data source of wildfly's console. included addition of mysql connector driver web container, too. after that, can inject data source application.


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -