📄 coursesvlt.java
字号:
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import myBean.Course;
import java.util.regex.*;
public class CourseSvlt extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String cour_id =req.getParameter("id");
int success = 0,period;
String action = req.getParameter("action");
Course cour = null;
String message="",name,dep,prepare;
Pattern p = Pattern.compile("[0-9]*");
if ("new".equalsIgnoreCase(action)) {
cour_id=req.getParameter("id");
prepare =req.getParameter("prepare");
name=new String(req.getParameter("name").getBytes("ISO8859_1"));
dep=new String (req.getParameter("dep").getBytes("ISO8859_1"));
Matcher m = p.matcher(req.getParameter("period"));
boolean b = m.matches();
if(b){
period = Integer.parseInt(req.getParameter("period"));
if(isTrue(req,res,cour_id,name,period) && hasLogin(req,res,cour_id) && isCompare(prepare,dep,req,res)){
cour = doNew(req,res,cour_id,name,period,prepare,dep);
sendBean(req, res, cour, "/getCourse.jsp");
}
}else{doError(req,res,"学时只能为正整数!");}
}
if ("update".equalsIgnoreCase(action)) {
name=new String(req.getParameter("name").getBytes("ISO8859_1"));
dep=new String (req.getParameter("dep").getBytes("ISO8859_1"));
prepare =req.getParameter("prepare");
Matcher m = p.matcher(req.getParameter("period"));
boolean b = m.matches();
if(b){
period = Integer.parseInt(req.getParameter("period"));
if(isTrue(req,res,cour_id,name,period) && isCompare(prepare,dep,req,res)){
try{
cour = doUpdate(req,res,cour_id,name,period,prepare,dep);
sendBean(req,res,cour,"/getCourse.jsp");
}
catch(SQLException e){}
}
}else{doError(req,res,"学时只能为正整数!");}
}
if ("delete".equalsIgnoreCase(action)) {
try{
success = doDelete(cour_id);
}
catch(SQLException e){}
if (success != 1) {
doError(req, res, "CourseSvlt: Delete unsuccessful. Rows affected: " + success);
} else {
res.sendRedirect("http://localhost:8080/0903/getCourse.jsp");
}}
if("updateScore".equalsIgnoreCase(action)){
String stu_id = req.getParameter("stu_id");
String grade = req.getParameter("mark");
Matcher m = p.matcher(grade);
boolean b = m.matches();
if(b){
cour = new Course();
cour.updateMark(stu_id,cour_id,grade);
sendBean(req,res,cour,"/getCourse.jsp");
}else{doError(req,res,"成绩只能为正整数!");}
}
}
public Course doNew(HttpServletRequest req,HttpServletResponse res,String cour_id,String name,int period,String prepare,String dep)
throws ServletException,IOException{
float credit = Float.valueOf(req.getParameter("mark")).floatValue();
String semester=new String(req.getParameter("semester").getBytes("ISO8859_1"));
Course cour= new Course();
cour.setId(cour_id);
cour.setName(name);
cour.setCredit(credit);
cour.setDep(dep);
cour.setPrepare(prepare);
cour.setPeriod(period);
cour.setSemester(semester);
cour.addCourse();
return cour;
}
public boolean isCompare(String prepare,String dep,
HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException{
boolean f=true;
String tempDep=null;
String message=null;
Course cour= new Course();
if( !prepare.equalsIgnoreCase("0")){
tempDep=cour.getPrepareDep(prepare);
if(tempDep.equals("public"))
return true;
else if(dep.equalsIgnoreCase(tempDep))
f=true;
else {
f=false;
message="错误,课程所在系与预修课所在系不一致!";
doError(req,res,message);
}
}
return f;
}
public Course doUpdate(HttpServletRequest req,HttpServletResponse res , String id,String name,int period,String prepare,String dep)
throws ServletException,IOException,SQLException {
float credit = Float.valueOf(req.getParameter("mark")).floatValue();
String semester=new String(req.getParameter("semester").getBytes("ISO8859_1"));
Course cour = new Course();
cour.setName(name);
cour.setCredit(credit);
cour.setDep(dep);
cour.setPrepare(prepare);
cour.setPeriod(period);
cour.setSemester(semester);
cour.updateCourse(id);
return cour;
}
public int doDelete(String id) throws SQLException {
int num=0;
Course cour=new Course();
num=cour.deleteCourse(id);
return num;
}
public void sendBean(HttpServletRequest req, HttpServletResponse res,
Course cour, String target)
throws ServletException, IOException {
req.setAttribute("cour", cour);
RequestDispatcher rd = getServletContext().getRequestDispatcher(target);
rd.forward(req, res);
}
public void doError(HttpServletRequest req,
HttpServletResponse res,
String str)
throws ServletException, IOException {
req.setAttribute("problem", str);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/errorpage.jsp");
rd.forward(req, res);
}
public boolean hasLogin(HttpServletRequest req, HttpServletResponse res,String id)
throws ServletException, IOException{
boolean f=true;
String message="对不起,该课程号已经被注册过了!";
Course cour= new Course();
f= cour.hasLogin(id);
if(f==false){
doError(req,res,message);
}
return f;
}
public boolean isTrue(HttpServletRequest req, HttpServletResponse res,
String id,String name,int period)
throws ServletException, IOException {
boolean f=true;
String message ="";
if(id==null || id.equals("")) {
f=false;
message="错误,课程号不能为空!";
doError(req,res,message); }
else if(name==null || name.equals("")) {
f=false;
message="课程名不能为空,请重新填写!";
doError(req,res,message); }
else if(period<=0)
{f=false;
message = "学时必须为正整数,请重新填写!";
doError(req,res,message);
}
return f;
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doGet(req, res);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -