I. overview of Servlet and JSP

  • 2020-05-05 11:42:57
  • OfStack

1.1 Java Servlet and its features

Servlet is the Java technology's answer to CGI programming. The Servlet program runs on the server side, dynamically generating Web pages. Java Servlet is more efficient, easier to use, more powerful, more portable, and less expensive than traditional CGI and many other technologies like CGI
Efficient.

In traditional CGI, each request starts a new process, and if the CGI program itself runs short, the cost of starting the process is likely to exceed the actual execution time. In Servlet, each request is handled by a lightweight Java thread (rather than a heavyweight operating system process).
In traditional CGI, if there are N concurrent requests to the same CGI program, the code of the CGI program is loaded N times in memory. For Servlet, the request is handled by N threads, requiring only one copy of the Servlet class code. In terms of performance optimization, Servlet also has more options than CGI, such as buffering previous calculations, keeping database connections active, and so on.


Convenient.

Servlet provides a number of utility routines, such as automatically parsing and decoding HTML form data, reading and setting HTTP headers, processing Cookie, tracking session state, and so on.


Powerful.

In Servlet, many of the tasks that are difficult to do with the traditional CGI program can be done easily. For example, Servlet can interact directly with the Web server, whereas normal CGI programs cannot. Servlet also has the ability to share data between programs, making things like database connection pooling easy.


Good portability.

Servlet is written in Java, and Servlet API has perfect standards. Therefore, Servlet written for I-Planet Enterprise Server can be ported to Apache, Microsoft IIS or WebStar without any substantial changes. Almost all major servers support Servlet either directly or through plug-ins.


Save on investment.

Not only are there many cheap or even free Web servers available for individuals or small scale websites, but these features are often free (or require very little investment) on existing servers if they do not support Servlet.
1.2 JSP and its features

JavaServer Pages (JSP) is a technology that implements the hybrid encoding of common static HTML and dynamic HTML. Please refer to the JSP technical introduction for an explanation of the basic concepts of JSP.

Many pages generated by CGI programs are still mostly static HTML, with dynamic content appearing in only a limited number of parts of the page. But most CGI technologies, including Servlet, and their variants, always programmatically generate the entire page. JSP allows us to create these two parts separately. For example, here is a simple JSP page:

The < HTML >
Welcome to the online store < /TITLE > < TITLE > < /TITLE > < /HEAD >
The < BODY >
< H1 > welcome < /H1 >
< SMALL > welcome,

< % out println (Utils getUserNameFromCookie (request)); % >
To set account information, click
The < A HREF = & quot; Account - Settings. html & quot; This is < /A > < /SMALL >
The < P >
The rest of the page. .
< /BODY > < /HTML >



Here is a simple comparison of JSP and other similar or related technologies:

JSP compared to Active Server Pages (ASP)

Microsoft's ASP is a similar technology to JSP. JSP has two advantages over ASP. First, the dynamic part, written in Java instead of VB Script or other Microsoft languages, is both more powerful and easier to use. Second, JSP applications can be ported to other operating systems and non-Microsoft Web servers.


JSP compared to Servlet pure

JSP does not add any functionality that is essentially impossible to implement with Servlet. However, it is more convenient to write static HTML in JSP, instead of having to output each line of HTML code in an println statement. More importantly, the separation of content and appearance makes it easy to separate the tasks of different nature in page production: for example, HTML is designed by a page designer, while leaving room for the Servlet programmer to insert dynamic content.


JSP and server-side include (Server-Side Include, SSI) compared to

SSI is a widely supported technique for introducing external code into static HTML. JSP's support in this area is more complete because it can generate dynamic content using Servlet instead of a stand-alone program. In addition, SSI is really just for simple inclusion, not for "real" programs that can process form data and access databases.


JSP compared to JavaScript

JavaScript can dynamically generate HTML on the client side. While JavaScript is useful, it can only handle dynamic information based on the client environment. With the exception of Cookie, HTTP state and form submission data are not available to JavaScript. In addition, because it runs on the client side, JavaScript cannot access server-side resources, such as databases, directory information, and so on.





Related articles: