4

我正在尝试熟悉 JavaEE。我对每个“组件”的目的(因为没有更好的词)有点困惑:会话 Bean 和 Servlet,以及它们如何与 Web 应用程序(客户端 JavaScript)正确交互。

为了理解这一点,我正在构建一个简单的 Web 应用程序。使用每个组件构建类似于以下内容的首选方法是什么:

  1. 用户访问“登录”页面
  2. 用户输入数据并点击提交。然后我使用 AJAX 发送请求以登录用户。
  3. 然后服务器端验证用户输入并“登录”用户(返回用户配置文件等)

发送请求时,我是将其发送到 Servlet(它使用 EJB)还是通过 WSDL 发送到会话 Bean?如何使用任何一种方法为该用户维护“状态”?我假设使用 Session Beans 就像用 @Stateful 注释一样简单。

另外,我假设从客户端发送的请求必须是 SOAP 格式。使用更轻量级的东西(例如 JSON)有多容易?虽然我更喜欢使用轻量级的东西,但如果 SOAP 使开发更快/更容易,那就没有必要了。

4

3 回答 3

3

Java 企业版教程几乎涵盖了您提出的所有主题;不同类型的 bean 类型的目的是什么,如何实现 Web 服务,如何实现身份验证等。

我强烈建议您花时间构建示例应用程序,特别是如果您对 Java Enterprise Edition (Java EE) 完全陌生。建立对核心概念的良好理解非常重要,因为由于构成 Java EE 的技术和标准的广度和深度,一开始可能很难知道应该关注什么。

需要记住的一件事是,虽然 Java EE 肯定会尝试支持最佳实践并支持设计和开发性能和可扩展性良好的安全企业应用程序,但它并没有规定或限制企业应用程序遵循一种特定的协议、数据格式和企业应用程序设计模式。一些协议和格式可以更好地被核心框架实现开箱即用地支持,并且一些选择取决于供应商,但很少有特定的技术选择被锁定在规范中。

To answer some of your specific questions, Java EE has great support for SOAP, but it does not preference nor limit web services to the SOAP protocol. With JAXB and JAX-RS it is just as easy to develop RESTful web services that accept and return XML or JSON, or both. It's up to you to decide whether you need to use SOAP, REST, or another protocol.

It's also your choice whether you want to use frameworks like JAX-RS or explicitly develop Servlets to handle HTTP requests and responses. In many cases, JAX-RS will have everything you need, meaning you'll be able to implement your web services as plain old Java methods with a few annotations without ever having to bother with marshalling and unmarshalling contents and parameters.

Similarly, with JAXB it's up to you whether you want to use WSDL or not. It's great if you have WSDL definitions, but no problem if you don't.

In many cases you will typically maintain state using the Java Persistence Architecture framework (JPA), and access and manipulate such data through stateless session beans. Developers new to Java EE are often tempted to use stateful session beans to maintain state that is better managed in the persistent storage. The tutorial takes you through the different kinds of bean types and their purpose.

于 2011-06-25T22:40:23.483 回答
1

Web services (WSDL, SOAP, etc.) are usually used for communications between applications.

Inside a single web app, you usually make simple GET/POST requests, using AJAX or not, and receive either a full HTML page, or a fragment of HTML (AJAX), or XML or JSON data (AJAX). The browser usually talks to a servlet, but it's rare to use servlets directly.

The usual way is to use a framework on top of servlets. The frameworks can be divided in two big categories : action-based frameworks (Stripes, Spring MVC, Struts, etc.) or component-based frameworks (JSF, Wicket, Tapestry, etc.).

In a n-tier application, all of the above technologies are supposed to only contain the presentation layer. This presentation layer talks to a business layer, where the real business logic happens, where transactions are used to access databases, messaging systems, etc. This business layer is where EJBs are used.

于 2011-06-25T22:40:27.320 回答
0

You can create basic architecture as follows :

Create EAR instread two different Project like EJB Jar and Web Application WAR

You can create servlets which will call some delegate class which has logic to reffer the EJB Either by calling it as remote call/ Either by Using @EJB annotation in the Delegation Class.

 ServletClass {   
     do/post(){
     DelegateClass d = new DelegateClass();
     d.callMethod(withParam);
   }
  }


    DelegateClass   {
       @EJB
       EJBlocalinterface  ejbintance;
       void callMethod(DefinPrarm){
          ejbinstance.callEJBMethod();
       }
    }



    @Statelss
    EJBbeanClass implements EJBlocalinterface{
        void callEJBmethod(someParam){
        }
    }
于 2013-04-15T10:42:09.313 回答