studentloginsvlt.java

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

JAVA
121
字号
import java.io.*;
import myBeans.SQLBean;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.util.regex.*;//正则表达式

public class StudentLoginSvlt extends HttpServlet{
	
public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
    	
    String pw1=null;
    String pw2=null;
    String  action = req.getParameter("action");
    ResultSet rs=null;      

	if ("update".equalsIgnoreCase(action)) {

	   String stu_id =req.getParameter("id");
     pw1=req.getParameter("pw1");
     pw2=req.getParameter("pw2");
     if(pw1.equals("") || pw2.equals("") || pw1==null || pw2==null)
         doError(req,res,"密码不能为空!");
     else if(doUpdate(req,res,pw1,pw2,stu_id)!=0)
         res.sendRedirect("/0903/student.jsp");
   }	
  if("add".equalsIgnoreCase(action)){
  	String cour_id = req.getParameter("cour_id");
  	if(doAdd(req,res,cour_id)!=0)
  	   res.sendRedirect("/0903/admin_Main.jsp");
  }

 }
    
  	 public int doUpdate(HttpServletRequest req, HttpServletResponse res,
  	 String pw1,String pw2,String id)
  	 throws ServletException, IOException{
  	 	int num=0;
  	 	if(!pw1.equals(pw2))
  	 	   doError(req,res,"密码不一致,请重输!");
  	 	else{
  	 	   SQLBean db = new SQLBean();
  	 	   String sql = "update student set PASSWORD='"+pw1+"' where ID='"+id+"'";
  	 	   num = db.executeInsert(sql);
  	     if(num==0) doError(req,res,"更新失败");
  	    }
  	    return num;
  	 	}
  	 	
  	 	public int doAdd(HttpServletRequest req,HttpServletResponse res, String cour_id)
  	 	          throws ServletException,IOException{
  	 	  int num = 0;String sql = "";SQLBean db = new SQLBean();
  	 	  Pattern p = Pattern.compile("[0-9]*");
  	 	  Enumeration paramNames = req.getParameterNames();
  	 	  while(paramNames.hasMoreElements()){num=-1;
  	 	     String paramName = (String)paramNames.nextElement();
  	 	     if(paramName.length()==12){
  	 	        String[] paramValues = req.getParameterValues(paramName);
  	 	        String paramValue = "";
  	 	        if(paramValues.length==1){
  	 	          paramValue = paramValues[0];
  	 	        }else{
  	 	          paramValue = "0";
  	 	        }
  	 	        Matcher m = p.matcher(paramValue);
  	 	        boolean b = m.matches();
  	 	        if(b){
  	 	        	sql = "select * from stu_course where STUID='"+paramName+"' and COURSEID='"+cour_id+"'";
  	 	        	ResultSet rs = db.executeQuery(sql);
  	 	        	try{
  	 	        	if(!rs.next()){
  	 	        	   sql = "insert into stu_course values('"+paramName+"','"+cour_id+"',"+Integer.parseInt(paramValue)+")"; 
  	 	             num = db.executeInsert(sql); 
  	 	            }else{} 
  	 	          rs.close();
  	 	          }catch(Exception e){e.getMessage();}
  	 	        }else{
  	 	          num = 0;
  	 	          doError(req,res,"错误,成绩只能由数字构成!");
  	 	          break;
  	 	         }
  	 	    }
  	 	  }
  	 		return num;
  	 	}
    
      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 void sendResultSet(HttpServletRequest req, HttpServletResponse res,
                      java.sql.ResultSet rs, String target)
                       throws ServletException, IOException {              	
    req.setAttribute("rs", rs);
    RequestDispatcher rd = getServletContext().getRequestDispatcher(target);
    rd.forward(req, res);
  }
  
      public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {

    doGet(req, res);

  } 
  

                       
    	
    	}
    	
    	
    	

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?