?? studentsvlt.java
字號(hào):
package com.course;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class StudentSvlt extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
//獲得學(xué)生ID
String stu_id =req.getParameter("id");
int success = 0;
// 獲取請(qǐng)求中的參數(shù)
String action = action = req.getParameter("action");
student stu = null;
String message="";
//如果請(qǐng)求中包含新建學(xué)生參數(shù)
if ("new".equalsIgnoreCase(action)) {
// 調(diào)用新建學(xué)生方法
stu = doNew(req,res);
// 并將新建學(xué)生存儲(chǔ)在request上下文中,然后轉(zhuǎn)向getStudent.jsp頁面
sendBean(req, res, stu, "/getStudent.jsp");
}
// 如果請(qǐng)求中包含修改學(xué)生參數(shù)
if ("update".equalsIgnoreCase(action)) {
try{
// 調(diào)用修改學(xué)生信息方法
stu = doUpdate(req,res, stu_id);
// 并將修改后的學(xué)生信息存儲(chǔ)在request上下文中,然后轉(zhuǎn)向getStudent.jsp頁面
sendBean(req,res,stu,"/getStudent.jsp");
}
catch(SQLException e){}
}
// 如果請(qǐng)求中包含刪除學(xué)生參數(shù)
if ("delete".equalsIgnoreCase(action)) {
try{
//調(diào)用刪除學(xué)生方法
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/test/getStudent.jsp");
}
}
}
//新建學(xué)生方法定義
public student doNew(HttpServletRequest req,HttpServletResponse res )
throws ServletException,IOException{
student stu= new student();
String stu_id=req.getParameter("id");
String name=new String(req.getParameter("name").getBytes("ISO8859_1"));
String password= req.getParameter("password");
String dep=new String (req.getParameter("dep").getBytes("ISO8859_1"));
String sex = new String(req.getParameter("sex").getBytes("ISO8859_1"));
String jiguan = new String(req.getParameter("jiguan").getBytes("ISO8859_1"));
if(isTrue(req,res,stu_id,name,password) && hasLogin(req,res,stu_id)){
stu.setId(stu_id);
stu.setName(name);
stu.setPassword(password);
stu.setDep(dep);
stu.setSex(sex);
stu.setJiguan(jiguan);
stu.addStudent(); }
return stu;
}
//更新學(xué)生信息方法
public student doUpdate(HttpServletRequest req,HttpServletResponse res , String id)
throws ServletException,IOException,SQLException {
student stu = new student();
String name=new String(req.getParameter("name").getBytes("ISO8859_1"));
String password = req.getParameter("password");
String dep = new String(req.getParameter("dep").getBytes("ISO8859_1"));
String sex = new String(req.getParameter("sex").getBytes("ISO8859_1"));
String jiguan = new String(req.getParameter("jiguan").getBytes("ISO8859_1"));
if(isTrue(req,res,id,name,password)){
stu.setId(id);
stu.setName(name);
stu.setPassword(password);
stu.setDep(dep);
stu.setSex(sex);
stu.setJiguan(jiguan);
stu.updateStudent();}
return stu;
}
//刪除學(xué)生方法
public int doDelete(String id) throws SQLException {
int num=0;
student stu=new student();
num=stu.deleteStudent(id);
return num;
}
//保存學(xué)生信息的JavaBean到request上下文中
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);
}
//錯(cuò)誤處理方法
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);
}
//判斷學(xué)生是否被注冊(cè)過
public boolean hasLogin(HttpServletRequest req, HttpServletResponse res,String id)
throws ServletException, IOException{
boolean f=true;
String message="對(duì)不起,該學(xué)生號(hào)已經(jīng)被注冊(cè)過了!";
student stu= new student();
f= stu.hasLogin(id);
if(f==false){
doError(req,res,message);
}
return f;
}
//判斷學(xué)生注冊(cè)是否成功
public boolean isTrue(HttpServletRequest req, HttpServletResponse res,
String id,String name,String password)
throws ServletException, IOException {
boolean f=true;
String message ="";
if(id==null || id.equals("")) {
f=false;
message="錯(cuò)誤,學(xué)生號(hào)不能為空!";
doError(req,res,message); }
if(name==null || name.equals("")) {
f=false;
message="學(xué)生姓名不能為空,請(qǐng)重新填寫!";
doError(req,res,message); }
if(password==null || password.equals("")) {
f=false;
message="密碼不能為空,請(qǐng)重新填寫!";
doError(req,res,message); }
return f;
}
//響應(yīng)post請(qǐng)求
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doGet(req, res);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -