⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 servletsubject.java

📁 在线教育平台: 从小学
💻 JAVA
字号:
package edu;

import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Servletsubject extends javax.servlet.http.HttpServlet implements
		javax.servlet.Servlet {
	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		int id = 0;
		HttpSession session = req.getSession(true);
		int success = 0;
		String action = null;
		action = req.getParameter("action");
		try {
			id = Integer.valueOf(req.getParameter("subject_id")).intValue();
		} catch (NumberFormatException e) {
		}
		SubjectBean subjects = null;
		String sort = null;
		try {
			sort = session.getAttribute("sort").toString();
		} catch (NullPointerException e) {
		}
		if (sort.equals("admin")) {

			if ("new".equalsIgnoreCase(action)) {
				subjects = doNew(req, res);

				sendBean(req, res, subjects, "/addclass.jsp");
			}

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

					subjects = doUpdate(req, res, id);

					sendBean(req, res, subjects, "/adminsubject.jsp");
				} catch (SQLException e) {
				}
			}

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

				try {
					success = doDelete(id);
				} catch (SQLException e) {
				}

				if (success != 1) {
					doError(req, res, "删除失败. Rows affected: " + success);
				} else {
					res.sendRedirect("/adminsubject.jsp");
				}
			}
		} else
			doError(req, res, "你无权操作,或你还没有登陆!");
	}

	public SubjectBean doNew(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {

		SubjectBean subjects = new SubjectBean();
		String subject = new String(req.getParameter("subject").getBytes(
				"ISO8859_1"));

		int grade_id = 0, class_id = 0;
		try {
			class_id = Integer.valueOf(req.getParameter("class_id")).intValue();
			grade_id = Integer.valueOf(req.getParameter("grade_id")).intValue();
		} catch (NumberFormatException e) {
		}
		subjects.setClass_id(class_id);
		subjects.setGrade_id(grade_id);
		subjects.setSubject(subject);
		subjects.addSubject();
		return subjects;
	}

	public SubjectBean doUpdate(HttpServletRequest req,
			HttpServletResponse res, int id) throws ServletException,
			IOException, SQLException {
		SubjectBean subjects = new SubjectBean();
		String subject = new String(req.getParameter("subject").getBytes(
				"ISO8859_1"));
		int grade_id = 0, class_id = 0;
		try {
			class_id = Integer.valueOf(req.getParameter("class_id")).intValue();
			grade_id = Integer.valueOf(req.getParameter("grade_id")).intValue();
		} catch (NumberFormatException e) {
		}

		if (isTrue(req, res, subject)) {
			subjects.setId(id);
			subjects.setGrade_id(grade_id);
			subjects.setClass_id(class_id);
			subjects.setSubject(subject);
			subjects.updateSubject(id);
		}
		return subjects;
	}

	public int doDelete(int id) throws SQLException {
		int num = 0;
		SubjectBean subjects = new SubjectBean();
		num = subjects.deleteSubject(id);
		return num;
	}

	public void sendBean(HttpServletRequest req, HttpServletResponse res,
			SubjectBean subjects, String target) throws ServletException,
			IOException {
		req.setAttribute("subjects", subjects);
		try {
			RequestDispatcher rd = req.getRequestDispatcher(target);
			rd.forward(req, res);
		} catch (Throwable t) {
		}
	}

	public void doError(HttpServletRequest req, HttpServletResponse res,
			String str) throws ServletException, IOException {
		req.setAttribute("problem", str);
		try {
			RequestDispatcher rd = req
					.getRequestDispatcher("/errorpage_ad.jsp");
			rd.forward(req, res);
		} catch (Throwable t) {
		}
	}

	public boolean isTrue(HttpServletRequest req, HttpServletResponse res,
			String subject) throws ServletException, IOException {
		boolean f = true;
		String message = "";
		if (subject == null || subject.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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -