JSP / Servlets - JavaServer Pages

Dynamically generated web content

Table des matières :

JSP Overview

JSP (Java Server Pages) est une technologie qui permet de mixer du code HTML statique et dymamique.
Les pages construites par CGI (ou servlets) sont entièrement générées même si une toute petite partie de celles ci est dynamique. Leur inconvénient majeur est de mélanger les parties statique et dynamique.
JSP permet de bien séparer la partie statique et dynamique. Voir l'exemple de code ci-dessous.
D'autres technologies semblables :

Example of JSP (HTML document with JSP tag) :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head><title>welcome to our store</title></head> <body> <h1>welcome to our store</h1> <small>welcome back, <% out.println(getUsernameFromCookie(request)); %>
to access your account settings, click <a href="account-settings.html">here.</a></small> <p> ...
</body></html>

Servlet Overview

A Servlet is actually a Java class you write that extends the javax.servlet.HttpServlet class. Here's how your servlet works:
  • A Web server receives an HTTP GET or POST request from a browser (or other TCP client)
  • The web server directs the HTTP request to the servlet engine (in the same way that direct CGI requests).
  • If your servlet is not already in memory, the servlet engine loads it and initializes it.
  • The servlet engine then encapsulates the HTTP request into a class called HttpServletRequest and delivers it to your servlet's doPost or doGet method.
  • Your servlet responds by writing HTML into HttpServletResponse which is sent back to the web server and delivered back to the TCP client.

Différence fondamentale entre JSP / Servlet

JavaServer Pages technology allows web developers and designers to rapidly develop and easily maintain, information-rich, dynamic web pages that leverage existing business systems. As part of the Java family, JSP technology enables rapid development of web-based applications that are platform independent. JavaServer Pages technology separates the user interface from content generation enabling designers to change the overall page layout without altering the underlying dynamic content.
JavaServer Pages technology uses XML-like tags and scriptlets written in the Java programming language to encapsulate the logic that generates the content for the page.
JavaServer Pages technology is an extension of the Java Servlet technology.
  • JSP : Le code est dans la page HTML (qui est compilé par le serveur JSP).
  • Servlet : La page HTML est generée par le code

JSP vs ASP

JSP and ASP deliver similar functionality. They both use tags to allow embedded code in an HTML page, session tracking, and database connection. Some of the trivial differences are:
  • ASP pages are written in VBScript and JSP pages are written in the Java programming language. Therefore, JSP pages are platform-independent and ASP pages are not.
  • JSP pages use JavaBeans technology as the component architecture and ASP pages use ActiveX components.
Beyond these trivial differences, there are a number of important differences that may help you in choosing a technology for your organization:
  • Speed and Scalability: Although ASP pages are cached, they are always interpreted. By contrast, JSP pages are compiled into Java servlets and loaded into memory the first time they are called, and executed for all subsequent calls. This gives JSP pages aspeed and scalability advantage over ASP pages.
  • Extensible Tags: JSP pages have an advanced feature known as extensible tags. This mechanism enables developers to create custom tags. In other words, extensible tags allow you to extend the JSP pages tag syntax. You cannot do this with ASP pages.
  • Freedom of Choice: Unless you install Chili!Soft ASP, ASP pages work only with Microsoft IIS and Personal Web Server. Using ASP pages requires a commitment to Microsoft products, while JSP pages do not tie you to any specific web server or operating system. JSP pages are becoming a widely supported standard.
For a more detailed comparison between JSP pages and ASP pages, see Comparing JSP and ASP.

Source : site de Sun

Here is a breakdown of the advantages of servlets over CGI

  • Efficient : With traditional CGI, a new process is started for each HTTP request. If the CGI program does a relatively fast operation, the overhead of starting the process can dominate the execution time. With servlets, the Java Virtual Machine stays up, and each request is handled by a lightweight Java thread, not a heavyweight operating system process. Servlets also have more alternatives than do regular CGI programs for optimizations such as caching previous computations, keeping database connections open, and the like.
  • Convenient : Hey, you already know Java. Why learn Perl too? Besides the convenience of being able to use a familiar language, servlets have an extensive infrastructure for automatically parsing and decoding HTML form data, reading and setting HTTP headers, handling cookies, tracking sessions, and many other such tasks.
  • Powerful : Java servlets let you easily do several things that are difficult or impossible with regular CGI. For one thing, servlets can talk directly to the Web server (regular CGI programs can't). This simplifies operations that need to look up images and other data stored in standard places. Servlets can also share data among each other, making useful things like database connection pools easy to implement. They can also maintain information from request to request, simplifying things like session tracking and caching of previous computations.
  • Portable : Servlets are written in Java and follow a well-standardized API. Consequently, servlets written for, say I-Planet Enterprise Server can run virtually unchanged on Apache, Microsoft IIS, or WebStar. Servlets are supported directly or via a plugin on almost every major Web server.
  • Inexpensive : There are a number of free or very inexpensive Web servers available that are good for "personal" use or low-volume Web sites. But with the major exception of Apache, which is free, most commercial-quality Web servers are relatively expensive. But once you have a Web server, no matter the cost of that server, adding servlet support to it (if it doesn't come preconfigured to support servlets) is usually cheap or even free.

Source : http://javaboutique.internet.com/tutorials/JSP/part01/page02.html


Article extrait du site Loribel.com.
https://loribel.com/java/topics/jsp.html