views:

80

answers:

2

Hi all,

I guess my questions are not well described. Is this coding style (like the example below) still recommended?

Aren't there any new methods of mixing html with jsp? I see some examples with the blockquote < script > < /script >. I guess that's for seperating code blockwise and not always the best way.

Any suggestions, let's say a framework, syntax coloring or even formatting would help.

My problem is that the relation or even errors between Class and JSP file is not always displayed well in eclipse. Or it's maybe not easy for a new person to read/understand it.

Here an example

<%} %>
<%if(condition) { %>
<tr>
    <td class="label"><%=method()%></td>
    <td class="text"><%=method()%></td>
</tr>
<%} %>
<tr>
+1  A: 

You could use tag libs (the Java standard tag lib for instance) if you like. The standard tag libs can help you get those ugly if statements out of the HTML. The statements are there of course, as tags (xml like things), but it fit's better in the JSP, and is easier to work with using Eclipse.

Do be aware that you don't mix business logic and view logic in a JSP. That will really make a JSP unreadable.

extraneon
A: 

Those ugly things are called scriptlets and yes, they are considered bad practice since a decade, since the moment that taglibs and EL were born. Nowadays they may be useful for quick prototyping, but really nothing more than that.

Nowadays, you normally use taglibs like JSTL to control the page flow and output. You normally use Expresson Language (EL) to access backend data. You can do fairly a lot at the presentation layer with them. If you comes to a point that it is not possible with either of them and you're forced to grab scriptlets, then the code logic ultimately belongs in a real Java class. You can use a Servlet class to control/preprocess/postprocess requests, you can use a Filter class to filter requests, you can use a DAO class to do the database interaction, you can use a Javabean class to store/transfer/access data, you can use a Domain class for business logic, you can use an Utility class for static tools. Use the JSP where it is intented for: a view technology to write template text like HTML/CSS/Javascript in which provides the possibility to interact with backend Java code and data by taglibs and EL.

Those tutorials are very good to get started with them all the right way: Beginner and Intermediate JSP/Servlet tutorials, Advanced JSP/Servlet tutorials.

That said, the <script></script> things where you're talking about have by the way nothing to do with Java/JSP. Those are HTML elements which indicate a block of Javascript. This is an entirely independent language with the first 4 characters of the name and a less or more simliar keyword/syntax usage as the only relation to Java.

BalusC
Thank you BalusC...This was the appropiate answer for my question.
Maglebolia
You're welcome.
BalusC