.jsp中的if else与<%@include file = .html>交互

huangapple go评论78阅读模式
英文:

.jsp if else with <%@include file = .html> interaction

问题

- 更新后尝试的代码_02

<%@page contentType="text/html;charset=UTF-8" %>
<%@include file="/include/gds_include.jsp" %>

<jsp:useBean class="com.gds.DBconnect" id="dbHelper" scope="page" />

<%@include file="01_product_categories_detail.html"%>

<%

String id = request.getParameter("pid");

try {

dbHelper.setConnection(77);

dbHelper.setStatementIndex(0);
dbHelper.setPreparedStatement("select * from webpage where p_id = '"+id+"'");
dbHelper.exePreparedStatement();
resultSet = dbHelper.getSqlResultArray();

if(resultSet != null) {
    

    for(int i=0; i<resultSet.length; i++) {
        
        out.print("<tr><td>" );
        out.print("<h1>" + resultSet[i][2] + "</h1>"); 
        out.print(resultSet[i][6]);
        
    }
    

}

}
catch(Exception ex) {

out.print("錯誤訊息:" + ex);

}
finally {

dbHelper.doFinalize();

}

%>

<%
String id = request.getParameter("pid");

  if(!id.equals("en_1-01"))  {

%>
<%@include file="02_product_categories_detail.html"%>
<%
} else if (id.equals("en_1-02")) {
%>
<%@include file="microchip_product_categories_detail.html"%>
<%
}
%>


```

  • 错误消息
消息 无法编译 JSP 类:

描述 服务器遇到意外情况,导致无法满足请求。

异常

org.apache.jasper.JasperException: 无法编译 JSP 类: 

在 jsp 文件中的第 [58] 行出现错误: [/english/product_0207_sec.jsp]
重复的局部变量 id
55: 
56: 
57: <%
58:       String id = request.getParameter("pid");
59: 
60:       if(!id.equals("en_1-01"))  {
61: %>


堆栈跟踪:
	org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
	org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:213)
	org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:509)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:397)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:367)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:351)
	org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:603)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:399)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:380)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:328)
	jakarta.servlet.http.HttpServlet.service(HttpServlet.java:777)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
注 完整的根本原因堆栈跟踪可在服务器日志中获得。

try { } 部分之前运行良好,因此错误出现在 if else 语句中(我猜测)。


<details>
<summary>英文:</summary>

demo url:   
`http://192.168.0.222/english/product_0207_sec.jsp?pid=en_1-02`   
`http://192.168.0.222/english/product_0207_sec.jsp?pid=en_999`

inside .jsp file, the `&lt;try&gt;` part can extract different data in the same html page,  
  **how can I make the if else work with `&lt;%@include file = .html&gt;`**




-----

- tried .jsp if else present with different include file  (in line 55 to 62)

<%@page contentType="text/html;charset=UTF-8" %>
<%@include file="/include/gds_include.jsp" %>

<jsp:useBean class="com.gds.DBconnect" id="dbHelper" scope="page" />

<%@include file="01_product_categories_detail.html"%>

<%

String id = request.getParameter("pid");

try {

dbHelper.setConnection(77);

dbHelper.setStatementIndex(0);
dbHelper.setPreparedStatement(&quot;select * from webpage where p_id = &#39;&quot;+id+&quot;&#39;&quot;);
dbHelper.exePreparedStatement();
resultSet = dbHelper.getSqlResultArray();

if(resultSet != null) {
    

    for(int i=0; i&lt;resultSet.length; i++) {
        
        out.print(&quot;&lt;tr&gt;&lt;td&gt;&quot; );
        out.print(&quot;&lt;h1&gt;&quot; + resultSet[i][2] + &quot;&lt;/h1&gt;&quot;); 
        out.print( resultSet[i][6]);
        
    }
    

}

}
catch(Exception ex) {

out.print(&quot;error message:&quot; + ex);

}
finally {

dbHelper.doFinalize();

}

%>

</html>

<%@include file="02_product_categories_detail.html"%>

<%
String id = request.getParameter("pid");

  if(pid != &quot;en_1-01&quot;)
      out.println(&quot; &lt;%@include file=&quot;footer_A.html&quot;%&gt; &quot;);
  else if (pid == &quot;en_1-02&quot;)
      out.println(&quot; &lt;%@include file=&quot;footer_B.html&quot;%&gt; &quot;);

%>


so that this .jsp can deal with much more scenario, please give me a hand 

---

- updated tried code_02

<%@page contentType="text/html;charset=UTF-8" %>
<%@include file="/include/gds_include.jsp" %>

<jsp:useBean class="com.gds.DBconnect" id="dbHelper" scope="page" />

<%@include file="01_product_categories_detail.html"%>

<%

String id = request.getParameter("pid");

try {

dbHelper.setConnection(77);

dbHelper.setStatementIndex(0);
dbHelper.setPreparedStatement(&quot;select * from webpage where p_id = &#39;&quot;+id+&quot;&#39;&quot;);
dbHelper.exePreparedStatement();
resultSet = dbHelper.getSqlResultArray();

if(resultSet != null) {
    

    for(int i=0; i&lt;resultSet.length; i++) {
        
        out.print(&quot;&lt;tr&gt;&lt;td&gt;&quot; );
        out.print(&quot;&lt;h1&gt;&quot; + resultSet[i][2] + &quot;&lt;/h1&gt;&quot;); 
        out.print( resultSet[i][6]);
        
    }
    

}

}
catch(Exception ex) {

out.print(&quot;錯誤訊息:&quot; + ex);

}
finally {

dbHelper.doFinalize();

}

%>

<%
String id = request.getParameter("pid");

  if(!id.equals(&quot;en_1-01&quot;))  {

%>
<%@include file="02_product_categories_detail.html"%>
<%
} else if (id.equals("en_1-02")) {
%>
<%@include file="microchip_product_categories_detail.html"%>
<%
}
%>

</html>


- error message

Message Unable to compile class for JSP:

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: [58] in the jsp file: [/english/product_0207_sec.jsp]
Duplicate local variable id
55:
56:
57: <%
58: String id = request.getParameter("pid");
59:
60: if(!id.equals("en_1-01")) {
61: %>

Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:213)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:509)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:397)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:367)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:351)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:603)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:399)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:380)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:328)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:777)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.


the `try { }` part works fine before, so the error occur on the if else statement (I guess)

</details>


# 答案1
**得分**: 1

尝试这个:

更新:

(1)移除 String id = request.getParameter("pid");

(2)字符串比较必须使用 equals,而不是 ==

&lt;%
      if(!id.equals("en_1-01"))  {
%&gt;
    &lt;%@include file="footer_A.html"%&gt;
&lt;%
    }   else if (id.equals("en_1-02")) {
%&gt;      
     &lt;%@include file="footer_B.html"%&gt;
&lt;%
    }
%&gt;

<details>
<summary>英文:</summary>

Try this:

UPDATE:

(1) REMOVE `String id = request.getParameter(&quot;pid&quot;);`

(2) String comparisons must use `equals`, not `==`.

<%
if(!id.equals("en_1-01")) {
%>
<%@include file="footer_A.html"%>
<%
} else if (id.equals("en_1-02")) {
%>
<%@include file="footer_B.html"%>
<%
}
%>



</details>



huangapple
  • 本文由 发表于 2023年2月8日 14:45:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定