JSP内置对象的cookie和session实现简单登录界面
创建一个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);
} }
显示如下:


JSP内置对象的cookie和session实现简单登录界面的更多相关文章
- JSP 内置对象(request response session application out pageContext)
request对象 javax.servlet.http.HttpServletRequest接口的实例 request.setCharacterEncoding("utf-8" ...
- JSP内置对象--pageContent,request,response,session,application,config,out,page,exception
- EL表达式,JSP内置对象
基本语法格式 EL都是以 ${ 为起始.以} 为结尾的 ${ EL Expression} 示例: ${ “Helloworld” } //输出字符串常量 ${ str } //输出字符串变量st ...
- JSP内置对象——session
sessionsession表示客户端与服务器的一次会话Web中的session指的是用户在浏览某个网站时,从进入网站到浏览器关闭所进过的这段时间,也就是用户浏览这个网站所花费的时间从上述定义中可以看 ...
- jsp内置对象浅谈
jsp内置对象浅谈 | 浏览:1184 | 更新:2013-12-11 16:01 JSP内置对象:我们在使用JSP进行页面编程时可以直接使用而不需自己创建的一些Web容器已为用户创建好的JSP内置对 ...
- Jsp内置对象及EL表达式的使用
一.JSP的内置对象(9个JSP内置对象) JSP的内置对象引用名称 对应的类型 request HttpServletRequest response HttpServletResponse ses ...
- JavaWeb之 JSP:内置对象,EL表达式,JSP标签基础
JSP的内置对象 什么是JSP的内置对象呢? 在JSP页面进行编程的时候,如果我们要使用一些对象,如:HttpSession,ServletConfig,ServletContext这些对象,如果每次 ...
- JSP内置对象详解
jsp中内置对象:request.response.session.applecation.out.pagecontesx.config.page.exception.cookie 1.request ...
- JSP内置对象(上)
在JSP中为了简化页面的开发提供了一些内置的对象.这些对象不需要由JSP的编写者通过new关键字实例化,他们都由容器实现和管理,在所有的JSP页面中都可以使用内置对象. JSP中共有9大内置对象: o ...
随机推荐
- 进度条插件使用demo
1.下载地址: http://down.htmleaf.com/1502/201502031710.zip 2.效果图: 3.HTML代码:其中80设置当前所占百分比,即蓝色部分比例:注意引入必须的j ...
- LibreOJ #515. 「LibreOJ β Round #2」贪心只能过样例
题目描述 一共有 nnn个数,第 iii 个数 xix_ixi 可以取 [ai,bi][a_i , b_i][ai,bi] 中任意值.设 S=∑xi2S = \sum{{x_i}^2 ...
- 将C4C Service Request中的summary和其他附件同步到ERP的Billing Request去
C4C里将Service Request称为Work Ticket. 比如现在我的Service Request有两个行项目,只有第一个需要同步到ERP去.但是第二个行项目对于客户检查Invoice来 ...
- IM云通信行业步入快车道,谁将成为代表中国的全球IM“独角兽”?
2016年,Twilio的成功上市,以及抢眼的股价表现,拓宽了全球云通信行业的想象空间,行业内公司估值水平也集体上调. 在中国,IM云通信行业也从2016年开始进入了一个“黄金发展时期”,一批如融云. ...
- JS等号的小注释
一言以蔽之:一个等号是赋值操作,==先转换类型再比较,===先判断类型,如果不是同一类型直接为false.
- Expires和Cache-Control
本文原链接:https://blog.csdn.net/zhouziyu2011/article/details/71312452 浅谈前端性能优化(一)——Expires和Cache-Control ...
- 厌食?暴食?试试这个 VR 新疗法
今日导读 “我知道我要吃饭,但我真的什么都吃不下.” “我脑子里想的只有吃东西,吃吃吃!” ....... 作为一个正常人,我们完全无法想象患厌食症或贪食症人群所受的痛苦.长期的厌食,会使一个人瘦的只 ...
- Python 进程 线程总结
操作系统的底层是 进程 线程 实现的 进程 操作系统完成系统进程的切换,中间有状态的保存.进程有自己独立的空间,进程多,资源消耗大 进程是最小的资源管理单位 可以理解为盛放线程的容器 线程 线程是最小 ...
- Sum All Numbers in a Range-freecodecamp算法题目
Sum All Numbers in a Range 要求 给你一个包含两个数字的数组.返回这两个数字和它们之间所有数字的和. 最小的数字并非总在最前面. 思路 定义结果变量num 在for循环中,i ...
- Git基本操作笔记:初始化,用户设置,撤销修改
1. Git 初始化 git init git remote add repos_name repos_url git add . git commit -m 'commit message' gi ...