需求:用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>