您的位置:首页 > Web前端 > JavaScript

JSP内置对象综合实例:学生成绩排序

2017-07-12 11:22 363 查看
学生成绩存放在txt文件中,每行一个记录,每条信息用空格分开。

txt文件存放在webcontent目录下。







首先定义学生基本信息类:Student,按照学号排序:NOComparator,按照总成绩排序:GradeComparator,以及制表类。使用<%! %>定义类。

<%@page import="org.dom4j.util.NodeComparator"%>
<%@page import="java.util.Collections"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.FileReader"%>
<%@page import="java.util.Vector"%>
<%@page import="java.util.Comparator"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%!
class Student{
String no;
String name;
int chinese;
int math;
int english;

Student (String n,String na,int c,int m,int e){
no=n;
name=na;
chinese=c;
math=m;
english=e;
}
}
class NOComparator implements Comparator{  //按照学号排序
public int compare(Object o,Object o2){
Student one=(Student)o;
Student two=(Student)o2;
return one.no.compareTo(two.no);
}
}
class GradeComparator implements Comparator{   //按照成绩排序
public int compare(Object o,Object o2){
Student one= (Student)o;
Student two=(Student)o2;
int total=one.chinese+one.math+one.english;
int total1=two.chinese+two.math+two.english;
return total-total1;
}
}
String fillTable(Vector v){
String s="<table border=1>";
s+="<tr><th>学号</th><th>姓名</th><th>语文</th>"+"<th>数学</th><th>外语</th><th>总成绩</th>"+"</tr>";
for(int i=0;i<v.size();i++){
Student st=(Student)v.get(i);
int grade=st.math+st.chinese+st.english;
s+="<tr>"+
"<td>"+st.no+"</td>"+"<td>"+st.name+"</td>"+"<td>"+st.chinese+"</td>"+"<td>"+st.math+"</td>"+"<td>"+st.english+"</td>"+"<td>"+grade+"</td>"+"</tr>";
}
s+="</table>";
return s;
}
%>
<%
Vector vec=new Vector();
//读文件
String strLine;
String path=application.getRealPath("/message.txt");
FileReader in=new FileReader(path);
BufferedReader in2=new BufferedReader(in);
while((strLine=in2.readLine())!=null){
String u[]=strLine.split(" ");
Student st=new Student(u[0],u[1],Integer.parseInt(u[2]),Integer.parseInt(u[3]),Integer.parseInt(u[4]));
vec.add(st);

}
in.close();
in2.close();

request.setCharacterEncoding("UTF-8"); //
String type=request.getParameter("sorttype");
if(type==null || type.equals("按学号升序")){
Collections.sort(vec,new NOComparator());
}else{
Collections.sort(vec,new GradeComparator());
}
%>

<body>
<%= fillTable(vec) %>
<form method="post">
<input type="submit" name="sorttype" value="按学号升序"/>  
<input type="submit" name="sorttype" value="按总成绩升序"/>  
</form>

</body>
</html>


先通过内置命令application.getrealpath()获得文本文档的绝对路径,然后读取文件,拆分后形成学生对象,存放到vector向量中。

String path=application.getRealPath("/message.txt");


FileReader in=new FileReader(path);
BufferedReader in2=new BufferedReader(in);


while((strLine=in2.readLine())!=null){
String u[]=strLine.split(" ");
Student st=new Student(u[0],u[1],Integer.parseInt(u[2]),Integer.parseInt(u[3]),Integer.parseInt(u[4]));
vec.add(st);

}


**

Java String.split()用法小结

**

在java.lang包中有String.split()方法,返回是一个数组

1、如果用“.”作为分隔的话,必须是如下写法,String.split(“\.”),这样才能正确的分隔开,不能用String.split(“.”);

2、如果用“|”作为分隔的话,必须是如下写法,String.split(“\|”),这样才能正确的分隔开,不能用String.split(“|”);

“.”和“|”都是转义字符,必须得加”\”;

3、如果在一个字符串中有多个分隔符,可以用“|”作为连字符,比如,“acount=? and uu =? or n=?”,把三个都分隔出来,可以用String.split(“and|or”);

**

编码中的setCharacterEncoding 理解

**

1、pageEncoding=”UTF-8”的作用是设置JSP编译成Servlet时使用的编码。

2、contentType=”text/html;charset=UTF-8”的作用是指定对服务器响应进行重新编码的编码。

3、request.setCharacterEncoding(“UTF-8”)的作用是设置对客户端请求进行重新编码的编码。

4、response.setCharacterEncoding(“UTF-8”)的作用是指定对服务器响应进行重新编码的编码。

response.setCharacterEncoding(“UTF-8”)的作用是指定对服务器响应进行重新编码的编码。同时,浏览器也是根据这个参数来对其接收到的数据进行重新编码(或者称为解码)。所以在无论你在JSP中设置response.setCharacterEncoding (“UTF-8”)或者response.setCharacterEncoding(“GBK”),浏览器均能正确显示中文(前提是你发送到浏览器的数据编码是正确的,比如正确设置了pageEncoding参数等)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jsp