Pregunta

People, please, help me!!! I am driving really crazy. My code have been working before today, and now it's broken...

Here is my servlet:

@Stateless
class TempSession {
    void createFile( String fileName ) {
        try {
            FileWriter writer = new FileWriter( fileName );
            writer.close();
        }
        catch ( IOException e ) {
            e.printStackTrace();
        }
    }
}

@WebServlet( name = "CreteFileServlet", urlPatterns = "/createCSV" )
public class CreateCSVConfigurationServlet extends javax.servlet.http.HttpServlet {
    @Inject
    TempSession session;

    protected void doPost( javax.servlet.http.HttpServletRequest request,
            javax.servlet.http.HttpServletResponse response ) throws javax.servlet.ServletException, IOException {}

    protected void doGet( javax.servlet.http.HttpServletRequest request,
            javax.servlet.http.HttpServletResponse response ) throws javax.servlet.ServletException, IOException {
        session.createFile( "d:/session.txt" );
    }
}

Here is web.xml:

<servlet>
    <servlet-name>CreateFileServlet</servlet-name>
    <servlet-class>com.bs.amg.test.servlets.CreateCSVConfigurationServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>CreateFileServlet</servlet-name>
    <url-pattern>/createCSV</url-pattern>
</servlet-mapping>

JSP snippet:

<form action="createCSV" method="get">
    <input type="submit" value="Create configuration"/>
</form>

Anв Jboss 7.0.1 log server snippet:

14:10:52,471 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "ear-1.0.ear"
14:11:10,336 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/web].[CreteFileServlet]] (http--127.0.0.1-8080-2) Servlet.service() for servlet CreteFileServlet threw exception: java.lang.NullPointerException
    at com.bs.amg.test.servlets.CreateCSVConfigurationServlet.doGet(CreateCSVConfigurationServlet.java:136) [classes:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
    at java.lang.Thread.run(Thread.java:724) [rt.jar:1.7.0_25] 

Please, I really need your help!!!!!

¿Fue útil?

Solución 2

I think that the injection is not occuring because CDI is not enabled. Make sure you have a beans.xml as stated in CDI documentation: http://docs.oracle.com/javaee/6/tutorial/doc/gjbnz.html. Here is a sample beans.xml file you can use to enable CDI:

<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

Otros consejos

You are declaring your servlet with both webservlet annotation and web.xml mapping. You should have only one declaration using the annotation as in this example.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top