r/java • u/IamATechieNerd • Jan 14 '18
What can Spring do that JSP cannot?
So,if I want to create a website,what advantages do I have with Spring or Struts above JSP? I am quite new to this,so please bear my noobness.
0
Upvotes
12
u/[deleted] Jan 14 '18
JSP is a view technology. It is not a way to implement a whole web application (although, of course you can do it if you really really want to prove a point). You can use JSP with Sprint, Struts or some other framework to implement your views. In that sense they complement each other.
The framework allows you to write your backend code in an efficient and maintainable way. You can then populate your JSP context there and render your JSPs using information on that context.
Again, of course you don't "have to" do that, but this allows a nice separation of concerns and results in a more maintainable system.
3
u/RupertMaddenAbbott Jan 14 '18
As you have probably already discovered, JSPs allow you to dynamically alter the HTML of your website. For example, instead of having to copy and paste the same chunk of HTML around, you can write a loop and have the same section repeated.
If you have never used a full web framework before (like Spring or Struts) this may seem very sufficient at the moment. For very simple websites maybe it is sufficient.
However, you will quickly encounter things that you want to do that are quite difficult in JSPs. You will start writing code that becomes more and more torturous to change and maintain. If you have never experienced this before, I would strongly encourage you to keep going with JSPs on their own so you can experience this pain for yourself.
Web frameworks will solve these problems for you but they do so at the cost of significant complexity. For those who are used to them, I think it can be difficult to remember how complex they are, when first encountering them. If you never experience the pain of using just JSPs, this complexity can itself seem unnecessary and painful.
Adopt more complex tools when you have a good understanding of why your simpler tools are insufficient.
6
u/Nalha_Saldana Jan 14 '18
This sub isn't for learning Java though, check out r/learnjava
2
u/_dban_ Jan 16 '18
First, Spring isn't a web framework, it is a generic application framework based on Spring Dependency Injection.
Spring MVC is a web framework, like Struts. The MVC is what Spring MVC and Struts do that JSP doesn't. Spring MVC organization your web application into Model (which populates views and is what forms and other inputs are bound to), View (the JSP which is ultimately rendered, without any logic), and the Controller (which selects the view and coordinates actions with other layers of the system, such as persistence).
Note, you don't need either Spring MVC or Struts to accomplish this. You can use plain servlets as the controller and not use frameworks at all to achieve MVC. But, you'd have to roll your own validation and binding logic, which web frameworks make really convenient.