基本信息
源码名称:jsp单点登录实例(入门级)
源码大小:0.73M
文件格式:.zip
开发语言:Java
更新时间:2020-03-18
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 4 元 
   源码介绍

package com.sso.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

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;

/**
 * Servlet implementation class MainServlet
 */
public class MainServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String _COOKIENAME = "SSO" ;
    private ConcurrentMap SSOMap;
    public MainServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		execute(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		execute(request,response);
	}

	private void execute(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		String rs = "";
		try {
			String method = request.getParameter("method");
			String tourl = request.getParameter("tourl");
			if("login".equals(method)){
				session.setAttribute("tourl", tourl);
				response.sendRedirect("login.jsp");
			}else if("getFlag".equals(method)){
				String cookiename = request.getParameter("cookiename");
				System.out.println("---------cookiename:" cookiename);
				System.out.println("---------SSOMap.size():" SSOMap.size());
				if(cookiename!=null&&SSOMap!=null&&SSOMap.size()>0){
					String value = SSOMap.get(cookiename).toString();
					if(value != null){
						rs = value;
					}else{
						rs = "nologin";
					}
				}else{
					rs = "nologin";
				}
			}else if("logincheck".equals(method)){
				String username = request.getParameter("username");
				String password = request.getParameter("password");
				tourl = session.getAttribute("tourl").toString();
				if("t".equals(username)&&"123".equals(password)){
//					Cookie cookie = new Cookie("cookiename","cookievalue");
					SSOMap.put(_COOKIENAME, "t");
					System.out.println("--------tourl:" tourl);
					response.sendRedirect(tourl);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			PrintWriter pw = response.getWriter();
			pw.write(rs);
			pw.flush();
			pw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		SSOMap = new ConcurrentHashMap<String,Object>();
	}
}