Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What's the difference between JSP and Servlet?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces what is the difference between JSP and Servlet. It is very detailed and has a certain reference value. Friends who are interested must finish it!

What is the difference between JSP and Servlet, and what is the connection between them?

In fact, Servlet technology appeared very early and was developed for the server-side application of Java at that time. Everyone knows that Applet is the application Mini Program, and Servlet is the server-side Mini Program. However, with the advent of Microsoft's ASP technology, line-by-line output statements are very clumsy when using Servlet for response output, especially for complex layouts or displaying pages. JSP is developed on top of Servlet technology to meet this requirement. It can be seen that there is an inherent blood relationship between JSP and Servlet. If we can grasp this connection when learning JSP, we can understand the operation mechanism of JSP application more deeply and achieve twice the result with half the effort.

Through the analysis of the running process of a JSP application, this paper goes deep into the inside story of the operation of JSP, and expounds some technical key points of JSP from a new perspective.

HelloWorld.jsp

Let's take the Tomcat 4.1.17 server as an example to see how the simplest HelloWorld.jsp application works.

Code listing 1:HelloWorld.jsp

HelloWorld.jsp

< % String message = "Hello World!"; %>

< %=message%>

This file is very simple, only defines a variable of String, and outputs. Put this file in the webappsROOT directory of Tomcat, start Tomcat, access http://localhost:8080/HelloWorld.jsp in the browser, and the output in the browser is "HelloWorld!"

Let's see what Tomcat has done. Go to the workStandalonelocalhost_ directory of Tomcat and find the following HelloWorld_jsp.java, which is the source file generated when Tomcat parses HelloWorld.jsp:

Code listing 2:HelloWorld_jsp.java

Package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import org.apache.jasper.runtime.*; public class HelloWorld_jsp extends HttpJspBase {. Public void _ jspService (HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException {JspFactory _ jspxFactory = null; javax.servlet.jsp.PageContext pageContext = null; HttpSession session = null; ServletContext application = null; ServletConfig config = null; JspWriter out = null; Object page = this; JspWriter _ jspx_out = null; try {_ jspxFactory = JspFactory.getDefaultFactory (); response.setContentType ("text/html;charset=ISO-8859-1") PageContext = _ jspxFactory.getPageContext (this, request, response,null, true, 8192, true); application = pageContext.getServletContext (); config = pageContext.getServletConfig (); session = pageContext.getSession (); out = pageContext.getOut (); _ jspx_out = out; String message = "Hello World!"; out.print (message);} catch (Throwable t) {out = _ jspx_out If (out! = null & & out.getBufferSize ()! = 0) out.clearBuffer (); if (pageContext! = null) pageContext.handlePageException (t);} finally {if (_ jspxFactory! = null) _ jspxFactory.releasePageContext (pageContext);}

As you can see from the above, HelloWorld.jsp first parses into a Java class HelloWorld_jsp.java at runtime, which inherits from the org.apache.jasper.runtime.HttpJspBase base class, and HttpJspBase implements the HttpServlet interface. It can be seen that the JSP application will first be compiled into a Servlet before running, which is the key to understanding JSP technology.

We also know that there are several built-in objects in JSP pages, such as pageContext, application, config, page, session, out, etc., and you may wonder why these built-in objects can be used directly in code snippets in JSP. Look at the _ jspService () method, where these built-in objects are actually defined. Initialize these built-in objects before parsing the code snippets in the JSP file.

First, call the getDefaultFactory () method of JspFactory to get a reference to a JspFactory object of the container implementation (Tomcat 4.1.17 in this article). JspFactory is an abstract class defined in the javax.servlet.jsp package, where two static methods set/getDefaultFactory () are defined. The set method is placed by the JSP container (Tomcat) when it instantiates the page's Servlet (that is, the HelloWorld_jsp class), so you can call the JspFactory.getDefaultFactory () method directly to get the implementation class of the JSP factory. Tomcat invokes the org.apache.jasper.runtime.JspFactoryImpl class.

Then, call the getPageContext () method of the JspFactoryImpl, populate a PageContext return, and assign it to the built-in variable pageConext. Other built-in objects are obtained through this pageContext. The specific process is shown in the code above, and I will not repeat it here. The environment of the page Servlet is set up, and the page is parsed. The HelloWorld.jsp page simply defines a String variable and then outputs it directly. The parsed code is as follows:

Code snippet parsed on the code listing 3:JSP page

String message = "Hello World!"; out.print (message); these are all the contents of the article "what's the difference between JSP and Servlet". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report