使用EJB和Servlet进行Web应用程序的首选方法是什么? [英] What is the preferred way to use EJBs and Servlets for web applications?

查看:232
本文介绍了使用EJB和Servlet进行Web应用程序的首选方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试熟悉JavaEE。对于每个组件(缺乏更好的词)的目的是什么是会话Bean和Servlet,以及它们如何与Web应用程序(客户端JavaScript)进行正确的交互,我有点困惑。



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


  1. 用户访问登录页面

  2. 用户输入数据并点击提交。然后,我发送一个请求与AJAX登录用户。

  3. 服务器端然后验证用户输入和记录用户(返回用户配置文件等)

发送请求时,是否通过WSDL将其发送到Servlet(使用EJB)或Session Bean?如何使用这两种方法维护该用户的状态?我假设与会话Bean一样简单,用@Stateful来注释它。



此外,我认为从客户端发送的请求必须是SOAP格式。使用更轻量级的东西(如JSON)有多容易?虽然我更喜欢使用轻量级的东西,但是如果SOAP使开发更快/更容易,则不需要。

解决方案

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



我强烈建议您花点时间来构建示例应用程序,特别是如果您完全是Java Enterprise Edition(Java EE)新手。重要的是您对核心概念有一个很好的了解,因为Java EE的技术和标准的广度和深度,可能很难知道一开始就要关注什么。


需要记住的一点是,尽管Java EE肯定会尽可能地支持最佳实践,并使安全企业应用程序的设计和开发能够执行和扩展,但它并不规定或限制企业应用程序遵循一个特定协议,数据格式和企业应用设计模式。一些协议和格式通过核心框架实现得到更好的支持,一些选择是供应商依赖的,但很少有具体的技术选择被锁定在规范中。



为了回答您的一些具体问题,Java EE对SOAP有很大的支持,但是它并不倾向于将SOAP服务限制在SOAP协议上。使用JAXB和JAX-RS,开发接受和返回XML或JSON的RESTful Web服务,或者同时兼容。您需要决定是否需要使用SOAP,REST或其他协议。



您也可以选择是否使用JAX-RS或明确的框架开发Servlet来处理HTTP请求和响应。在许多情况下,JAX-RS将拥有您所需要的一切,这意味着您将能够以简单的旧Java方法实现您的Web服务,并附上几个注释,而无需打扰编组和解组的内容和参数。



同样的,使用JAXB由你决定是否要使用WSDL。如果您有WSDL定义,那么很好,但是如果没有,则不会出现问题。



在许多情况下,您通常会使用Java Persistence Architecture框架(JPA)维护状态,并通过无状态会话bean访问和操纵这些数据。开发人员通常会喜欢使用有状态会话bean来维护在持久存储中更好地管理的状态。本教程将引导您了解不同类型的bean类型及其用途。


I am trying to familiarize myself with JavaEE. I am a bit confused as to what the purpose of each "component" (for lack of a better word) is: Session Beans and Servlets, and how they properly interact with a web application (client-side JavaScript).

In an attempt to understand this I am building a simple web application. What is the preferred way to use each component to build something similar to the following:

  1. User visits a "Log in" page
  2. User inputs data and clicks submit. I then send an request with AJAX to log in the user.
  3. The server side then validates the user input and "logs" the user in (returns user profile, etc.)

When sending the request, do I send it to a Servlet (which uses an EJB), or to a Session Bean through WSDL? How do I go about maintaining a "state" for that user using either method? I assume with Session Beans it's as simple as annotating it with @Stateful.

Also, I assume the requests sent from the client side must be in SOAP format. How easy is it to use something more lightweight (such as JSON)? While I would prefer to use something lightweight, it's not necessary if SOAP makes development faster/easier.

解决方案

The Java Enterprise Edition tutorial address pretty much all of the topics you bring up; what's the purpose with the different kind of bean types, how do I implement web services, how do I implement authentication, etc.

I highly recommend you take the time to build the sample application, especially if you're completely new to the Java Enterprise Edition (Java EE). It is important you build up a good understanding of the core concepts because it can be hard to know what to focus on in the beginning due to the breadth and depth of technologies and standards that comprise Java EE.

One thing to keep in mind is that while Java EE certainly tries to support best practice and enable design and development of secure enterprise applications that perform and scale well, it does not prescribe or limit enterprise applications to follow one particular protocol, data format, and enterprise application design pattern. Some protocols and formats are better supported out of the box by the core framework implementations, and some choices are vendor-dependent, but very few specific technology choices are locked into the specification.

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.

这篇关于使用EJB和Servlet进行Web应用程序的首选方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆