One of the points about the example above is that the HTML output was embedded in the Servlet. That is fine for our 9 lines of HTML output. However, typically there will be much more than that. Java Server Pages (JSP's) are a HTML template framework where you can write a HTML document and then use special tags that will be processed by the application server before sending the result to the browser. This is similar to the templates used in PHP and ASP.NET. Here is a basic JSP page.
Anything between <% %> tags is processed by the JSP processor. The first line
is the page directive telling the JSP processor what the content type and encoding are.
Lower down a variable called message is defined and then that is written out to
the browser. Anything in a <%= %> is written to the browser. To test it
out copy it to one of the web applications defined above and enter the URL
http://localhost:8080/bio/hello.jsp into your browser:
As your application scales in complexity you will want to separate the Java code from the HTML as
far as practical. One way to do this is to process the HTTP request with a Servlet first, place the
results in the request object, forward to a JSP page, and then show the results.
This is demonstrated in the BioJava section of this article,
which also gives an example of uploading a file in Java.
There are no user comments.
Please send ideas and opinions by email at alexamies@gmail.com.