-1

JSP Application programming Which is better Coding in minimal number of JSP pages with multiple if conditions For example inserting sales,costs,expenses,customers,products etc with an if statement with variables.

if(variableValue=='Sales'
{

Sales insert code

}

else if(variableValue='Costs'
{
costs insert code
}

Creating multiple pages.

salesinsert.jsp costinsert.jsp etc

which is better and good programming. what are the advantages and disadvantages in short term and long term Performance Maintanence Migration to a different technology stack (such as java to python)

I know it is not good to write application logic in JSP. But unfortunately i cannot change it now. Both the ways i am coding logic in jsp page itself

Javanewbie
  • 73
  • 7
  • Shouldn't be putting logic in any JSPs. Scriptlet code should never be written. If you must put code in JSPs you should use JSTL. – duffymo Nov 19 '18 at 13:02

2 Answers2

1

This code isn't compatible with MVC pattern you should follow:

The model is the central component of the pattern. It is the application's dynamic data structure, independent of the user interface. It directly manages the data, logic and rules of the application.

A view can be any output representation of information, such as a chart or a diagram. Multiple views of the same information are possible, such as a bar chart for management and a tabular view for accountants.

The third part or section, the controller, accepts input and converts it to commands for the model or view.

JSP is view component and should decide which data to represent, but controller, which can be ActionServlet in struts

ActionServlet acts like FrontController pattern.

Community
  • 1
  • 1
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
1

It all depends on the size of the JSP pages:

  • Put it in the same page if the resulting JSP file has a reasonable size (less than 300 lines)
  • Create seperate JSP files / tag files if the combined page would be too big and if you can split it without adding a lot of complexity for passing data to the sub pages.

if statements in JSP files aren't necessarily application logic. Often you have the case that depending on some value part of the page is shown or not. I'd rather call this view logic. The same goes for for loops if you have a repeating view element.

You certainly don't want to have SQL, data manipulation, data validation etc. in JSP.

Codo
  • 75,595
  • 17
  • 168
  • 206
  • Can you kindly provide a sample code for jsp in MVC pattern. – Javanewbie Nov 19 '18 at 13:15
  • Why are you so keen on MVC – a pattern misunderstood so often? The best solution depends on your entire software – and we know hardly anything about it. When it comes to JSP, don't put the application / business logic (SQL, data manipulation, data validation etc.) there. Instead, prepare all the relevant data in a servlet or Spring controller so the JSP page is lean. You might even need simple Java classes (POJOs) just for providing the prepared data to the JSP file in a convenient form. – Codo Nov 19 '18 at 13:32