Reputation: 1739
I am an absolute beginner at JSP and Servlet Web development thus I am struggling a little with some conceptual things.
1) I don't really understand why we need to have pure Java code files which we compile but also have separate JSP files... Don't they both end up implementing logic/functions in Java?
2) How do you determine which functions/operations you should include in the java file and which you should include in the JSP files...?
3) Lastly, there is HTML code in both the Java and JSP file (although in the Java file we are dynamically printing the HTML code. But doesn't this result in repeat HTML code?
Upvotes: 1
Views: 4440
Reputation: 90427
1) I don't really understand why we need to have pure Java code files which we compile but also have separate JSP files... Don't they both end up implementing logic/functions in Java?
I think the pure Java code files you mean is the Servlet.In fact , You can write the whole web application only using the Servlet, but you will find the following downside in your application:
HTML is generated using the println()
, which means that if want to make some changes to the HTML , the java code has to be changed , recompiled and then redeployed.It results in the maintenance problem.
The java code is hard to understand for the web page designer .
Weak separation between the contents and the presentation as every things are expressed in the Java code.
JSP is designed to address these downsides :
You don't have to re-compile the JSP to reflect the changes . Just update and save the JSP , the web container will automatically recompile the code when the JSP is retrieved in the next request .
JSP pages look very similar to HTML pages, it is a lot easier for Web page designers to work with JSP pages instead of Servlet.
JSP helps separate between the presentation and business logic code , in which the presentation can be expressed in the JSP while the business logic can be implemented in the form of Java Beans or custom tags.This separation will result in a better maintainability .
2) How do you determine which functions/operations you should include in the java file and which you should include in the JSP files...?
The strength of JSP is displaying HTML .You should use JSP to define the view to present the data to the user.
Servlet's strength is in the area of controlling and dispatching .For example , if you want to display different contents depending on some input parameters or some business logic processing ,you should do this dispatching work and choose which JSP to display the result in the Servlet.
If the request 's output does not contain HTML , for example generating a graph or downloading a file , you should do it in the Servlet.
So , from the MVC pattern point 's of the view , Servlet plays the role of Controller while JSP plays the role of View.
You can found the best practices of using Servlets and JSP here: http://www.oracle.com/technetwork/articles/javase/servlets-jsp-140445.html
Upvotes: 4
Reputation: 422
An explanation of java servlets vs. JSP can be found here:
http://www.oxxus.net/tutorials/jsp/why-jsp
Most importantly
JSP enables a clean separation of business logic from presentation. JSP, by using Java as the scripting language, is not limited to a specific vendor platform. JSP, as an integral part of the J2EE architecture, has full access to server-side resources.
2: If the function is only going to be called by that JSP, go ahead and throw it in there. In general you should just be calling out to the java's from the JSPs and not leaving too much code in them. Building the functions into the .java's and calling them from the JSPs will promote code reuse and make you think more carefully about how you implement those functions.
3: Try to avoid mixing presentation (HTML, and otherwise) with the business logic (Java).
doesn't this result in repeat html code?
It does, don't do that.
Upvotes: 1