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