gpt4 book ai didi

java - 一旦请求被分派(dispatch)到 JSP,后端会发生什么

转载 作者:行者123 更新时间:2023-11-28 22:54:26 26 4
gpt4 key购买 nike

我正在努力提高网络应用程序的性能。现在,Web 表单在浏览器上加载大约需要 3-5 秒,但有时会从 3-20 秒不等。一旦请求被分派(dispatch)到特定的 JSP,我想分割组件所花费的时间。这样我就可以分析它的网络表单问题或服务器配置问题,并相应地采取纠正措施。

我的理解是 JSP 在服务器上首次访问时被编译成类文件并用于后续请求。

当请求发送到 JSP 时,服务器端会发生什么?是否有任何有助于提高性能的硬件配置指南?

最佳答案

请求发送到 JSP 后后端会发生什么?

A JSP page services requests as a servlet. Thus, the life cycle and many of the capabilities of JSP pages (in particular the dynamic aspects) are determined by Java Servlet technology.

When a request is mapped to a JSP page, the web container first checks whether the JSP page’s servlet is older than the JSP page. If the servlet is older, the web container translates the JSP page into a servlet class and compiles the class. During development, one of the advantages of JSP pages over servlets is that the build process is performed automatically.

TRANSLATION AND COMPILATION

During the translation phase each type of data in a JSP page is treated differently. Static data is transformed into code that will emit the data into the response stream. JSP elements are treated as follows:

  • Directives are used to control how the web container translates and executes the JSP page.
  • Scripting elements are inserted into the JSP page’s servlet class. Expression language expressions are passed as parameters to calls to the JSP expression evaluator.
  • jsp:[set|get]Property elements are converted into method calls to JavaBeans components.
  • jsp:[include|forward] elements are converted into invocations of the Java Servlet API.
  • The jsp:plugin element is converted into browser-specific markup for activating an applet.
  • Custom tags are converted into calls to the tag handler that implements the custom tag. In the Application Server, the source for the servlet created from a JSP page named pageName is in this file: domain-dir/generated/jsp/j2ee-modules/WAR-NAME/pageName_jsp.java.

For example, the source for the index page (named index.jsp) for the date localization example discussed at the beginning of the chapter would be named:

domain-dir/generated/jsp/j2ee-modules/date/index_jsp.java

Both the translation and the compilation phases can yield errors that are observed only when the page is requested for the first time. If an error is encountered during either phase, the server will return JasperException and a message that includes the name of the JSP page and the line where the error occurred.

After the page has been translated and compiled, the JSP page’s servlet (for the most part) follows the servlet life cycle described in Servlet Life Cycle:

1. If an instance of the JSP page’s servlet does not exist, the container:
a. Loads the JSP page’s servlet class
b. Instantiates an instance of the servlet class
c. Initializes the servlet instance by calling the jspInit method
2. The container invokes the `_jspService` method, passing request and response objects. If the container needs to remove the JSP page’s

servlet, it calls the jspDestroy method.

EXECUTION

You can control various JSP page execution parameters by using page directives. The directives that pertain to buffering output and handling errors are discussed here. Other directives are covered in the context of specific page-authoring tasks throughout the chapter.

BUFFERING

When a JSP page is executed, output written to the response object is automatically buffered. You can set the size of the buffer using the following page directive:

<%@ page buffer="none|xxxkb" %>

A larger buffer allows more content to be written before anything is actually sent back to the client, thus providing the JSP page with more time to set appropriate status codes and headers or to forward to another web resource. A smaller buffer decreases server memory load and allows the client to start receiving data more quickly.

HANDLING JSP PAGE ERRORS

Any number of exceptions can arise when a JSP page is executed. To specify that the web container should forward control to an error page if an exception occurs, include the following page directive at the beginning of your JSP page:

<%@ page errorPage="file-name" %>

阅读more .

是否有任何有助于提高性能的硬件配置指南?

购买更多内存和更昂贵的芯片,使用SSD硬盘,CDN(内容分发网络)等。

您不会询问如何通过编程方式调整性能。虽然,我认为这个link对你有帮助。

关于java - 一旦请求被分派(dispatch)到 JSP,后端会发生什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31351929/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com