📄 coursesvlt.java
字号:
package com.course;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CourseSvlt extends HttpServlet{
//响应get请求
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
//获得课程ID
String cour_id =req.getParameter("id");
int success = 0;
//获取请求中的参数
String action = action = req.getParameter("action");
course cour = null;
String message="";
//如果请求中包含新建课程参数
if ("new".equalsIgnoreCase(action)) {
//调用新建课程方法
cour = doNew(req,res);
//并将新建课程存储在request上下文中,然后转向getcourse.jsp页面
sendBean(req, res, cour, "/getcourse.jsp");
}
//如果请求中包含更新课程参数
if ("update".equalsIgnoreCase(action)) {
try{
// 调用更新课程方法
cour = doUpdate(req,res, cour_id);
//并将更新后的课程存储在request上下文中,然后转向getcourse.jsp页面
sendBean(req,res,cour,"/getcourse.jsp");
}
catch(SQLException e){}
}
//如果请求中包含删除课程参数
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/test/getcourse.jsp");
}}
}
//新建课程方法定义
public course doNew(HttpServletRequest req,HttpServletResponse res )
throws ServletException,IOException{
course cour= new course();
String cour_id=req.getParameter("id");
int mark;
String name=new String(req.getParameter("name").getBytes("ISO8859_1"));
try{
mark= Integer.parseInt(req.getParameter("mark"));
}catch(NumberFormatException e){mark=0;}
String dep=new String (req.getParameter("dep").getBytes("ISO8859_1"));
String prepare =req.getParameter("prepare");
if(isTrue(req,res,cour_id,name) && hasLogin(req,res,cour_id) && isCompare(prepare,dep,req,res)){
cour.setId(cour_id);
cour.setName(name);
cour.setDep(dep);
cour.setPrepare(prepare);
cour.setMark(mark);
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;
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)
throws ServletException,IOException,SQLException {
course cour = new course();
String name=new String(req.getParameter("name").getBytes("ISO8859_1"));
int mark = Integer.parseInt(req.getParameter("mark"));
String dep = req.getParameter("dep");
String prepare = req.getParameter("prepare");
if(isTrue(req,res,id,name) && isCompare(prepare,dep,req,res)){
cour.setName(name);
cour.setMark(mark);
cour.setDep(dep);
cour.setPrepare(prepare);
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;
}
//保存课程信息的JavaBean到request上下文中
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)
throws ServletException, IOException {
boolean f=true;
String message ="";
if(id==null || id.equals("")) {
f=false;
message="错误,课程号不能为空!";
doError(req,res,message); }
if(name==null || name.equals("")) {
f=false;
message="课程名不能为空,请重新填写!";
doError(req,res,message); }
return f;
}
//响应post请求
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doGet(req, res);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -