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

How to analyze the working principle of JSP+JavaBean+Servlet structure

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to parse JSP+JavaBean+Servlet structure working principle, the article content quality is high, so Xiaobian share for everyone to make a reference, I hope you read this article after the relevant knowledge has a certain understanding.

JSP+JavaBean two-tier structure working principle should be more familiar, but also better understanding.

But one thing must be clear is that the user through the browser to send a web page request, this request arrived at the server after the server to find the corresponding web page, if it is the first request (the second time does not need to explain the execution), for JSP to generate Servlet, and then through the Servlet engine to execute Servlet, call JavaBean embedded in the results of the page returned to the user's browser.

JSP+JavaBean+Servlet three-tier structure is the essence of an additional Controller: Servlet to distribute client browser requests. It is helpful to understand Servlets if the role of Servlets acting as controllers is understood as pre-processing client requests. Through the web.xml configuration file, you can find the correspondence between user requests and specific Servlets. Each Servlet has a specific Servlet object corresponding to it, so what handles user requests is a Servlet object inherited from HttpServlet.

﹤!-- JSPC servlet mappings start --﹥ ﹤servlet﹥ ﹤servlet-name﹥ms1﹤/servlet-name﹥ ﹤servlet-class﹥news.FirstAction﹤/servlet-class﹥ ﹤/servlet﹥ ﹤servlet﹥ ﹤servlet-name﹥ms2﹤/servlet-name﹥ ﹤servlet-class﹥news.DetailAction﹤/servlet-class﹥ ﹤/servlet﹥﹤!-- JSPC servlet mappings end --﹥ ﹤servlet-mapping﹥ ﹤servlet-name﹥ms1﹤/servlet-name﹥ ﹤url-pattern﹥/newsmain﹤/url-pattern﹥ ﹤/servlet-mapping﹥ ﹤servlet-mapping﹥ ﹤servlet-name﹥ms2﹤/servlet-name﹥ ﹤url-pattern﹥/newsDetail﹤/url-pattern﹥ ﹤/servlet-mapping﹥

As shown above, a section of configuration servlet extracted from web.xml. The *** part is mainly used to configure the Servlet to associate with the specific Servlet object. The second part is mainly used to configure which Servlet the request is processed by. The Servlet name is associated, and the processing request is associated with the specific Servlet processing object. For example, the request sent by the client browser/newsmain is processed by ms1 servlet, and the corresponding serlet object news.FirstAction can be found through ms1, i.e./newsmain-> ms1-> news.FirstAction. This is what configuration files are all about. Now understand that the user/newsmain request will be processed by the news.FirstAction class object, so to understand the program you have to understand what the role of FirstAction is. For example, here is an implementation of FirstAction.

public final class FirstAction extends HttpServlet { protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { DB db = new DB(); HttpSession session = req.getSession(); try { session.setAttribute(Constants.NEWS_LIST_KEY, News .SearchNewsTitle(db)); } catch (Exception e) { e.printStackTrace(); } db.close(); String target = "/P43_News/newsMain.jsp"; resp.sendRedirect(target); } }

Through this implementation, you can see that when the server receives the client request to execute the News.SearchNewsTitle(db) operation, and then put the return value into the session through session.setAttribute, and then indirectly transfer to newsMain.jsp through resp.sendRedirect(target), so that in newsMain.jsp through the session.getAttribute function you can get the corresponding value stored in the session.

Looking back, it is easy to see that JSP+JavaBean works differently from JSP+JavaBean+Servlet. Two-tier structure must put preprocessing in JSP, such as News.SearchNewsTitle(db). Three-tier structure first carries out preprocessing in Servlet, and then returns the processing result to JSP through Session, so that JSP pays more attention to the display of interface.

How to parse JSP+JavaBean+Servlet structure on the working principle of sharing here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

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