JSP——cooike和session

11 篇文章 0 订阅
2 篇文章 0 订阅

需求:用cooike和session分别做模拟登陆(在第一个网页输入用户名,在后面的网页显示用户名)。


session:

MainServlet.java

package web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MainServlet extends HttpServlet {

	@Override
	protected void service(
			HttpServletRequest req, 
			HttpServletResponse res)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String path = req.getServletPath();
		if("/toLogin.do".equals(path)) {
			toLogin(req,res);
		} else if("/login.do".equals(path)) {
			login(req,res);
		}
	}

	private void toLogin(
			HttpServletRequest req, 
			HttpServletResponse res) throws ServletException, IOException {
		req.getRequestDispatcher("WEB-INF/login.jsp")
			.forward(req, res);
	}

	private void login(
			HttpServletRequest req, 
			HttpServletResponse res) throws ServletException, IOException {
		HttpSession hs = req.getSession();
		hs.setAttribute("userCode", req.getParameter("userCode"));
		req.getRequestDispatcher("WEB-INF/index.jsp")
			.forward(req, res);
	}

}
login.jsp
<%@page pageEncoding="utf-8"%>
<!doctype html>
<html>
	<head>
		<meta charset="utf-8"/>
		<title>登陆</title>
	</head>
	<body>
		<div>
			<form action="login.do" method="post">
				<p>用户名:<input type="text" name="userCode"/></p>
				<p>密码:<input type="text"/></p>
				<p><input type="submit" value="登陆"/></p>
			</form>
		</div>
	</body>
</html>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<!DOCTYPE HTML>
<html>
  <head>
       <title>My JSP 'index.jsp' starting page</title>
    <meta charset="utf-8"/>
  </head>
  <body>
    <p>欢迎!${userCode }。</p>
  </body>
</html>

cookie:

MainServlet.java

package web;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
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;

public class MainServlet extends HttpServlet {

	@Override
	protected void service(
			HttpServletRequest req, 
			HttpServletResponse res)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String path = req.getServletPath();
		if("/toLogin.do".equals(path)) {
			toLogin(req,res);
		} else if("/login.do".equals(path)) {
			login(req,res);
		}
	}

	private void toLogin(
			HttpServletRequest req, 
			HttpServletResponse res) throws ServletException, IOException {
		req.getRequestDispatcher("WEB-INF/login.jsp")
			.forward(req, res);
	}

	private void login(
			HttpServletRequest req, 
			HttpServletResponse res) throws ServletException, IOException {
		Cookie c = new Cookie("userCode",
				URLEncoder.encode(req.getParameter("userCode"),"utf-8"));
		res.addCookie(c);		//在response中添加cookie,以便将cookie于发送到客户端
								//(session则是直接在服务器端设置)
		req.getRequestDispatcher("WEB-INF/index.jsp")
			.forward(req, res);
	}

}
login.jsp与上面相同

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<!DOCTYPE HTML>
<html>
  <head>
       <title>My JSP 'index.jsp' starting page</title>
    <meta charset="utf-8"/>
  </head>
  <body>
    <p>欢迎!${cookie.userCode.value }。</p>
  </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值