1

I am new to JSP programming and am writing a web app for a family member. As I study, I hear a lot about how JSP's are supposed to be used for presentation and servlets are for business logic. My question is basically about how far that goes and when my use of JSTL would be bad practice. Here's an example: I have a login page for my app, and I am using c:if's with custom functions connected to my java classes to process the form. Would that be considered poor MVC practice or, since I'm only referencing my logic code from EL, is this a legitimate use of JSP's?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Patrick S.
  • 275
  • 1
  • 5
  • 19
  • 1
    JSPs are an older technology, a 1990s response to Microsoft Active Server Pages. A more modern approach would say view pages using HTML5, CSS3, and JavaScript; REST services; HTTP servlets/controllers to interact with clients. – duffymo Sep 14 '16 at 14:46
  • So you would recommend not using them at all? – Patrick S. Sep 14 '16 at 14:52
  • 1
    Please don't take that advice. HTML5, CSS3 and JavaScript are all great but they don't help you at all for dynamic Java web applications. It sound's like your building a Java web app. Use JSPs. Otherwise you limit what you can do. – Shaggy Sep 14 '16 at 14:56
  • @JaxSwagger That's not true. Like duffymo said, ajax/REST apps are popular these days, and they're just as dynamic (even more actually) than JSPs. – Kayaman Sep 14 '16 at 15:00
  • @Kayaman Please read the question. OP is asking about JSTL and EL. Use of Ajax/Rest is awesome, but at the same time it's not the question. – Shaggy Sep 14 '16 at 15:06
  • Yes they do. You can do dynamic web pages using any templating solution: Velocity, Thymeleaf, or JSP. The user explicity asked about best practices. I don't think that recommending a 1990s technology as best practice in 2015 is correct. – duffymo Sep 14 '16 at 15:10
  • 1
    Yes, I am more interested in the use of JSTL and EL. I chose that architecture because it seemed like a lot of the job posts regarding Java mentioned JSP as a requirement, and I am new and trying to determine the best framework to gain experience in with Java. Maybe it is old but still used? – Patrick S. Sep 14 '16 at 15:10
  • Old but still used, yes. Just be aware that there are other possibilities that offer advantages. You said "best". I don't think JSP is best. – duffymo Sep 14 '16 at 15:11
  • @JaxSwagger No, I was answering your wrong comment about not being able to create dynamic web applications without JSP. You can create them with JSP, but JSP is not a requirement for them. There's more than one way to skin a cat, your comment was misleading. Then you've got Thymeleaf, Velocity, a whole lot of other things that aren't JSP. – Kayaman Sep 14 '16 at 15:12
  • 4
    Just be aware that if you write JSPs with scriptlet code we will find you. Only use JSTL and stick to tags. – duffymo Sep 14 '16 at 15:13
  • Yes, I have studied it enough so far to determine scriptlets are a no-no and I also need to keep it as MVC as possible. A lot to think about. Maybe I will get my feet wet with REST while I finish learning JSP. – Patrick S. Sep 14 '16 at 15:14
  • The best of the best that is not the best. – Roman C Sep 15 '16 at 19:13

3 Answers3

5

Your question contains a lot of what are best-practices which invokes a lot of opinion and debate, which is usually frowned upon in this forum. In general, the JSP is the "V"iew in MVC and should be used to present the data provided by the "M"odel which would be your Java code. The "C"ontroller is often scattered between the M and the V (inviting more debate, sorry).

Any logic you put in your JSP that is beyond looking at the data given you and deciding how to present it, moves it towards the Model. Your login page should just collect the credentials and present them to the Model, which should in turn respond with "Invalid" and re-request the credentials (or fail completely) or if valid, move on to the next page.

In practice, IMHO, you should not put a lot, if any, code that manipulates the data except for formatting it - creating table entries, wrapping with links, etc. You should not (IMHO) query databases, perform calculations, etc., in the JSP - let the Model do that.

As duffymo stated, JSPs are old, but they are still valid. I would suggest that you also consider AngularJs (ng) (after reading about the controversy of V1 v. V2).

user3481644
  • 398
  • 2
  • 12
4

JSP is an outdated technology and there are very few Softwares that still use it. But if you want to use it I would suggest that you use Oracle Coding Standards with it. This page should give you a clear idea of what you should and shouldn't do with it.

Henrique Martins
  • 352
  • 2
  • 11
0

The best practice with JSP - is not to use JSP at all. I’ll try to explain why and be clear.

First I have to explain something that does not have a connection to JSP at all, but it will help you to understand exact problems with JSP technology.

In functional programming there is a term - pure function. It means that it does not have side effects. Additionally, such function does guarantee that for each invocation with the same input it ALWAYS return the same output. In OOP functions are not pure. It may have side effects. It makes our life more complicated. But what is important is that these side effects can happen only WITHIN your function. You can debug it. More or less it is UNDER YOUR CONTROL.

Let’s imagine our functionality written in JSP as a function f with input I and output O:

O f(I)
  1. The first problem with JSP is that it DOES have side effects AND such side effects can happen not only inside of your function f, but also can affect it from outside. A simple example: you use tiles technology, your jsp page is used as a component in a tiles template. Another component of this template uses getOutputstream() method and writes to this output stream. But an application can either call getOutputStream or getWriter on any given response, it's not allowed to do both. JSP engines use getWriter, and so you cannot call getOutputStream. And you get in your jsp page that works fine when it is alone:

    java.lang.IllegalStateException: getOutputStream() has already been called for this response

getOutputStream() has already been called for this response

  1. With a function you get explicitly input parameters. The input is always clear. Additionally you can use constants or, if your function has side effect use another service to get data for processing. But it is always WITHIN your function and more or less under control. With JSP pages you do not have such control at all. Input data can be put into session with different servlets/web components, input data can be put into request scope via servlet with a lot of if statements. You must first investigate a logic of this servlet. It is additional complexity that is not obvious when you create “Hello World!” program, but that really makes you crazy when you maintain such pages, written several years ago.
  2. I think you have already read that mixing both output and logic is not a good idea. JSP allows people to do that. Cause it is “so convenient”.
  3. You cannot test logic within your jsp pages. Or it makes it more difficult.

You can say that correct usage of jsp technology and applying best practices resolve most of issues. Yes. Agree. But it will never get rid of its internal drawbacks and complexity. You always have to check, really developers followed best practices or not? There are better, much better technologies our days.

Note: the only exception, or use case, when I’d personally would use it: for localisation. You do not have get all messages from server. You do not want to ask a server to get a localized string one by one. You do want to get a batch of values, that will be used on your web form, for instance. With JSP + JS you can do that very easily and explicite.

Community
  • 1
  • 1
Alexandr
  • 9,213
  • 12
  • 62
  • 102