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

📄 servletnote.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;

/**
 * Servlet implementation class for Servlet: Servletnote
 * 
 */
public class Servletnote 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("id")).intValue();
		} catch (NumberFormatException e) {
		}
		NoteBean notes = null;
		// String message = "";
		String sort = null;
		try {
			sort = session.getAttribute("sort").toString();
		} catch (NullPointerException e) {
		}
		if (sort.equals("admin")) {
			if ("new".equalsIgnoreCase(action)) {
				notes = doNew(req, res);

				sendBean(req, res, notes, "/getnote.jsp");
			}

			if ("update".equalsIgnoreCase(action)) {
				try {
					notes = doUpdate(req, res, id);

					sendBean(req, res, notes, "/getnote.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("getnote.jsp");
				}
			}
		} else
			doError(req, res, "你无权操作,或你还没有登陆!");
	}

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

		NoteBean notes = new NoteBean();
		String title = new String(req.getParameter("title").getBytes(
				"ISO8859_1"));
		String content = new String(req.getParameter("content").getBytes(
				"ISO8859_1"));
		String time = new String(req.getParameter("time").getBytes("ISO8859_1"));
		notes.setTime(time);
		notes.setContent(content);
		notes.setTitle(title);
		notes.addNote();
		return notes;
	}

	public NoteBean doUpdate(HttpServletRequest req, HttpServletResponse res,
			int id) throws ServletException, IOException, SQLException {
		NoteBean notes = new NoteBean();

		String title = new String(req.getParameter("title").getBytes(
				"ISO8859_1"));
		String content = new String(req.getParameter("content").getBytes(
				"ISO8859_1"));
		if (isTrue(req, res, title, content)) {
			notes.setId(id);
			notes.setTitle(title);
			notes.setContent(content);
			notes.updateNote();
		}
		return notes;
	}

	public int doDelete(int id) throws SQLException {
		int num = 0;
		NoteBean notes = new NoteBean();
		num = notes.deleteNote(id);
		return num;
	}

	public void sendBean(HttpServletRequest req, HttpServletResponse res,
			NoteBean notes, String target) throws ServletException, IOException {
		req.setAttribute("notes", notes);
		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 title, String content) throws ServletException, IOException {
		boolean f = true;
		String message = "";
		if (title == null || title.equals("")) {
			f = false;
			message = "主题不能为空,请重新填写!";
			doError(req, res, message);
		}

		if (content == null || content.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 + -