创建一个index.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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="login" method="post">
用户名:<input type="text" name="uid" /><br>
密码:<input type="password" name="pwd" /><br>
<input type="submit" value="登录">
</form>
</body>
</html>

创建一个main.jsp页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%--
if(session.getAttribute("user")==null){
response.sendRedirect("index.jsp");
}
--%>
<%
Cookie[] cc = request.getCookies();
boolean has = false;
for(Cookie c:cc){
if(c.getName().equals("user")){
has=true;
}
}
if(has==false){
response.sendRedirect("index.jsp");
}
%>
welcome:<%=session.getAttribute("user") %>
<% for(Cookie c:cc){
if(c.getName().equals("user")){
out.print(c.getValue());
}
}
%>
</body>
</html>

创建一个servlet界面,名为Login

package com.ceshi;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; /**
* Servlet implementation class Login
*/
@WebServlet("/login")
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public Login() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
String uid = request.getParameter("uid");
String pwd = request.getParameter("pwd");
//HttpSession session= request.getSession();
//session.setAttribute("user",uid);
Cookie c = new Cookie("user", uid);
response.addCookie(c);
response.sendRedirect("main.jsp");
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }

显示如下:

最新文章

  1. 最新做路径动画必备Simple Waypoint System5.1.1最新做路径动画必备Simple Waypoint System5.1.1
  2. leetcode 91 Decode Ways ----- java
  3. hadoop错误ERROR namenode.NameNode (NameNode.javamain(1657)) - Failed to start namenode java.net.BindException:Port in use:host1:50070
  4. Drupal 7.31 SQL注射分析POC
  5. Threejs 开发3D地图实践总结
  6. c++对象的存储空间
  7. open suse 42.3常用软件源(包括阿里云)
  8. 解决Android Studio 将String类型保存为.txt文件,按下button跳转到文件管理器(解决了保存txt文件到文件管理后,手机打开是乱码的问题)
  9. keil项目的调试与编译
  10. 区块链 + 大数据:EOS存储
  11. wxpython(python3.5)安装
  12. 【BZOJ3280】 小R的烦恼(费用流,建模)
  13. JS将日期转化为unix时间戳
  14. webpack创建library及从零开始发布一个npm包
  15. 【SQL】178. Rank Scores
  16. JDK_源码
  17. info.plist文件里面添加描述 -&gt; 配置定位,相册等
  18. 创建oracle数据库时,出现ORA-00922: 选项缺失或无效
  19. CF520B——Two Buttons——————【广搜或找规律】
  20. libevent网络编程汇总

热门文章

  1. mongodb安全整理
  2. cv2.Laplacian 模糊判断
  3. ReferenceError: internalBinding is not defined
  4. Cross-Entropy Loss 与Accuracy的数值关系(很重要,很好的博客)
  5. pbr 5.2.1需使用中科大的源,豆瓣的不行
  6. Python01 VSCode开发环境和入门程序
  7. 学习JavaScript你必须掌握的8大知识点!
  8. untiy3d action管理机制的编写
  9. VueJS坎坷之路222--vue cli 3.0引入静态文件
  10. bash编程之case语句,函数