Java code belongs to java file. JSP falls under view in MVC(Model View Controller) architecture which is used to display the content. Business processing should done in java files.
<% %> using this you can include the same code in JSP file and make it work, but scriplets are not good practice and instead JSTL and EL should be used.
Hope this helps. and Welcome to Javaranch :)