1

I have a Web Application developed inside NetBeans. It uses no frameworks at the moment, there are just some JSP pages. I would like to add some business logic, i.e. some Java classes that would process some data and pass it to the output. I understand, that JSP pages are the View layer. What about the rest (Model and Controller)?

Do I have to use a framework that would handle this (i.e. it's not possible to mix JSP with java class-based business logic without a framework)? Is JSP used only to be compiled into servlets, which have to be managed by something beyond JSP? I'm a newbie, so forgive me if it's trivial ;)

PS I know I can include Java code inside scriplet <% ... %> tags, but that's not what I'm looking for.

Roman C
  • 49,761
  • 33
  • 66
  • 176
ducin
  • 25,621
  • 41
  • 157
  • 256
  • 2
    You can always use servlets and use your business logic from within the servlets. – Dave Newton Jan 10 '13 at 20:29
  • better to start learning frameworks, especially that utilize the MVC pattern. – Roman C Jan 10 '13 at 20:41
  • This is heavily explained on our [Servlets tag wiki](http://stackoverflow.com/tags/servlets/info) and in links posted there like [How do servlets work? Instantiation, session variables and multithreading](http://stackoverflow.com/q/3106452/1065197) and [How to use Servlets and Ajax?](http://stackoverflow.com/q/4112686/1065197) – Luiggi Mendoza Jan 10 '13 at 21:42
  • Also, in order to learn how to do Web Applications in real life, you **must** know [How to avoid Java Code in JSP-Files?](http://stackoverflow.com/q/3177733/1065197) – Luiggi Mendoza Jan 10 '13 at 21:44

1 Answers1

1
  • You can call a Java Servlet implementation from your JSPs. (With your forms or the jsp:include and jsp:forward tags)
  • You might or might not want to use EJB. (Tutorial)
  • A framework is not mandatory though Spring, Hibernate and/or Struts might be useful.

If it is the first time you are using servlets, i recommend you to start with Servlet and POJOs only.

There is plenty of information here http://docs.oracle.com/javaee/6/tutorial/doc/bnafd.html

Adrián
  • 6,135
  • 1
  • 27
  • 49
  • Do you mean that if I implement a Servlet Java class manually (such code for example: http://pl.wikisource.org/wiki/Serwlet/kod) I can call it with a jsp:include action? – ducin Jan 10 '13 at 20:56
  • Yeah, you can. Although I'm not sure how would it work with a Writer like in your example. That's not a very good practice. I guess it's ok as a learning point though. – Adrián Jan 10 '13 at 21:00
  • Yep, that's it - I don't want to implement nothing much bigger than a 'hello world' in pure JSP, but I want to understand the differences between all those Java tools. This is quite confusing in the beginning ;) thanks for your reply. – ducin Jan 10 '13 at 21:11
  • 1
    I think it'll just print the strings in the servlet at the top of your page and it's likely to mess up with your CSS. More than enough for a Hello World though! – Adrián Jan 10 '13 at 21:12
  • 1
    My example worked. Maybe someone wll find it useful: look at http://stackoverflow.com/a/5649777/769384 and follow instructions – ducin Jan 10 '13 at 21:42
  • 1
    @tkoomzaaskz that's better explained in the Servlet SO wiki. – Luiggi Mendoza Jan 10 '13 at 21:46
  • @LuiggiMendoza Nice contribution --> [The Wiki](http://stackoverflow.com/tags/servlets/info) – Adrián Jan 10 '13 at 21:49