简体   繁体   English

Servlet doPost()方法设置?

[英]Servlet doPost() Method setup?

I am interested in creating a web app that uses JSP , Servlets and XML . 我对创建一个使用JSPServletsXML的Web应用感兴趣。

At the moment I have the following: 目前,我有以下内容:

JSP - Form input. JSP表单输入。

Servlet - Retrieving Form data and sending that data to a java object . Servlet检索Form数据并将该数据发送到java object

Java object (1) - Converts data into XML file....instantiates java object (2). Java object (1)-将数据​​转换为XML文件...实例化java object (2)。

Java object (2) - Sends that file to a database . Java object (2)-将文件发送到database

On the returning side the database will send back another XML file that I will then process using XSLT to display back to the user. 在返回端, database将发送回另一个XML file ,然后我将使用XSLT处理该XML file以显示给用户。

Can I place that XSLT code in the orignial Servlets doPost() method? 我可以将XSLT代码放在原始的Servlets doPost()方法中吗? So my doPost()` method would: 所以我的doPost()`方法将:

  1. Retrieve user inputted data from the form on my JSP page . 从我的JSP page上的表单中检索用户输入的数据。

  2. Instantiate a java object to convert that data to XML , in-turn that object will instantiates another object to send the XML file to a database . 实例化一个java object以将该数据转换为XML ,然后该object将实例化另一个object以将XML file发送到database

  3. Converts the resulting XML file sent from the database and displays it for the user. 转换从database发送的结果XML file并为用户显示。

Can one servlet doPost() method handle all of this? 一个servlet doPost()方法可以处理所有这些吗? If not, how would I set up my application and classes to handle this work flow? 如果没有,我将如何设置我的应用程序和classes来处理此工作流程?

Thank you in advance 先感谢您

I wouldn't load the XSLT in POST, because every method has to do it. 我不会在POST中加载XSLT,因为每种方法都必须这样做。

Read that XSTL in the init method, precompile and cache it. 在init方法中读取该XSTL,对其进行预编译和缓存。 Just make sure that you keep it thread safe. 只要确保您保持线程安全即可。

Once you have the XSLT, you've got to apply it to every XML response, so those steps do belong in POST. 一旦拥有XSLT,就必须将其应用于每个XML响应,因此这些步骤确实属于POST。

All your doPost() method has to do is generate a suitable servlet response (some form of content, and a suitable HTTP response structure). 您的doPost()方法要做的就是生成合适的servlet响应(某种形式的内容和合适的HTTP响应结构)。 So it can do anything you want (including the above). 因此它可以做任何您想做的事情(包括上面的事情)。

However it sounds like your rendering requirement is distinct from your form submission and storage requirement. 但是,听起来您的呈现要求与表单提交和存储要求不同。 So I would make your doPost() method delegate to a suitable method for rendering the output. 因此,我会让您的doPost()方法委托给合适的方法来呈现输出。 That way you can generate output from stored data separately from submitting data to the database. 这样,您可以从存储的数据生成输出,而不必将数据提交到数据库。

Well, this is not really specific to servlets, but more to Java/OOP (object oriented programming) in general. 好吧,这实际上并不是特定于servlet的,而是通常更多地针对Java / OOP(面向对象的编程)。 You can in fact do everything in a single method, even in a main() method. 你可以做实际上一切都在一个单一的方法,即使是在main()方法。 But hundreds or more of lines in a single method isn't really readable, maintainable, reuseable nor testable in long terms. 但是从长远来看,单一方法中的数百行或更多行实际上不是可读性,可维护性,可重用性或可测试性。 Right now, you're probably just starting with Java and you probably don't need to do anything else than this, but if you ever need to duplicate (almost) the same lines of code, then it's time to refactor. 现在,您可能只是从Java开始,您可能不需要做其他任何事情,但是,如果您需要(几乎)重复相同的代码行,那么就该重构了。 Extract the variables from the duplicate code lines and wrap those lines in a new method which takes those variables as arguments and does a simple one-step task. 从重复的代码行中提取变量 ,并将这些行包装在一个新方法中,该方法将这些变量作为参数并执行简单的单步任务。

In general, you'd like to already split the big task in separate subtasks beforehand, using separate and reuseable classes and methods. 通常,您希望已经使用分离的和可重用的类和方法预先将大任务拆分为单独的子任务。 In your case, you can for example have a single DAO class for all the DB interaction task, a generic XML helper class to convert Javabeans to XML and vice versa with help of XSL and (maybe) a domain object to manage the input/output processing (conversion/validation/errorhandling/response) and executing actions. 在您的情况下,例如,对于所有的数据库交互任务,您可以有一个DAO类,可以在XSL的帮助下使用一个通用XML帮助器类将Javabeans转换为XML,反之亦然,并且可以使用一个域对象来管理输入/输出。处理(转换/验证/错误处理/响应)和执行操作。 Write down in paper how the big picture is to be accomplished in small single tasks. 在纸上写下如何在小的单个任务中实现大局观。 Each task can be often as good done by a single method. 每个任务通常可以通过一种方法完成。 Group the methods with the same responsibilities and/or the same shared data in the same class. 将具有相同职责和/或相同共享数据的方法归为同一类。

To go a step further, for several tasks there may be 3rd party tools available which eases the task. 更进一步,对于一些任务,可能有简化任务的第三方工具可用。 I can think of for example XMLBeans and/or XStream to do the Javabean <--> XML conversion. 我可以想到例如XMLBeans和/或XStream来进行Javabean <-> XML转换。 That would already save a lot of boilerplate code and likely also the XSL step. 那已经节省了很多样板代码,可能还节省了XSL步骤。

That said, duffymo's suggestion to load the XSL only once is a very good one. 就是说,duffymo的建议只加载一次XSL是一个很好的建议。 You don't need to re-execute exactly the same task which isn't dependent on request parameters at all again and again on every request, that's only inefficient. 您无需重新执行完全相同的任务,而该任务完全不依赖于请求参数,而且每次请求都一次又一次地执行,这只是效率低下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM