ОБНОВЛЕНО: я включил весь код для устранения неоднозначности.
Это класс Compile, который я использую для методов получения и установки:
package user;
public class CompileClass {
public String date1;
public String date2;
public String p_code;
public CompileClass(){
}
public void setDate1( String name ) {
date1 = name;
}
public void setDate2( String name ) {
date2 = name;
}
public void setP_code( String name ) {
p_code = name;
}
public String getDate1() {
return date1;
}
public String getDate2() {
return date2;
}
public String getP_code() {
return p_code;
}
}
А это мой BEAN duplicaterecords.jsp:
package user;
import java.io.* ;
import java.sql.*;
import java.text.*;
import javax.servlet.*;//modified for JSP
import javax.servlet.http.*;//modified for JSP
import user.CompileClass;
/*to find duplicate records and their time stamps*/
public class duplicaterecords extends HttpServlet{//modified for JSP
public static void main(String[] args,HttpServletRequest request, HttpServletResponse response)//modified for JSP
{
int l,x=0,y=0,tow,i=0,tower1=0,t=0;
String p_code,date[],date1,date2,getdate,date3,tower,t_split;
String time2;
//tow=new int[1000];
date=new String[100];
CompileClass c=new CompileClass();//modified for JSP
//HttpServletRequest request;//modified for JSP
DecimalFormat df = new DecimalFormat("#.##");
try
{
BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); //input buffer
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement stmt=con.createStatement();
String query1="select distinct(date) FROM `report_data` WHERE date>= ? and date<= ? "; //Query one for date input
PreparedStatement ps=con.prepareStatement(query1);
//System.out.println("Enter the 1st DATE"); //Date 1 is entered
date1 = c.getDate1();//modified for JSP
//System.out.println("Enter the 2nd DATE"); //Date 2 is entered
//date2=b.readLine();
date2 = c.getDate2();//modified for JSP
ps.setString(1,date1);
ps.setString(2,date2);
//System.out.println("enter the param_code"); // param_code is entered
//p_code= b.readLine();
p_code=c.getP_code();//modified for JSP
ResultSet result=ps.executeQuery();
//System.out.print("Tow_id");
while(result.next() )
{
getdate=result.getString("date");
//System.out.print("\t"+getdate);
request.setAttribute("dates", getdate);//modified for JSP
date3='%'+getdate+'%';
date[x]=date3;
x++;
}
l=x;
String query2="SELECT distinct(tow_id) FROM `tower_data` WHERE TIME_STAMP LIKE ? "; //query 2 for finding tower-id
PreparedStatement ps1=con.prepareStatement(query2);
ps1.setString(1,date[0]);
ResultSet result1=ps1.executeQuery();
while(result1.next())
{
//System.out.println("");
tower=result1.getString("tow_id");
tower1= Integer.parseInt(tower);
t=y;
//System.out.print(tower1);
request.setAttribute("towers", tower1);//modified for JSP
int count=0;
x=0;
while(count<l)
{
String query3="SELECT time_stamp FROM tower_data WHERE `TIME_STAMP` LIKE ? AND `PARAM_CODE` = ? AND `TOW_ID`=? GROUP BY time_stamp HAVING count( * ) >1";
//Query 3 for finding time stamps with duplicate data
PreparedStatement ps2=con.prepareStatement(query3);
ps2.setString(2,p_code);
ps2.setString(1,date[x]);
ps2.setInt(3,tower1);
ResultSet result2=ps2.executeQuery();
int row=0;
while(result2.next())
{
t_split=result2.getString("time_stamp");
String[] parts= t_split.split(" "); //splitting time_stamp to extract only time without date
time2=parts[1]; //time stored in time2
//System.out.println("\t"+time2);
request.setAttribute("times", time2);//modified for JSP
row++;
}
if(row==0)
{
//System.out.println("\t"+"no duplicate");
}
// System.out.print("\t"+"\t");
x++;
count++;
}
}
con.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Это мой Result.jsp :
<%@ page import="java.net.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="java.util.ArrayList"%>
<jsp:useBean id="user" scope="session" class="user.duplicaterecords" />
<jsp:setProperty property="*" name="user"/>
<html>
<body>
Dates:<BR>
<%--<%= request.getAttribute("dates") %><br/>--%>
<%--Email: <%= user.getMail() %><BR>--%>
<%-- Age: <%= user.getAge() %><BR> --%>
<c:forEach items="${sessionScope.dates}" var="item">
<c:out value="${item}"/>
</c:forEach>
</body>
</html>
Как ясно изкод
s.setAttribute("dates", getdate);
getdate будет содержать массив строковых значений.
Теперь я хочу вызвать егов моем JSP вот так:
<%= request.getAttribute("dates") %>
Но вызов значения вроде возвращает null .Поэтому я хотел узнать, как получить доступ к атрибуту getdate в виде массива и распечатать все значения.Помощь