知方号

知方号

What is JSP? Introduction to Jakarta Server Pages

Layout of a JSP web application

Tomcat follows the standard layout for Java servlet and JSP pages. The general structure is a folder with the name of the application, which is deployed inside Tomcat’s /webapps directory.

In the Implicit Objects application, the application is /examples. Opening that folder reveals three important children: the /WEB-INF directory, the index.html file, and a set of other directories.

The WEB-INF directory contains metadata that informs Tomcat how to run the application.

The remaining content, including index.html, is directly accessible, just as it would be in a typical web server. Likewise, the implicit-objects.jsp is available to view, and is automatically handled as a JSP page (not an HTML page) by the server.

JSP’s request-response architecture

In a typical servlet/JSP application, servlets receive requests and interact with server-side components and services (such as the database) to fulfill them. Once handled, the request result is forwarded to a JSP page, which presents the response based on the data.

A servlet uses the following syntax when sending a request response to a JSP page:

request.getRequestDispatcher("path/to/jsp.jsp").forward(req,res);

Whatever data the servlet has inserted into the various JSP scopes will be accessible to JSP as objects, like the param object you saw earlier. A scope is simply a range of life for a given object. Request and response objects are request scoped, meaning they live for the duration of the request. Other scopes include page scope (living as long as the JSP page does) and session scope (terminating when the user ends the given session).

MVC in JSP and servlet applications

The interaction between servlets and JSP pages follows the classic MVC pattern: the controller object (a servlet) prepares the model object (an item or user object) and sends it to the view (the JSP page) for rendering. MVC’s clean separation of concerns has made it a popular and long-lived approach to building software. Struts is one of the oldest and most well-known frameworks implementing MVC with JSP and servlets. Spring MVC also includes built-in support for JSP.

Using Spring MVC is probably the most common approach to JSP development within a larger framework, since it gives you access to the entire Spring ecosystem. A good way to accelerate the process is use a JSP Spring Boot template.

Conclusion

JSP is a well-known and versatile technology for developing Java web application views. Combined with servlets, JSP pages are very powerful and provide access to the entire range of Java capabilities. While Jakarta Server Pages may not be your first choice for developing a modern Java web application, you will encounter JSP pages in legacy applications. It’s also a good choice for simpler applications, where you want to quickly connect your HTML front end to server-side Java code. The JSTL tags are capable of handling most web application requirements, and third-party libraries extend that functionality for specific technologies and use cases.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lizi9903@foxmail.com举报,一经查实,本站将立刻删除。