There are 2 pretty simple rules:
- Whenever you want to write Java code (business logic), do it in a Java class (so, Servlet).
- Whenever you want to write HTML/CSS/JS code (view/template logic), do it in a JSP.
Related question:
- How to avoid Java code in JSP