主要Servlet
public class lowLogin extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
File f = new File("low.html");
File f2 = new File("submit.html");
RespReturn.returnResp(resp,f);
RespReturn.returnResp(resp,"<br><br>周一:");
for(String type:RespReturn.teseMap.get("周一")){
int count = getFromSQL.getCount("mon",type);
RespReturn.returnResp(resp,"<br><br> ");
RespReturn.returnResp(resp,"<input type=\"radio\" name=\"interest1\" value=\""+type+"\" checked/>"+type+",剩下名额"+(50-count)+",设定人数50人");
}
RespReturn.returnResp(resp,"<br><br>周二:");
for(String type:RespReturn.teseMap.get("周一")){
int count = getFromSQL.getCount("tue",type);
RespReturn.returnResp(resp,"<br><br> ");
RespReturn.returnResp(resp,"<input type=\"radio\" name=\"interest2\" value=\""+type+"\" checked/>"+type+",剩下名额"+(50-count)+",设定人数50人");
}
RespReturn.returnResp(resp,"<br><br>周三:");
for(String type:RespReturn.teseMap.get("周一")){
int count = getFromSQL.getCount("wed",type);
RespReturn.returnResp(resp,"<br><br> ");
RespReturn.returnResp(resp,"<input type=\"radio\" name=\"interest3\" value=\""+type+"\" checked/>"+type+",剩下名额"+(50-count)+",设定人数50人");
}
RespReturn.returnResp(resp,f2);
}
}
@WebServlet("/lowbegin")
public class lowLoginbegin extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
Map<String, String[]> map = req.getParameterMap();
Student ss = new Student();
try {
BeanUtils.populate(ss,map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
if(!checkCopy(ss)){
req.setAttribute("err","err01");
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/fail");
requestDispatcher.forward(req,resp);
}else{
if(!checknumber(ss)){
req.setAttribute("err","err02");
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/fail");
requestDispatcher.forward(req,resp);
}else{
req.setAttribute("student",ss);
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/success");
requestDispatcher.forward(req,resp);
}
}
}
public boolean checkCopy(Student ss){
if(ss.getInterest4()==null&&ss.getInterest5()==null){
if(ss.getInterest1().equals(ss.getInterest2())||ss.getInterest2().equals(ss.getInterest3())||ss.getInterest1().equals(ss.getInterest3())){
return false;
}
return true;
}else{
if(ss.getInterest4().equals(ss.getInterest5())){
return false;
}
return true;
}
}
public boolean checknumber(Student ss){
if(ss.getInterest4()==null&&ss.getInterest5()==null){
int count01 = getFromSQL.getCount("mon",ss.getInterest1());
if(count01>=50){
return false;
}
int count02 = getFromSQL.getCount("tue",ss.getInterest2());
if(count01>=50){
return false;
}
int count03 = getFromSQL.getCount("wed",ss.getInterest3());
if(count01>=50){
return false;
}
return true;
}else{
int count04 = getFromSQL.getCount("thu",ss.getInterest4());
if(count04>=50){
return false;
}
int count05 = getFromSQL.getCount("fri",ss.getInterest5());
if(count05>=50){
return false;
}
return true;
}
}
}
@WebServlet("/fail")
public class FailLogin extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("faildopost....");
resp.setContentType("text/html;charset=utf-8");
String str =(String) req.getAttribute("err");
if(str.equals("err01")){
resp.getWriter().write("您选取的特色课存在重复,请重新选择");
}else{
resp.getWriter().write("您选取的特色课人数已满,请重新选择");
}
}
}
@WebServlet("/success")
public class SuccessLogin extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
Student ss =(Student) req.getAttribute("student");
if(getFromSQL.getInclude(ss.getName(),ss.getGrade())==1){
JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
String sql = "update student set mon=?,tue=?,wed=?,thu=?,fri=? where name = ? and grade=?";
int count = template.update(sql,ss.getInterest1(),ss.getInterest2(),ss.getInterest3(),ss.getInterest4(),ss.getInterest5(),ss.getName(),ss.getGrade());
System.out.println(count);
resp.getWriter().write("恭喜您,"+ss.getName()+"已经完成选课");
}else{
JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
String sql = "insert into tese(name,grade,gender,phone,mon,tue,wed,thu,fri) values(?,?,?,?,?,?,?,?,?)";
int count = template.update(sql,ss.getName(),ss.getGrade(),ss.getGender(),ss.getPhoneNumber(),ss.getInterest1(),ss.getInterest2(),ss.getInterest3(),ss.getInterest4(),ss.getInterest5());
System.out.println(count);
resp.getWriter().write("恭喜您,"+ss.getName()+"已经完成选课");
}
}
}
2.基本实现,通过在线选课,直接保存至数据库中