loginservlet.java

来自「我实习时候的设计」· Java 代码 · 共 78 行

JAVA
78
字号
package cn.yxh.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.pzb.db.DB;
import cn.yxh.dao.LoginDAO;

public class LoginServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("GBK");
		
		String usertype = request.getParameter("usertype");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
	
			boolean b = LoginDAO.checkLogin(username, password,usertype);
			System.out.println(b);
			if(b) {
				if(usertype.equals("3")) {
					request.getSession().setAttribute("username", username);
					request.getRequestDispatcher("main.jsp").forward(request, response);
				}
				else if(usertype.equals("1")) {
					request.getSession().setAttribute("usertype", usertype);
					request.getSession().setAttribute("studentid", username);
					request.getRequestDispatcher("StudentViewServlet").forward(request, response);
				}
				else {
					String teachername = new DB().getTeacherName(username);
					request.getSession().setAttribute("usertype", usertype);
					request.getSession().setAttribute("teachername", teachername);
					request.getSession().setAttribute("teacherid", username);
					request.getRequestDispatcher("scoreList.jsp").forward(request, response);
				}
			}
			else{
				request.setAttribute("error", "错误的用户名或密码");
				request.getRequestDispatcher("login.jsp").forward(request, response);
			}
	}

}

⌨️ 快捷键说明

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