Servlet在Intellij IDEA的EJB项目中不起作用

时间:2015-06-14 16:15:32

标签: intellij-idea ejb wildfly ear java-ee-7

我是Intellij IDEA的新手,我在其中创建了一个测试Java EE项目。首先在New Project中我选择了Java Enterprise(Java JDK 7)并添加了额外的Libraries:

  • 网络应用程序(3.1)
  • EJB:Enterprise Java Bean(3.2不带ejb-jar.xml)
  • Java EE应用程序(Java EE 7)

该应用已成功创建。然后我创建了无状态Java类:

package test.server;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;


@Stateless(name = "TestClientEJB")
@LocalBean
public class TestClientBean {
    public TestClientBean() {
    }

    public String getHello() {
        return "Hello EJB from Intellij";
    }

}

并在创建Servlet之后:

package test.client;

import test.server.TestClientBean;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "ClientServlet")
public class ClientServlet extends HttpServlet {

    @EJB
    TestClientBean testClientBean;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.print(testClientBean.getHello());
    }
}

作为Application Server,我使用WildFly 8.1 Final。我使用EAR工件设置它,并将URL指定为:

http://localhost:8080/EJBTestWeb/

当我在浏览器中运行它时,它会显示我在index.jsp中写的内容。但是当我写类型

http://localhost:8080/EJBTestWeb/ClientServlet

它显示我未找到的消息。问题出在哪儿?我下载所有需要的罐子

1 个答案:

答案 0 :(得分:0)

在Servlet中我改变了

@WebServlet(name = "ClientServlet")

@WebServlet("/ClientServlet")