studentsvlt.java

来自「采用基于B/S结构进行开发学生管理信息系统,本系统采用Servlet+Jsp+J」· Java 代码 · 共 182 行

JAVA
182
字号
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import myBean.Student;

public class StudentSvlt extends HttpServlet{
	
public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
    
    String stu_id =req.getParameter("id");
    int success = 0;
    String action = req.getParameter("action");
    Student stu = null;
    String message="";
    String name,password,classid;
   	
   if ("new".equalsIgnoreCase(action)) {
   	  stu_id=req.getParameter("id");
   	  name=new String(req.getParameter("name").getBytes("ISO8859_1"));
      password= req.getParameter("password");
      classid = req.getParameter("classid");
   	  
   	  if(isTrue(req,res,stu_id,name,password,classid) && hasLogin(req,res,stu_id)){
         stu = doNew(req,res,stu_id,password,name,classid);      
         sendBean(req, res, stu, "/getStudent.jsp");
        }
    }  
    
    if ("update".equalsIgnoreCase(action)) {    	
      name=new String(req.getParameter("name").getBytes("ISO8859_1"));
      password= req.getParameter("password");
      classid = req.getParameter("classid");
      
      if(isTrue(req,res,stu_id,name,password,classid)){
    	   try{		
     	       stu = doUpdate(req,res,stu_id,password,name,classid);
     	       sendBean(req,res,stu,"/getStudent.jsp");
            }
     	   catch(SQLException e){} 
     	  }
    }
			   
    if ("delete".equalsIgnoreCase(action)) {
    	try{			
      	success = doDelete(stu_id);	
      	    }
      	    catch(SQLException e){}				
    	if (success != 1) {
    		doError(req, res, "StudentSvlt: Delete unsuccessful. Rows affected: " + success);
    	} else {
    		res.sendRedirect("http://localhost:8080/0903/getStudent.jsp");
    	}
   
    }
    }
    

 public Student doNew(HttpServletRequest req,HttpServletResponse res,String stu_id,String password,String name,String classid )
                           throws ServletException,IOException{
      Student stu= new Student(); 
      String dep=req.getParameter("dep");
      String sex = new String(req.getParameter("sex").getBytes("ISO8859_1"));
      String jiguan = new String(req.getParameter("jiguan").getBytes("ISO8859_1"));
      String age = req.getParameter("age");  
      String year = req.getParameter("year");                   	
      
      stu.setAge(Integer.parseInt(age));
      stu.setEntry(Integer.parseInt(year));
      stu.setClassid(classid);
      stu.setId(stu_id);
      stu.setName(name);
      stu.setPassword(password);
      stu.setDep(dep);
      stu.setSex(sex);
      stu.setJiguan(jiguan);
      stu.addStudent(); 
      return stu;                	           	
 	}

 public Student doUpdate(HttpServletRequest req,HttpServletResponse res , String id,String password,String name,String classid)
                           throws ServletException,IOException,SQLException { 
                           	                     	
    Student stu = new Student();    
    String age = req.getParameter("age");             
    String dep = req.getParameter("dep");
    String sex = new String(req.getParameter("sex").getBytes("ISO8859_1"));
    String jiguan = new String(req.getParameter("jiguan").getBytes("ISO8859_1"));
    String year = req.getParameter("year");
    
    stu.setAge(Integer.parseInt(age));
    stu.setEntry(Integer.parseInt(year));
    stu.setClassid(classid);
    stu.setId(id);
    stu.setName(name);
    stu.setPassword(password);
    stu.setDep(dep);
    stu.setSex(sex);
    stu.setJiguan(jiguan);
    stu.updateStudent();
  
		return stu;
  }

  public int doDelete(String id) throws SQLException {
  	int num=0;
    Student stu=new Student();
    num=stu.deleteStudent(id);
    return num;
  }
	
public void sendBean(HttpServletRequest req, HttpServletResponse res,
                       Student stu, String target)
                       throws ServletException, IOException {
    req.setAttribute("stu", stu);
    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="对不起,该学生号已经被注册过了!";
  	Student stu= new Student();
  	f= stu.hasLogin(id);
  	if(f==false){
  	doError(req,res,message);	
  		}
  		return f;
  	}
  
  public boolean isTrue(HttpServletRequest req, HttpServletResponse res,
                        String id,String name,String password,String classid)
                        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(password==null || password.equals(""))  {
   	f=false;
   	message="密码不能为空,请重新填写!";
   	doError(req,res,message);	}  
   else if(classid==null || classid.equals("")) {
   	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 + =
减小字号Ctrl + -
显示快捷键?