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

📄 enrolsvlt.java

📁 一个学生课绩管理系统
💻 JAVA
字号:
package control;

import java.io.*;
import java.sql.*;
import database.sqlBean;
import javax.servlet.*;
import javax.servlet.http.*;

public class EnrolSvlt extends HttpServlet {

	public void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {

		String message = null;
		String id = null;
		String realname = null;
		String password = null;
		id = req.getParameter("id");
		realname = req.getParameter("realname");
		password = req.getParameter("password");
		HttpSession session = req.getSession(true);
		session.setAttribute("id", String.valueOf(id));

		String kind = null;
		kind = req.getParameter("kind");
		boolean temp = checkId(req, res, id, kind);
		if (temp == true) {
			message = "用户名已经存在!";
			doError(req, res, message);

		} else {
			message = "注册成功!";
			String sql = "insert into" + kind + "(id,name,password)"
					+ "value('" + id + "','" + realname + "','" + password
					+ "')";
			sqlBean db = new sqlBean();
			db.executeUpdate(sql);
//			RequestDispatcher rd = getServletContext().getRequestDispatcher(
//					"/login.jsp");
//			rd.forward(req, res);
			
			req.setAttribute("problem", message);
			RequestDispatcher rd = getServletContext().getRequestDispatcher(
					"/login.jsp");
			rd.forward(req, res);
		}

	}

	public boolean checkId(HttpServletRequest req, HttpServletResponse res,
			String id, String kind) throws ServletException, IOException {
		sqlBean db = new sqlBean();
		String pw = "";
		String sql = "select id from " + kind + " where id='" + id + "'";
		try {
			ResultSet rs = db.executeQuery(sql);
			if (rs.next()) {
				return true;
			}
		} catch (Exception e) {
			System.out.print(e.toString());
		}
		return false;
	}

	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 doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		String action = action = req.getParameter("action");
		if ("logout".equalsIgnoreCase(action)) {
			HttpSession session = req.getSession(true);
			session.invalidate();
			RequestDispatcher rd = getServletContext().getRequestDispatcher(
					"/login.jsp");
			rd.forward(req, res);
		}
	}

}

⌨️ 快捷键说明

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