JSP新闻系统之八 Servlet

举报
tea_year 发表于 2021/12/29 23:21:08 2021/12/29
【摘要】 实用Servlet实现登陆验证 package com.news.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.Htt...

实用Servlet实现登陆验证

package com.news.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 javax.servlet.http.HttpSession;

import com.news.dao.UserDao;
import com.news.dao.impl.UserDaoImpl;
import com.news.entity.User;

public class LoginServlet extends HttpServlet {

	/**
	 * 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 {
		HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		
		String rand = (String)session.getAttribute("rand");
		   
	   	String username = request.getParameter("username");
	   	String userpwd = request.getParameter("userpwd");
	   	String srand = request.getParameter("rand");
	   	
	   	if(!rand.equals(srand)){
	   		out.println("<script>alert('验证码错误!!');location.href='login.jsp';</script>");
	   		return;
	   	}
	   	
	   	UserDao userDao = new UserDaoImpl();
	   	
	   	boolean flag = userDao.checkLogin(username,userpwd);
	   	
	   	if(flag){
	   		User user = new User();
	   		user.setUsername(username);
	   		session.setAttribute("user",user);
	   		response.sendRedirect("index.jsp");
	   	}
	   	else{
	   		response.sendRedirect("login.jsp");
	   	}
		
	}

}


文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。

原文链接:aaaedu.blog.csdn.net/article/details/53106146

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。