What is JSP?

In short, a JSP file is a file written in HTML with Java snippets inside special tags. Normal HTML tags are translated to “out.write(“<html><head>My JSP page </head>”);” in the resulting servlet page. This establishes the view of the page and utilizes the HTML markup well.

     When complex logic is required, a Java expression or statement can be used. Expressions in a JSP file will result in direct printing to the page being viewed. Writing a Java expression in JSP should look like “<h2>Today is <%= new Date(); %></h2> where the underlined portion is a Java expression inserted into the HTML and denoted by the tag <%= …%>. When translated to a servlet, our code would become two to three lines, demonstrating how useful JSP is at saving time.

out.write(“<h2> Today is”); // “writing” the actual HTML as normal tags

out.print(new Date()); // inserting the Java variable/function call/etc.

out.write(“</h2>”); //closing off the HTML tag

       Similarly to the expressions, statements are written and translated in much the same way. Statements are surrounded by the <%.... %> tag, and can contain conditional statements, loops, and much more in standard Java language.

        When the dynamic HTML page is called, the servlet can detect if re-compiling is necessary due to changes. The JSP files developers create are stored in the webApp folder of the Tomcat directory. (Tomcat is the official implementation of Java servlets for JSP and free!) The servlet code is automatically translated from the JSP and stored in the Tomcat directory under “Tomcat/work/Catalina/localhost/ROOT/org/apache/yourfilename_jsp.jsp”. This code can be reviewed for comparison of how things are translated from HTML to full Java.

 

https://cs.au.dk/~amoeller/WWW/jsp/translation.html

https://www3.ntu.edu.sg/home/ehchua/programming/java/JSPByExample.html

Previous
Previous

JSP and the Request & Response Objects

Next
Next

How would I approach an established application with the intent to add a feature?