Approaches to Web Development for Bioinformatics

Previous  Contents  Next
References

Java Server Pages

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.


<%@ page language="java" contentType="text/html; UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<%String message = "Hello JSP!"; %>
<%=message%>
</body>
</html>

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.


Previous  Contents  Next
References

Contributed Comments and NotesAdd a comment.

There are no user comments.

Google

Please send ideas and opinions by email at alexamies@gmail.com.

© 2006-2007 Alex Amies