DBILITY

vaadin session 실습 본문

front-end & ui/vaadin legacy

vaadin session 실습

DBILITY 2018. 6. 17. 15:37
반응형

httpSession(?)를 사용해 봤다면, 별다른 어려움은 없다.

 

package com.dbility.vseminar.vaadin_seminar.util;

import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RandomUtil {

	private static final Logger logger = LoggerFactory.getLogger(RandomUtil.class);

	private static final String AB = "0123456789abcdefghijklmnopqrstuvwxyz";
	private static final int LEN = 20;
	private static Random rnd = new Random();

	public static String randomKey(){
		StringBuilder sb = new StringBuilder( LEN );
		   for( int i = 0; i < LEN; i++ )
		      sb.append( AB.charAt( rnd.nextInt(AB.length()) ) );
		return sb.toString().substring(0,4)+"-"+sb.toString().substring(4,12)+"-"+sb.toString().substring(12,16)+"-"+sb.toString().substring(16,20);
	}
}
package com.dbility.vseminar.vaadin_seminar;

import java.util.Random;
import java.util.Set;

import javax.servlet.annotation.WebServlet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dbility.vseminar.vaadin_seminar.util.RandomUtil;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.Title;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinService;
import com.vaadin.server.VaadinServlet;
import com.vaadin.server.WrappedSession;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;
import com.vaadin.ui.themes.ValoTheme;

/**
 * This UI is the application entry point. A UI may either represent a browser window
 * (or tab) or some part of an HTML page where a Vaadin application is embedded.
 * <p>
 * The UI is initialized using {@link #init(VaadinRequest)}. This method is intended to be
 * overridden to add component to the user interface and initialize non-component functionality.
 */
@SuppressWarnings("serial")
@Title("vseminar")
@Theme("vseminar")
public class VSeminarUI extends UI {

	private static final Logger logger = LoggerFactory.getLogger(VSeminarUI.class);

    @Override
    protected void init(VaadinRequest vaadinRequest) {

    	VaadinRequest request = VaadinService.getCurrentRequest();

    	final WrappedSession session = request.getWrappedSession();

    	//WrappedSession session = vaadinRequest.getWrappedSession();

    	final HorizontalLayout layout = new HorizontalLayout();
		layout.setSizeUndefined();
		layout.setSpacing(true);
		layout.setMargin(true);

    	if(session == null) {
    		logger.debug("{}", session);
    	} else {
    		Set<String> set = session.getAttributeNames();
    		for (String str : set) {
    			logger.debug("{}", str);
			}
    		logger.debug("{}", session.getId());
    		logger.debug("{}", session.getLastAccessedTime());
    		logger.debug("{}", session.getMaxInactiveInterval());
    		logger.debug("{}", session.getAttribute("VSeminarUIServlet.lock"));
    		logger.debug("{}", session.getAttribute("com.vaadin.server.VaadinSession.VSeminarUIServlet"));

    		if(session.getAttribute("SESSID")==null) {
    			logger.debug("{}", "로그인해야지");
    			Label message = new Label("message");
    			message.setSizeUndefined();
    			message.setStyleName(ValoTheme.LABEL_H1);
    			message.setStyleName(ValoTheme.LABEL_COLORED);
    			message.setValue("로그인해야지");
    			layout.addComponent(message);

    			Button signon = new Button("SignOn");
    			signon.setSizeUndefined();
    			signon.setStyleName(ValoTheme.BUTTON_PRIMARY);
    			signon.addClickListener(e->{
    				session.setAttribute("SESSID", RandomUtil.randomKey());
    				getPage().reload();
    			});
    			layout.addComponent(signon);
    			layout.setComponentAlignment(signon, Alignment.BOTTOM_LEFT);
    		} else {
    			logger.debug("{}", session.getAttribute("SESSID"));

    			Label userName = new Label();
    			userName.setSizeUndefined();
    			userName.setStyleName(ValoTheme.LABEL_H1);
    			userName.setStyleName(ValoTheme.LABEL_COLORED);
    			userName.setValue(session.getAttribute("SESSID").toString());
    			layout.addComponent(userName);

    			Button signout = new Button("SignOut");
    			signout.setSizeUndefined();
    			signout.setStyleName(ValoTheme.BUTTON_PRIMARY);
    			signout.addClickListener(e->{
    				session.invalidate();
    				getPage().reload();
    			});

    			layout.addComponent(signout);
    			layout.setComponentAlignment(signout, Alignment.BOTTOM_LEFT);

    		}
    	}
	setContent(layout);
    }

    @WebServlet(urlPatterns = "/*", name = "VSeminarUIServlet", asyncSupported = true)
    @VaadinServletConfiguration(ui = VSeminarUI.class, productionMode = false)
    public static class VSeminarUIServlet extends VaadinServlet {
    }
}
DEBUG          c.d.v.vaadin_seminar.VSeminarUI   59 - com.vaadin.server.VaadinSession.VSeminarUIServlet
DEBUG          c.d.v.vaadin_seminar.VSeminarUI   59 - VSeminarUIServlet.lock
DEBUG          c.d.v.vaadin_seminar.VSeminarUI   61 - 899B4274096E44A3B85FA458C9D114D2
DEBUG          c.d.v.vaadin_seminar.VSeminarUI   62 - 1529222087140
DEBUG          c.d.v.vaadin_seminar.VSeminarUI   63 - 1800
DEBUG          c.d.v.vaadin_seminar.VSeminarUI   64 - java.util.concurrent.locks.ReentrantLock@26165440[Locked by thread http-nio-8080-exec-7]
DEBUG          c.d.v.vaadin_seminar.VSeminarUI   65 - com.vaadin.server.VaadinSession@3cd44bdd
DEBUG          c.d.v.vaadin_seminar.VSeminarUI   68 - 로그인해야지

DEBUG          c.d.v.vaadin_seminar.VSeminarUI   59 - com.vaadin.server.VaadinSession.VSeminarUIServlet
DEBUG          c.d.v.vaadin_seminar.VSeminarUI   59 - VSeminarUIServlet.lock
DEBUG          c.d.v.vaadin_seminar.VSeminarUI   59 - SESSID
DEBUG          c.d.v.vaadin_seminar.VSeminarUI   61 - 899B4274096E44A3B85FA458C9D114D2
DEBUG          c.d.v.vaadin_seminar.VSeminarUI   62 - 1529222101097
DEBUG          c.d.v.vaadin_seminar.VSeminarUI   63 - 1800
DEBUG          c.d.v.vaadin_seminar.VSeminarUI   64 - java.util.concurrent.locks.ReentrantLock@26165440[Locked by thread http-nio-8080-exec-1]
DEBUG          c.d.v.vaadin_seminar.VSeminarUI   65 - com.vaadin.server.VaadinSession@3cd44bdd
DEBUG          c.d.v.vaadin_seminar.VSeminarUI   86 - lksz-02b2jahl-ptw9-8v9p
반응형

'front-end & ui > vaadin legacy' 카테고리의 다른 글

vaadin Navigator 실습  (0) 2018.06.18
vaadin Polling 실습  (0) 2018.06.17
vaadin layout 실습  (0) 2018.06.16
vaadin field validation 실습2  (0) 2018.06.16
vaadin field validation 실습  (0) 2018.06.15
Comments