HTML in servlets

Hi,

I was thinking that U could add some tools to make writing HTML code in servlets easier. I'm not familiar with writing Idea plugins so I cant write it by my self, although I think it would be a useful one

8 comments


I think the idea is that one shouldn't be writting HTML as strings in servlets. It should be either fragments loaded from static files, or you should bite the bullet and go to JSP's. Automation is good, automating bad practices less so.

--Dave Griffith

0

Kalin wrote:

Hi,

I was thinking that U could add some tools to make writing HTML code in servlets easier. I'm not familiar with writing Idea plugins so I cant write it by my self, although I think it would be a useful one


A simple workaround would be to write the HTML in an HTML file (with
support from IDEA), and then copy the HTML text into a Java String literal.

E.g. From an HTML/XML/JSP file:
Pasted into a Java file string literal like this: "" Becomes: "

" It even handles newlines, converting them into a \n and string concat like this: "\n" + " ]]>"

--
Rob Harwood
Software Developer
JetBrains Inc.
http://www.jetbrains.com
"Develop with pleasure!"
0

Note that in spite of all the servlet books, writing plain HTML directly
in servlets is a bad idea from many reasons:
1. Separation between the controller and view is non existent.
2. It is by far not object oriented and thus cumbersome and yields HTML
code duplications.
3. Very hard to maintain (close tags, correct syntax, etc.) when you add
bits and pieces of HTML plus have controller code.

I would suggest not dwelling in this as it is just a bad idea to begin
with. Even JSP or JSF (which I am not an advocate of) and even Fabrique
are there to work in a more controlled layered environment rather than
have servlets that are 1 big pile of code.

I am not saying anyone here is a bad coder or engineer, I just want to
prevent focusing on methodologies which are undesirable in real world
large scale (at present or future) projects.

Amnon

Rob Harwood (JetBrains) wrote:

Kalin wrote:

>> Hi,
>>
>> I was thinking that U could add some tools to make writing HTML code
>> in servlets easier. I'm not familiar with writing Idea plugins so I
>> cant write it by my self, although I think it would be a useful one


A simple workaround would be to write the HTML in an HTML file (with
support from IDEA), and then copy the HTML text into a Java String literal.

E.g. From an HTML/XML/JSP file:
<td align="left">

Pasted into a Java file string literal like this:
""

Becomes:
"<td align=\"left\">"

It even handles newlines, converting them into a \n and string concat
like this:
"<html>\n" +
" <head>"

0

Amnon I. Govrin wrote:

Note that in spite of all the servlet books, writing plain HTML directly
in servlets is a bad idea from many reasons:


Oh, of course. I was thinking more for like a quick and dirty servlet.
It can also be useful for certain types of simple JUnit tests, instead
of using a file. It's just a technique that can be useful sometimes.

--
Rob Harwood
Software Developer
JetBrains Inc.
http://www.jetbrains.com
"Develop with pleasure!"

0

You're right. I guess I replied to the wrong post as I actually wanted
to refer to the idea of the feature as a whole, not necessarily your
workaround for specific cases.

I just wanted to avoid a case when an IDE endorses bad practice.

Amnon

Rob Harwood (JetBrains) wrote:

Amnon I. Govrin wrote:

>> Note that in spite of all the servlet books, writing plain HTML
>> directly in servlets is a bad idea from many reasons:


Oh, of course. I was thinking more for like a quick and dirty servlet.
It can also be useful for certain types of simple JUnit tests, instead
of using a file. It's just a technique that can be useful sometimes.

0

I think the problem is writing HTML in java strings, whether it is a servlet class or regular java class.

On the enterprise web portal software I work on, we do have most of the HTML in JSPs, but we also have alot of html formatting java classes which do have embedded html. One example, is a TableWriter class which takes column info, column data, column sort indexes, and outputs the html. It wraps up all functionality like sorting on a column, pagination, etc.

I personally would like to see better support for html in java strings.. but I get by using the trick Rob suggested. Also, since 95% of the HTML is in JSP pages, it isn't much of a problem.

0

With JSP 2.0 tag files, it's hard to justify the need for this.

Norris Shelton
Sun Certified Java Programmer




Alex wrote:

>I think the problem is writing HTML in java strings, whether it is a servlet class or regular java class.
>
>On the enterprise web portal software I work on, we do have most of the HTML in JSPs, but we also have alot of html formatting java classes which do have embedded html. One example, is a TableWriter class which takes column info, column data, column sort indexes, and outputs the html. It wraps up all functionality like sorting on a column, pagination, etc.
>
>I personally would like to see better support for html in java strings.. but I get by using the trick Rob suggested. Also, since 95% of the HTML is in JSP pages, it isn't much of a problem.

>

0

I would like to have IDEA support HTML in Servlets. Although this is not a Best practice, It's often Useful for a variety of Use Cases.

0

Please sign in to leave a comment.