Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structure JSP/Java code where logic is not in the JSP file

Tags:

java

jsp

This is a design question and good practice question. How do you structure your Java web development such that a lot of logic is not in the JSP file. Should you use any of the JSP tags for if structures or loops. I see a lot of code where the logic is hard to follow because of poorly designed JSP files. And then when you want to output to a different format like a PDF or XML document, you can't do it because all the logic is in the JSP file.

Are there any tips or practices that you follow for Java Web development.

I am currently using a combination of Spring, Hibernate, Struts...and work with some Servlet code.

There are good practices associated with Java development. Many of us that have worked a while know them. What are some good practices for JSP development.

like image 554
Berlin Brown Avatar asked Nov 18 '08 18:11

Berlin Brown


People also ask

Where do I put Java code in JSP?

In JSP, java code can be written inside the jsp page using the scriptlet tag.

Which of the following JSP tag is most suitable to write the Java code inside the HTML of JSP page?

Scriptlet tag allows to write Java code into JSP file.

What is <%@ in JSP?

The page directive is used to provide instructions to the container that pertain to the current JSP page. You may code the page directives anywhere in your JSP page. By convention, page directives are coded at the top of the JSP page. Following is the basic syntax of page directive − <%@ page attribute = "value" %>

Can JSP perform both presentation and business logic?

In Servlet, you have to implement both business logic and presentation logic in the single file. Whereas in JSP, business logic is split from presentation logic using JavaBeans.


1 Answers

The easiest way to avoid placing logic in JSPs is simply to perform all that logic before forwarding a request to a JSP. The only logic you should need to do in a JSP is some basic looping (e.g. for creating HTML table rows), evaluating conditional statements, and data formatting.

All of this can be done without using scriptlet code (Java code) in the JSPs by using JSP tag libraries and EL. The most important tag library is JSTL. The JSTL tag library provides most of the logic you should ever need to perform in a view, though you may occasionally also use niche 3rd party tag libraries like displaytag which can reduce the amount of JSP code you need to write for specific tasks.

like image 87
Dónal Avatar answered Oct 21 '22 08:10

Dónal