login.java

来自「刚刚做的EL和JSTL结合的前台」· Java 代码 · 共 82 行

JAVA
82
字号
package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

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

import beans.UserInf;

import database.DBConn;

public class Login 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 {

		response.setContentType("text/html;charset=gb2312");
		PrintWriter out = response.getWriter();
		String name = request.getParameter("name").trim();
		String pwd = request.getParameter("pwd").trim();
		UserInf u = new DBConn().login(name, pwd);
	
			if (u==null) {
				out.print("用户名不存在,请注册");
				response.setHeader("refresh","2;URL=reg.jsp");
			}
			else
				{
					String quanxian = u.getQuanxian();
					HttpSession session = request.getSession();
					if(quanxian.equals("1"))
					{
						session.setAttribute("admin", u);
						request.getRequestDispatcher("index.jsp").forward(request, response);
					}
					else if(quanxian.equals("2"))
					{
						session.setAttribute("admin", u);
						
						request.getRequestDispatcher("index.jsp").forward(request, response);
					}
					
			
				}
		
	}
	

}

⌨️ 快捷键说明

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