程序的原意是输出带两位小数的百分数
原代码:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,java.util.*,java.math.*;" errorPage="" %>
<html>
<head>
<title>邮箱运营</title>
<style type="text/css">
<!--
body {
background-color: #EEEEEE;
}
.style1 {
font-size: 14px;
font-weight: bold;
}
td {
font-size: 12px;
}
-->
</style></head>
<%! String url,user,password,sql,quhao,rongliang,tel,fangshi,shijian,qingkuang;%>
<%! Connection conn;%>
<%! ResultSet rs;%>
<%! Statement stmt;%>
<%! int hezec,hezecbatint;%>
<%! double hezecbat;%>
<%! FormatPercent(int, int);%>
<body>
  <%
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); 
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mail"; 
String user="sa"; 
String password="123456"; 
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
sql="select count(*) as hezecbatint from ruyi where quhao='0530' and tel>'13300000000' and fangshi='bat' and shijian like'2006-9%' ";
rs=stmt.executeQuery(sql);
rs.next();
hezecbatint=rs.getInt("hezecbatint");
sql="select count(*) as hezec from ruyi where quhao='0530' and tel>'13300000000' and shijian like'2006-9%' ";
rs=stmt.executeQuery(sql);
rs.next();
hezec=rs.getInt("hezec");
    %>
<%=hezecbatint%>.<%=hezec%>.<%=FormatPercent(hezecbatint/hezec,2)%>
<%
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
out.println(e.toString());
}
%>
</body>
</html>出错提示:
org.apache.jasper.JasperException: Unable to compile class for JSPAn error occurred at line: 25 in the jsp file: /333.jsp
Generated servlet error:
Return type for the method is missingAn error occurred at line: 25 in the jsp file: /333.jsp
Generated servlet error:
Syntax error on token "int", VariableDeclaratorId expected after this tokenAn error occurred at line: 25 in the jsp file: /333.jsp
Generated servlet error:
Syntax error on token "int", VariableDeclaratorId expected after this tokenAn error occurred at line: 44 in the jsp file: /333.jsp
Generated servlet error:
The method FormatPercent(int, int) is undefined for the type _333_jsp
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

解决方案 »

  1.   

    <%! String url,user,password,sql,quhao,rongliang,tel,fangshi,shijian,qingkuang;%>
    <%! Connection conn;%>
    <%! ResultSet rs;%>
    <%! Statement stmt;%>
    <%! int hezec,hezecbatint;%>
    <%! double hezecbat;%>
    <%! FormatPercent(int, int);%>这里是<%@ %>这样才对吧.
      

  2.   

    <%! String url,user,password,sql,quhao,rongliang,tel,fangshi,shijian,qingkuang;%>
    <%! Connection conn;%>
    <%! ResultSet rs;%>
    <%! Statement stmt;%>
    <%! int hezec,hezecbatint;%>
    <%! double hezecbat;%>
    <%! FormatPercent(int, int);%>这么做作
      

  3.   

    <% String url,user,password,sql,quhao,rongliang,tel,fangshi,shijian,qingkuang;
       Connection conn;
       ResultSet rs;
       Statement stmt;
       int hezec,hezecbatint;
       double hezecbat;
       FormatPercent(int, int);%>
    <body>
    <%
    try{
      

  4.   

    <%FormatPercent(int ,int );%>
    定义方法?
    --------------------------------------------
    注释是这样的
    <%//String url%>
    <%-- String url--%>
    --------------------------------------------
    <% String url;%>
    <%String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mail";%>
    你这两个地方重复了?---------------------------------------------
    以下通过:
    ---------------------------------------------------
    <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,java.util.*,java.math.*;" errorPage=""%>
    <html>
    <head>
    <title>邮箱运营</title>
    <style type="text/css">
    <!--
    body {
    background-color: #EEEEEE;
    }
    .style1 {
    font-size: 14px;
    font-weight: bold;
    }
    td {
    font-size: 12px;
    }
    -->
    </style>
    </head>
    <%String url, user, password, sql, quhao, rongliang, tel, fangshi, shijian, qingkuang;%>
    <%Connection conn;%>
    <%ResultSet rs;%>
    <%Statement stmt;%>
    <%int hezec, hezecbatint;%>
    <%double hezecbat;%>
    <%//FormatPercent(int ,int );%>
    <body>
    <%try {
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
    url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mail";
    user = "sa";
    password = "123456";
    conn = DriverManager.getConnection(url, user, password);
    stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    sql = "select count(*) as hezecbatint from ruyi where quhao='0530' and tel>'13300000000' and fangshi='bat' and shijian like'2006-9%' ";
    rs = stmt.executeQuery(sql);
    rs.next();
    hezecbatint = rs.getInt("hezecbatint");
    sql = "select count(*) as hezec from ruyi where quhao='0530' and tel>'13300000000' and shijian like'2006-9%' ";
    rs = stmt.executeQuery(sql);
    rs.next();
    hezec = rs.getInt("hezec"); %>
    <%=hezecbatint%>
    .
    <%=hezec%>
    .
    <%//=FormatPercent(hezecbatint/hezec,2)%>
    <%rs.close();
    stmt.close();
    conn.close();
    }
    catch (Exception e) {
    out.println(e.toString());
    } %>
    </body>
    </html>
      

  5.   

    这样声明方法:
    <%!int FormatPercent(int i, int j) {
    //return Integer.toString(i + j);
    return i + j;
    }%>
    -------------------------------------------------------------
    <%=FormatPercent(2, 3)%>