修改LoginServlet.java
package com.atguigu.login.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.atguigu.login.beans.User;
import com.atguigu.login.dao.UserDao;
import com.atguigu.login.dao.UserDaoImpl;
/*
* 处理登陆请求的Servlet
*
* Servlet是sun公司制定的标准,Tomcat(web应用服务器、Servlet容器)实现了这些标准
*
* HttpServlet:
*/
public class LoginServlet extends HttpServlet{
/*
* 常用方法:doGet doPost service
* doGet:处理客户端的get方式的请求
* doPost:处理客户端的post方式的请求
*
* service:根据具体的请求方式去调用对应的doGet、doPost方法
*/
// @Override
// protected void service(HttpServletRequest req, HttpServletResponse resp)
// throws ServletException, IOException {
// // TODO Auto-generated method stub
// super.service(req, resp);
// }
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 登录功能实现
System.out.println("登录过来了");
//获取到 用户输入的用户和密码,进行登录业务的处理
/*
* HttpServletRequest: 请求对象。Servlet容器会在请求到达后,创建一个request对象,
* 将Http请求相关的信息全部都封装到该对象中。
*/
req.setCharacterEncoding("utf-8");
//获取用户名
String username = req.getParameter("username");
// 获取密码
String password = req.getParameter("password");
System.out.println(username + " , " + password);
//验证用户名与密码是否正确
// 通过响应对象 HttpServletResponse , 给客户端响应数据
//返回模式使用utf-8
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
//获取Dao对象
UserDao userDao = new UserDaoImpl() ;
User user = userDao.getUserByUsernameAndPassword(username, password) ;
if(user == null){
//登录失败
//通过重定向的方式去往登录页面
/**
* 服务器会给浏览器发送一个302状态码以及一个新的地址
*/
// resp.sendRedirect("login.jsp");
/*
* 转发:
* 转发之前,绑定数据,就是将想要交给下一个组件(JSP)处理的数据,绑定到request对象中
*/
req.setAttribute("login_msg", "用户名或者密码错误!!!") ;
// 获取转发器
RequestDispatcher rd = req.getRequestDispatcher("login.jsp") ;
//开始转发
rd.forward(req, resp);
} else {
//登录成功
out.println("<h1><font color='green'>Login Success!<font></h1>");
}
}
}
修改login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- 告诉页面打开方式是utf-8 防止乱码 -->
<meta charset="utf-8">
<style>
body{
background-color:pink;
}
span{
color:red ;
}
</style>
</head>
<body>
<h1>欢迎登陆</h1>
<!--
action:请求的地址
method:请求的方式
GET :将请求数据拼接到请求地址中,直接暴露出来。http://localhost:8080/webo1/login?username=admin&password=111111 不安全
POST :将请求数据存到请求体中提交,不会直接暴露出来
-->
<form action="login" method="post">
用户名称:<input type="text" name="username"/>
<%
// 获取到requeest对象:request对象可以直接用。
String loginMsg = (String)request.getAttribute("login_msg") ;
%>
<span><%=loginMsg==null?"":loginMsg %></span>
<br/>
用户密码:<input type="password" name="password"/>
<br/>
<input type="submit" value="login"/>
</form>
</body>
</html>