⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sessionservlet.java

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 JAVA
字号:
/*____________________________________________________________________________*\
 *

 Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.

 These sources, libraries and applications are
 FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
 as long as the following conditions are adhered to.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer. 

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.

 3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission. 

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.

 *____________________________________________________________________________*|
 *
 * $Source: /cvsroot/pi3web/Pi3Web_200/Source/Servlet/org/pi3/servlet/samples/SessionServlet.java,v $
 * $Date: 2003/05/13 18:42:21 $
 *
 Description:
	Utility class, providing some usefull methods.

\*____________________________________________________________________________*/

package org.pi3.servlet.samples;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Enumeration;
import java.util.Date;

public class SessionServlet extends HttpServlet {
  public void doGet( HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {

	HttpSession session = request.getSession();
      String s;
      StringBuffer sbuf = new StringBuffer("<H1>Session Servlet</H1><HR>\r\n");
      sbuf.append("<H2>Request attributes</H2>");
	sbuf.append("<p><b>Uri: </b>" + request.getRequestURI());
	sbuf.append("<p><b>Cookie: </b>" + request.getHeader("Cookie"));
	sbuf.append("<p><b>Session Id: </b>" + request.getRequestedSessionId());
	sbuf.append("<p><b>Session valid: </b>" + request.isRequestedSessionIdValid());
	sbuf.append("<p><b>Session from cookie: </b>" + request.isRequestedSessionIdFromCookie());
	sbuf.append("<p><b>Session from url: </b>" + request.isRequestedSessionIdFromURL());

      sbuf.append("<H2>Session attributes</H2>");
	if (session != null) {
		try {
			session.isNew();
		}
		catch	(IllegalStateException e) {
			sbuf.append("<p>The session is now invalid!"); 
                  s = sbuf.toString();
                  response.setContentType("text/html; charset=utf-8");
                  response.setContentLength(s.length());
                  response.getOutputStream().print(s);
			return;
		};

		sbuf.append("<p><b>SID: </b>" + session.getId());
		Date date = new Date();
		date.setTime(session.getCreationTime());
		sbuf.append("<p><b>Created: </b>" + date);
		date.setTime(session.getLastAccessedTime());
		sbuf.append("<p><b>Last Access: </b>" + date);
		sbuf.append("<p><b>Max.Inactive: </b>" + session.getMaxInactiveInterval() + " sec.");
		sbuf.append("<p><b>New: </b>" + session.isNew());
		String [] a = session.getValueNames();
	      if (a.length != 0 ) {
			sbuf.append("<H2>Session values</H2><ol>");
		};
		for (int i = 0; i < a.length; i++) {
			sbuf.append("<li>" + a[i] + " = " + session.getValue(a[i]));
		};
		sbuf.append("</ol>");

		boolean isValid = true;
		try {
			session.isNew();
		}
		catch	(IllegalStateException e) {
			isValid = false;
		};

      	if (isValid && session.isNew()) {
			session.putValue ("user", "zimpel");
			session.putValue ("full name", "Holger Zimmermann");

                  sbuf.append("<H2>Action</H2>");
			sbuf.append("<p>Session.putValue() called, new values were stored with the session."); 
                  sbuf.append("<p>Press 'Refresh' to reload the Servlet and to retrieve session values."); 
		}
	      
		if (isValid && !session.isNew()) {
			session.invalidate();
			try {
				session.isNew();
			}
			catch	(IllegalStateException e) {
                        sbuf.append("<H2>Action</H2>");
				sbuf.append("<p>Session.invalidate() called, the session is now invalid."); 
				sbuf.append("<p>Press 'Refresh' to reload the Servlet and to create a new session."); 
			};
                  s = sbuf.toString();
                  response.setContentType("text/html; charset=utf-8");
                  response.setContentLength(s.length());
                  response.getOutputStream().print(s);
			return;
	      };
	};
      sbuf.append("<H2>Url rewriting</H2>");
	String url = "/servlet/SessionServlet/MyPathInfo?Param1=JohnDoe&Param2=TicTacToe";
	String encoded = response.encodeURL(url);
	if (url.equalsIgnoreCase(encoded)) {
		sbuf.append("<p>URL rewriting is not used.");
	} else {
		sbuf.append("<p>Url rewriting is used:");
		sbuf.append("<p><b>Url: </b>" + url);
		sbuf.append("<p><b>Encoded Url: </b>" + encoded);
		sbuf.append("<p>Try this <a href=" + encoded + ">link</a>");
	};
 
      s = sbuf.toString();
      response.setContentType("text/html; charset=utf-8");
      response.setContentLength(s.length());
      response.getOutputStream().print(s);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -