我需要:
以下是此特定功能的页面。我在这个项目中有很多不同的java页面,servlet和jsp页面,所以如果你需要那些东西,那就请问。
文本文件按以下顺序包含Student
信息:
学生证,姓氏,名字,测验分数1-5,化妆测验分数, 中期分数,问题分数和期末考试分数。
以及文本文件格式的示例如下:
1234,一些,人,98.0,94.0,97.0,96.0,99.0,0.0,92.0,95.0,99.0 1235,另一个,人,89.0,99.0,87.0,85.0,88.0,0.0,89.0,98.0,99.0 1236,Onemore,人,89.0,98.0,89.0,98.0,97.0,0.0,79.0,89.0,99.0
然而,我在表格中得到的输出只是第一行,而它全部在第一个单元格中。
如果你能指出我正确的方向,我就完全迷失了。
这是我的Student
课程页面:
public class Student {
private String sid,fnm,lnm,lgrade;
private double q1,q2,q3,q4,q5,qmkup,midt,probs,finex,qavg,cavg;
public Student (){
//empty constructor
sid="";
lnm="";
fnm="";
lgrade="";
q1= 0;
q2= 0;
q3= 0;
q4= 0;
q5= 0;
qmkup= 0;
midt= 0;
probs= 0;
finex= 0;
qavg= 0;
cavg= 0;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getFnm() {
return fnm;
}
public void setFnm(String fnm) {
this.fnm = fnm;
}
public String getLnm() {
return lnm;
}
public void setLnm(String lnm) {
this.lnm = lnm;
}
public String getLgrade() {
return lgrade;
}
public double getQ1() {
return q1;
}
public void setQ1(double q1) {
this.q1 = q1;
}
public double getQ2() {
return q2;
}
public void setQ2(double q2) {
this.q2 = q2;
}
public double getQ3() {
return q3;
}
public void setQ3(double q3) {
this.q3 = q3;
}
public double getQ4() {
return q4;
}
public void setQ4(double q4) {
this.q4 = q4;
}
public double getQ5() {
return q5;
}
public void setQ5(double q5) {
this.q5 = q5;
}
public double getQmkup() {
return qmkup;
}
public void setQmkup(double qmkup) {
this.qmkup = qmkup;
}
public double getMidt() {
return midt;
}
public void setMidt(double midt) {
this.midt = midt;
}
public double getProbs() {
return probs;
}
public void setProbs(double probs) {
this.probs = probs;
}
public double getFinex() {
return finex;
}
public void setFinex(double finex) {
this.finex = finex;
}
public double getQavg() {
calcStudent();
return qavg;
}
public double getCavg() {
return cavg;
}
private void calcStudent(){
double[] qs = { q1, q2, q3, q4, q5, qmkup};
Arrays.sort(qs);
qavg = (qs[2] + qs[3] + qs [4] + qs[5]) / 4.0;
if (qavg >= 89.5 && midt >= 89.5 && probs >= 89.5){
cavg = (qavg+midt+probs) / 3.0;
lgrade = "A";
}else{
cavg = (qavg * .5) + (midt * .15) + (probs * .1) + (finex * .25);
if (cavg >= 89.5){
lgrade = "A";
}else if (cavg >= 79.5){
lgrade = "B";
}else if (cavg >= 69.5){
lgrade = "C";
}else if (cavg >= 59.5){
lgrade = "D";
}else{
lgrade = "F";
}
}
}
@Override
public String toString(){
String s = sid + "," + lnm + "," + fnm + "," +
q1 + "," + q2 + "," + q3 + "," + q4 + "," + q5 + "," +
qmkup + "," + midt + "," + probs + "," + finex;
return s;
}
这是我的java页面:
public class StudentIO {
public static void addStudent(Student s, String path)
throws IOException{
File f = new File(path);
PrintWriter out = new PrintWriter(new FileWriter(f,true));
out.println(s.toString());
out.close();
}
public static ArrayList<Student> getStudentList(String path)
throws IOException {
BufferedReader in = new BufferedReader
(new FileReader(path));
ArrayList<Student> sList = new ArrayList<Student>();
String line = in.readLine();
String data[];
while (line != null){
data = line.split(",");
Student s = new Student();
s.setSid(data[0]);
s.setLnm(data[1]);
s.setFnm(data[2]);
s.setQ1(Double.parseDouble(data[3]));
s.setQ2(Double.parseDouble(data[4]));
s.setQ3(Double.parseDouble(data[5]));
s.setQ4(Double.parseDouble(data[6]));
s.setQ5(Double.parseDouble(data[7]));
s.setQmkup(Double.parseDouble(data[8]));
s.setMidt(Double.parseDouble(data[9]));
s.setProbs(Double.parseDouble(data[10]));
s.setFinex(Double.parseDouble(data[11]));
s.getSid();
s.getLnm();
s.getFnm();
s.getQ1();
s.getQ2();
s.getQ3();
s.getQ4();
s.getQ5();
s.getQmkup();
s.getMidt();
s.getProbs();
s.getFinex();
sList.addAll(Arrays.asList(s));
line = in.readLine();
}
in.close();
return sList;
}
这是我的servlet:
public class ClassListServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String emsg = "";
String path;
String URL = "/ClassList.jsp";
ArrayList<Student> stulist;
try{
path = getServletContext().getRealPath("/WEB-INF/classlist.txt");
stulist = StudentIO.getStudentList(path);
request.setAttribute("stulist", stulist);
}catch (Exception e){
emsg = "Process error: " +e.getMessage();
URL = "/students.jsp";
}
RequestDispatcher disp = getServletContext().getRequestDispatcher(URL);
disp.forward(request, response);
}
这是JSP页面:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Class List</title>
</head>
<body>
<h1>Students on file:</h1>
<table>
<tr>
<th colspan="12"><br>Class List</th>
</tr>
<tr>
<th colspan="1">Student ID</th>
<th colspan="1">Last Name</th>
<th colspan="1">First Name</th>
<th colspan="1">Quiz 1</th>
<th colspan="1">Quiz 2</th>
<th colspan="1">Quiz 3</th>
<th colspan="1">Quiz 4</th>
<th colspan="1">Quiz 5</th>
<th colspan="1">Make-Up Quiz</th>
<th colspan="1">Midterm</th>
<th colspan="1">Problems</th>
<th colspan="1">Final</th>
</tr>
<%
ArrayList<Student> studt = (ArrayList<Student>)
request.getAttribute("stulist");
for (int i = 0; i < studt.size(); i++) {
%>
<tr>
<td><%= studt.get(i) %></td>
</tr>
<% } %>
</table>
<%
//cells and titles for each output field
//every time through loop will process each individual
//student object instead of string objects
%>
</body>
答案 0 :(得分:2)
问题出在getStudentList()
方法中,您不需要每次都创建新的ArrayList
。
sList = new ArrayList<Student>();
必须移出while循环
答案 1 :(得分:0)
您正在ArrayList
上制作getStudentList()
的实例
只需从该方法sList = new ArrayList<Student>();
答案 2 :(得分:0)
当您在StudentIO.java中填充sList时,sList是否包含文件中的所有行?尝试将while循环更改为:
while ( ( line = in.readLine( ) ) != null ) {
此外,您将Student对象转换为字符串,然后将其发送到JSP。请传递完整的List对象,然后打印它。此外,您需要colspan属性:
<tr>
<td><%= studt.get(i) %></td>
</tr>