【问题标题】:Converting JSP Pages to servlets将 JSP 页面转换为 servlet
【发布时间】:2019-02-23 09:54:13
【问题描述】:

我们在 JSP 页面中也有 HTML 标记,因此在执行 jsp 时,它会被转换为 Servlet。所以我的问题是 HTML 标签发生了什么,或者我们可以在 servlet 中的哪里找到 HTML 标签?

【问题讨论】:

  • 你可以使用out.println()
  • 如果我在运行页面时在 jsp 页面中创建了一些带有一些逻辑的标题,那么 html 标签会发生什么......我知道逻辑将被转换为 servlet 服务方法,但是 html 标签呢
  • 使用JSTL从servlet通过response传递变量,对象然后你就可以从JSP文件访问它。

标签: java html jsp servlets jsp-tags


【解决方案1】:

每个由“.jsp”标识并由JSP Engine转换为Servlet的JSP页面。一些例子是:-

文件:-dashboardApp.jsp

<!DOCTYPE html>
<html>
<head>
<title>Dashboard App</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="appResources/images/favicon.ico" type="image/x-icon" />

<!-- jquery library -->
<script type="text/javascript" src="appResources/jquery/jquery.min_3.3.1.js"></script>  
</head>
<body ng-app="dashboardApp">

<!-- ${userId}  ${userName} -->
<input type="hidden"  id="userId" name="userId" value="${userId}"  />
<input type="hidden"   id="userName" name="userName" value="${userName}" />
</body>
</html>   

文件:-dashboardApp_jsp.java(通过附加文件名+“_jsp.java”创建的相同文件)

.jsp 中的所有 html 代码都进入到 servlet 的服务方法中。以上 .jsp 文件的示例

public void _jspService(HttpServletRequest request, HttpServletResponse response)
    throws java.io.IOException, ServletException {
 JspWriter out = out = pageContext.getOut();

  out.write("<!DOCTYPE html>\r\n");
  out.write("<html>\r\n");
  out.write("<head>\r\n");
  out.write(" <title>Dashboard App</title>\r\n");
  out.write(" <meta charset=\"utf-8\">\r\n");
  out.write(" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\r\n");
  out.write(" <link rel=\"shortcut icon\" href=\"appResources/images/favicon.ico\" type=\"image/x-icon\" />\r\n");
  out.write(" \r\n");
  out.write("              <!-- jquery library -->\r\n");
  out.write("   <script type=\"text/javascript\" src=\"appResources/jquery/jquery.min_3.3.1.js\"></script>  \r\n");
  out.write("<body ng-app=\"dashboardApp\">\r\n");
  out.write("   <input type=\"hidden\"  id=\"userId\" name=\"userId\" value=\"");
  out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${userId}", java.lang.String.class, (PageContext)_jspx_page_context, null, false));
  out.write("\"  />\r\n");
  out.write("   <input type=\"hidden\"   id=\"userName\" name=\"userName\" value=\"");
  out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${userName}", java.lang.String.class, (PageContext)_jspx_page_context, null, false));
  out.write("\" />  \r\n");
  out.write("</body>\r\n");
  out.write("</html>");
  }

JSP 引擎将 .jsp 文件转换为 servlet (.java) 和 .class 文件。

在这里您可以找到 .class servlet .java (Tomcat 服务器)。 /DashboardApp/target/tomcat/work/localEngine/localhost/DashboardApp/org/apache/jsp/WEB_002dINF/view/dashboardApp_jsp.java

【讨论】:

  • 不错!它对我有帮助
猜你喜欢
  • 1970-01-01
  • 2011-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-04
  • 1970-01-01
相关资源
最近更新 更多