版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhang_09_11/article/details/83243215
AServlet的代码:
public class AServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String userString=request.getParameter("username");
String userpassString =request.getParameter("password");
//System.out.println(userString+userpassString);为看输入的登录名和密码
if("admin".equals(userString)&&"12345".equals(userpassString)){
// if(!"admin".equalsIgnoreCase(userString)){
/**
* 使用Cookie来保存用户名,发送给客户端浏览器
* 当再次打开login.jsp会读取Cookie把它显示到用户名文本输入框
*
*/
Cookie cookie1=new Cookie("userString", userString);//创建cookie
cookie1.setMaxAge(60*60*24);//设置cookie的生存时间一天
response.addCookie(cookie1);//保存cookie
/**
* 重定向必须要加项目名
* 请求转发可以不加项目名你
* 请求成功之后跳转到success1.jsp
*/
HttpSession session=request.getSession();//获取session
session.setAttribute("userString", userString);//向session域中保存用户名
response.sendRedirect("/Demo11Session/loginpage/success1.jsp");
}else {
//response.sendRedirect("/Demo11Session/loginpage/error.jsp");
/**
* 如果错误则进行信息提示及转发到登陆页面
* 同时保存错误的的信息到request域中
*/
request.setAttribute("msg", "登录错误");
RequestDispatcher rp=request.getRequestDispatcher("/loginpage/login.jsp");//重定向到登录页面被
rp.forward(request, response);
}
}
}
VerifyServlet的代码:
login.jsp页面代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>欢迎登录!</h1>
<%/*读取名为userString的cookie*/
String username="";
Cookie[] csCookie=request.getCookies();//获取请求中所有cookie
if(csCookie!=null){//如果存在cookie
for(Cookie c:csCookie){//循环遍历所有的cookie
if("username".equals(c.getName())){//在所有cookie中寻找名为username
username=c.getValue();//获取这个cookie的值给username
}
}
} %>
<font color="red"><b>
<%
String msg=(String)request.getAttribute("msg"); //获取错误信息
if(msg!=null){
out.print(msg);
}
else{%>
<%
out.print("");
}
%>
</b>
</font>
<form action="/Demo11Session/AServlet" method="post">
<%--把用户名保存到用户名文本框 --%>
用户名:<input type="text" name="username" value="<%=username%>"/><br/>
密 码:<input type="password" name="password"/><br/>
验证码:<input type="text" name="verifyCode"/>
<img alt="验证码" src="/Demo11Session/VerifyServlet"/><!-- img标签的地址要项目名 -->
<input type="submit" value="提交"/>
</form>
</body>
</html>
登录结果的代码:
package com.myservlet.shape;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class VerifyServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=uft-8");
/**
* 生成图片
* 保存图片的文本到session文件中
* 把图片响应给客服端
*/
VerifyCode vCode=new VerifyCode();//此处引用了生成图片验证码的VerifyCode.java
BufferedImage image=vCode.getImage();
request.getSession().setAttribute("session_vcode", vCode.getText());//将图片上的文本获取到session域中
VerifyCode.output(image, response.getOutputStream());
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'success1.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>登录1</h1>
<% String username=(String) session.getAttribute("userString");
if(username==null){
/*向request域中保存错误信息,转发到登陆页面login.jsp*/
request.setAttribute("msg", "您还没有登录");
request.getRequestDispatcher("/loginpage/login.jsp").forward(request, response);
return;/*不执行后面代码,你需要跳出它,可以直接用return而不能加任何量在后面了*/
}%>
热烈欢迎<font><b><%out.print(session.getAttribute("userString")); %></b></font>
感谢您的一路相伴
</body>
</html>
测试结果
:
过期时间为一天