简体   繁体   English

java servlet PrintWriter的替代品

[英]java servlet PrintWriter alternative

I am new to Java and servlets. 我是Java和servlet的新手。 I was trying to develop a small web project in which I have one java servlet. 我试图开发一个小型Web项目,其中有一个Java Servlet。 As per the J2EE materials, to print something from servlet we should object (out) of PrintWriter. 根据J2EE资料,要从servlet打印某些内容,我们应该(不包括)PrintWriter。 And also I should write html code inside out.println(). 而且我也应该在out.println()中编写html代码。 If my page is quite big then this method of printing seems to be not comfortable. 如果我的页面很大,则这种打印方法似乎不舒服。 Do we have any other easiest way to print large pages of data from servlet 我们还有其他最简单的方法来从servlet打印大页数据吗?

Regards, Manohar 问候,Manohar

No, you're looking at information that's outdated. 不,您正在查看过时的信息。 (FYI - prefer "Java EE" to "J2EE". That acronym was true in 1999 when J2EE was announced, but it's old fashioned now. We're well beyond version 2.) (FYI-比“ J2EE”更喜欢“ Java EE”。该首字母缩写在1999年宣布J2EE时是正确的,但现在已经过时了。我们已经远远超过了版本2。)

I would say writing HTML in a servlet is a very bad idea. 我会说在servlet中编写HTML是一个非常糟糕的主意。

A better solution is to use templating to marry data into a template and generate HTML that way. 更好的解决方案是使用模板将数据嫁接到模板中并以这种方式生成HTML。

If you want to stick with the Java EE stack, you should use JSP and JSTL. 如果要坚持使用Java EE堆栈,则应使用JSP和JSTL。 A JSP is an HTML servlet generator template. JSP是HTML servlet生成器模板。 It gets compiled to a servlet that writes HTML for you. 它被编译为一个为您编写HTML的servlet。 If you stick with JSTL it'll be "taggy" and look more like HTML. 如果您坚持使用JSTL,它将变得“杂乱无章”,看起来更像HTML。

I prefer Velocity templates to JSP these days. 这些天,我更喜欢Velocity模板而不是JSP。

You need to use the servlet for your database access layer and business logic and then forward the request to a JSP for the view 您需要将servlet用于数据库访问层和业务逻辑,然后将请求转发到JSP进行视图

see here: http://www.datadisk.co.uk/html_docs/jsp/jsp_mvc_tutorial.htm 看到这里: http : //www.datadisk.co.uk/html_docs/jsp/jsp_mvc_tutorial.htm

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

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