创建一个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实现简单登录界面的更多相关文章

  1. JSP 内置对象(request response session application out pageContext)

    request对象  javax.servlet.http.HttpServletRequest接口的实例 request.setCharacterEncoding("utf-8" ...

  2. JSP内置对象--pageContent,request,response,session,application,config,out,page,exception

  3. EL表达式,JSP内置对象

    基本语法格式 EL都是以 ${ 为起始.以} 为结尾的 ${ EL Expression} 示例: ${ “Helloworld” }  //输出字符串常量 ${ str }  //输出字符串变量st ...

  4. JSP内置对象——session

    sessionsession表示客户端与服务器的一次会话Web中的session指的是用户在浏览某个网站时,从进入网站到浏览器关闭所进过的这段时间,也就是用户浏览这个网站所花费的时间从上述定义中可以看 ...

  5. jsp内置对象浅谈

    jsp内置对象浅谈 | 浏览:1184 | 更新:2013-12-11 16:01 JSP内置对象:我们在使用JSP进行页面编程时可以直接使用而不需自己创建的一些Web容器已为用户创建好的JSP内置对 ...

  6. Jsp内置对象及EL表达式的使用

    一.JSP的内置对象(9个JSP内置对象) JSP的内置对象引用名称 对应的类型 request HttpServletRequest response HttpServletResponse ses ...

  7. JavaWeb之 JSP:内置对象,EL表达式,JSP标签基础

    JSP的内置对象 什么是JSP的内置对象呢? 在JSP页面进行编程的时候,如果我们要使用一些对象,如:HttpSession,ServletConfig,ServletContext这些对象,如果每次 ...

  8. JSP内置对象详解

    jsp中内置对象:request.response.session.applecation.out.pagecontesx.config.page.exception.cookie 1.request ...

  9. JSP内置对象(上)

    在JSP中为了简化页面的开发提供了一些内置的对象.这些对象不需要由JSP的编写者通过new关键字实例化,他们都由容器实现和管理,在所有的JSP页面中都可以使用内置对象. JSP中共有9大内置对象: o ...

随机推荐

  1. pc端常见布局样式总结(针对常见的)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. 51nod 1631 小鲨鱼在51nod小学

    基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 鲨鱼巨巨2.0(以下简称小鲨鱼)以优异的成绩考入了51nod小学.并依靠算法方面的特长,在班里担任了许多职务.   ...

  3. jquery绑定事件的系统参数传递方法

    如果是传递的事件自带函数,,可使用以下语法(以鼠标移动事件为例): init: function () { $(document).on("mousemove",loginOper ...

  4. 【UML】用例图Use Case diagram(转)

    http://blog.csdn.net/sds15732622190/article/details/48858219 前言 总结完UML概述,就该说道UML中的九种图了,这九种图中,最先要说的,就 ...

  5. exportfs: /mnt/demo requires fsid= for NFS export

    解决方法:/mnt/demo 10.0.1.57(fsid=0,rw,async) //加入fsid=0参数就可.

  6. 03_5_static关键字

    03_5_static关键字 1. static关键字 在类中,用static声明的成员变量为静态成员变量,它为该类的公用 变量,在第一次使用时被初始化,对于该类的所有对象来说,static成员变量只 ...

  7. java中求几个字符串的最大公共子串 使用了比较器Comparator

    package com.swift; import java.util.ArrayList; import java.util.Collections; import java.util.Compar ...

  8. TypeError: Cannot read property 'tap' of undefined

    E:\vue-project\vue-element-admin-master>npm run build:prod vue-element-admin@3.8.1 build:prod E:\ ...

  9. Slasher Flick-freecodecamp算法题目

    Slasher Flick(截断数组) 要求 返回一个数组被截断n个元素后还剩余的元素,截断从索引0开始. 思路 利用.splice(0,howMany)删除数组中索引从0开始的howMany个元素 ...

  10. linux关于yum

    yum仓库设置:1.cd /etc/yum.repos.d yum仓库 2.CentOS-Base.repo 网络源 CentOS-Media.repo 光盘源 设置 vi CentOS-Media. ...