首页>代码>jdbc+mysql+servlet+jsp实现用户注册与登录功能>/Test/src/com/java/jsp/servlet/UserLoginServlet.java
package com.java.jsp.servlet;

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

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

import com.java.japdao.UserDao;
import com.java.japdao.Impl.UserDaoImpl;
import com.java.jsp.User;

public class UserLoginServlet extends HttpServlet {

	private static final long serialVersionUID = -205347041340147008L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		User user = new User();
		// 获取login.jsp页面提交的账号和密码
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		// 测试数据
		System.out.println(name + " " + password);
		// 获取login.jsp页面提交的账号和密码设置到实体类User中
		user.setName(name);
		user.setPassword(password);

		// 引入数据交互层
		UserDao dao = new UserDaoImpl();
		User us;
		try {
			us = dao.login(user);
			// 测试返回的值
			System.out.println(us);
			if (us.getName().equals(name) && us.getPassword().equals(password)) {
				response.sendRedirect("loginsuccess.jsp");
			}
			/*
			 * if (us != null) { response.sendRedirect("loginsuccess.jsp");
			 * request.setAttribute("info", "登陆成功"); }
			 */ else {
				response.sendRedirect("loginfailed.jsp");
				/* request.setAttribute("info", "登录失败"); */
			}

			/*
			 * request.getRequestDispatcher("/index/info.jsp").forward(request,
			 * response);
			 */
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

}
最近下载更多
asddwh  LV12 1月3日
CCCCWWWW  LV4 2023年12月8日
jiemomo  LV12 2023年10月19日
hongdongdong  LV12 2023年9月3日
慵懒的小橘猫  LV11 2022年12月24日
000666  LV10 2022年12月24日
9798665  LV3 2022年10月20日
微信网友_5822776429744128  LV6 2022年5月28日
zhaolei111  LV2 2022年5月15日
微信网友_5926336788320256  LV6 2022年5月13日
最近浏览更多
tartaglia  LV2 4月16日
GJQ123  LV4 2023年12月29日
asddwh  LV12 2023年12月25日
微信网友_6791236687548416  LV1 2023年12月23日
微信网友_6786215447367680  LV4 2023年12月23日
ling7314  LV4 2023年12月12日
yyidaaa  LV1 2023年12月9日
CCCCWWWW  LV4 2023年12月8日
ATOMBOMMM  LV1 2023年12月3日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友