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

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

在 .jsp 文件中,<try> 部分可以在同一个 html 页面中提取不同的数据,
如何使 if else 与 <%@include file = .html> 一起工作


  • 尝试使用 .jsp if else 展现不同的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("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("error message:" + ex);

}
finally {
    
    dbHelper.doFinalize();

}

%>

</html>

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


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

      if(pid != "en_1-01")
          out.println(" <%@include file="footer_A.html"%> ");
      else if (pid == "en_1-02")
          out.println(" <%@include file="footer_B.html"%> ");
%>

为了让这个 .jsp 可以处理更多的状况,请帮我一下


  • updated tried code

<%@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("error message:" + ex);

}
finally {
    
    dbHelper.doFinalize();

}

%>



<%
      if(pid == "en_1-01") {
%>
    <%@include file="02_product_categories_detail.html"%>

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

</html>
  • error message
Type Exception Report

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: [59] in the jsp file: [/english/product_0207_sec.jsp]
pid cannot be resolved to a variable
56: <%
57: 
58: 
59:       if(pid == "en_1-01") {
60: %>
61:     <%@include file="02_product_categories_detail.html"%>
62: 


An error occurred at line: [64] in the jsp file: [/english/product_0207_sec.jsp]
pid cannot be resolved to a variable
61:     <%@include file="02_product_categories_detail.html"%>
62: 
63: <%
64:     }   else if (pid == "en_1-02") {
65: %>      
66:      <%@include file="microchip_product_categories_detail.html"%>
67: <%


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.

try { } 部分在之前可以正常运行,所以错误发生在 if else 语句上(我猜)

你逻辑判断用的是pid ,但是在顶部定义的是 id接收的pid