package com.itheima.web.servlet.session;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
/**
* @program:
* @description
* @author: YunLong
**/
@WebServlet(urlPatterns = "/LoginServlet")
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
// 处理响应中文乱码
response.setContentType("text/html;charset=utf-8");
String vc = request.getParameter("verifyCode");
String code_session = (String) request.getSession().getAttribute("code_session");
if(code_session.equalsIgnoreCase(vc)){
//response.getWriter().write("验证码校验成功!");
String username = request.getParameter("username");
String password = request.getParameter("password");
boolean flag=false;
BufferedReader br = new BufferedReader(new FileReader("d:/stu.txt"));
String line;
while((line=br.readLine())!=null){
System.out.println(line);
String uname = line.split("\,")[0];
System.out.println(uname);
String pword = line.split("\,")[1];
System.out.println(pword);
if(username.equals(uname)&&pword.equals(password)){
flag=true;
}
}
if(flag){
request.getSession().setAttribute("username",username);
response.sendRedirect(request.getContextPath()+"/success.html");
}
else{
response.sendRedirect(request.getContextPath()+"/fail.html");
}
}else{
response.getWriter().write("验证码校验失败!");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
// 处理请求的中文乱码
request.setCharacterEncoding("utf-8");
doGet(request, response);
}
}
CheckcodeServlet(生成验证码)
package com.itheima.web.servlet.session;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/checkcodeServlet")
public class CheckcodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 创建画布
int width = 120;
int height = 40;
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 获得画笔
Graphics g = bufferedImage.getGraphics();
// 填充背景颜色
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
// 绘制边框
g.setColor(Color.red);
g.drawRect(0, 0, width - 1, height - 1);
// 生成随机字符
// 准备数据
String data = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
// 准备随机对象
Random r = new Random();
// 声明一个变量 保存验证码
String code = "";
// 书写4个随机字符
for (int i = 0; i < 4; i++) {
// 设置字体
g.setFont(new Font("宋体", Font.BOLD, 28));
// 设置随机颜色
g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
String str = data.charAt(r.nextInt(data.length())) + "";
g.drawString(str, 10 + i * 28, 30);
// 将新的字符 保存到验证码中
code = code + str;
}
// 绘制干扰线
for (int i = 0; i < 6; i++) {
// 设置随机颜色
g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));
}
// 将验证码 打印到控制台
System.out.println(code);
// 将验证码放到session中
request.getSession().setAttribute("code_session", code);
// 将画布显示在浏览器中
ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
ShowPriServlet(用来校验用户是否是在登陆的时候进入的success.html界面中,并且查看已有的权限)
package com.itheima.web.servlet.session;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @program:
* @description
* @author: YunLong
**/
@WebServlet(urlPatterns = "/ShowPriServlet")
public class ShowPriServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
// 处理响应中文乱码
response.setContentType("text/html;charset=utf-8");
String username = (String)request.getSession().getAttribute("username");
if(username!=null){
request.getRequestDispatcher("/WEB-INF/pri.html").forward(request,response);
}else{
response.getWriter().write("你还未登陆,无法查看已有权限,请先登录 ");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
// 处理请求的中文乱码
request.setCharacterEncoding("utf-8");
doGet(request, response);
}
}
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户登录</title>
<script>
window.onload=function () {
document.querySelectorAll("img")[0].onclick=function () {
this.src="/day24_cookie/checkcodeServlet?time="+new Date().getTime();
}
}
</script>
</head>
<body>
<form action="/day24_cookie/LoginServlet" method="get">
用户名:<input type="text" name="username"><br/>
密 码:<input type="password" name="password"><br/>
验证码:<input type="text" name="verifyCode"> <!--验证码一般是一个图片-->
<img src="/day24_cookie/checkcodeServlet">
<br/>
<input type="submit" value="登录">
</form>
</body>
</html>
success.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录成功页面</title>
</head>
<body>
<h2 style="color: red">恭喜您,登陆成功!</h2>
<a href="/day24_cookie/ShowPriServlet">查看我的权限</a>
</body>
</html>
fail.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>失败提示页</title>
</head>
<body>
<h2 style="color:red;">用户名或密码错误</h2>
<a href="login.html">重新登陆</a>
</body>
</html>
查看已有权限功能没有实现。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)