赞
踩
- import java.io.*;
- import javax.servlet.ServletException;
- import javax.servlet.http.*;
-
- public class SetCookies extends HttpServlet {
-
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- response.setContentType("text/html");
- //新建一个Cookie
- Cookie cookie = new Cookie("Session-cookie", "svalue");
- //通过response对象向客户端中添加Cookie
- response.addCookie(cookie);
- cookie = new Cookie("Persistent-cookie", "pvalue");
- cookie.setMaxAge(3600);
- response.addCookie(cookie);
- PrintWriter out = response.getWriter();
- out.println("<a href='ShowCookies'>ShowCookies</herf>");
- }
- }
import java.io.*; import javax.servlet.ServletException; import javax.servlet.http.*; public class ShowCookies extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ //设置反馈给客户端的内容 resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); //通过req对象获得客户端所携带的Cookie Cookie[] cookies = req.getCookies(); if(cookies.length > 0){ for(Cookie cookie : cookies){ out.println(cookie.getName()+ " " + cookie.getValue()); } } } }
- import java.io.*;
- import javax.servlet.ServletException;
- import javax.servlet.http.*;
-
-
- public class ShowSession extends HttpServlet{
-
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
- resp.setContentType("text/html");
- HttpSession session = req.getSession(true);
- if(session.getAttribute("cnt") == null){
- session.setAttribute("cnt", 0);
- }else{
- session.setAttribute("cnt", (Integer)session.getAttribute("cnt") + 1);
- }
- PrintWriter out = resp.getWriter();
- out.println((Integer)session.getAttribute("cnt"));
- out.println("<br>" + session.getId());
- }
- }
- import java.io.*;
- import javax.servlet.ServletException;
- import javax.servlet.http.*;
-
- public class SessionInfo extends HttpServlet{
-
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
- HttpSession session = req.getSession(true);
- resp.setContentType("text/html");
- PrintWriter out = resp.getWriter();
- out.println("is new? " + session.isNew());
- out.println("id? " + session.getId());
- out.println("create time? " + session.getCreationTime());
- out.println("last access time ?" + session.getLastAccessedTime());
-
- out.println("id from req ?" + req.getRequestedSessionId());
- out.println("cookie ? " + req.isRequestedSessionIdFromCookie());
- out.println("URL ? " + req.isRequestedSessionIdFromURL());
- out.println("<a href=" + resp.encodeURL("SessionInfo") + "></a>");
- }
- }
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
-
- public class TestApplication extends HttpServlet{
-
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- ServletContext application = this.getServletContext();
- Integer count = (Integer) application.getAttribute("count");
- if(count == null){
- count = 1;
- }else{
- count = (Integer) application.getAttribute("count");
- }
- application.setAttribute("count", count + 1);
- out.println(count);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。