Example usage for javax.servlet.http HttpSession setAttribute

List of usage examples for javax.servlet.http HttpSession setAttribute

Introduction

In this page you can find the example usage for javax.servlet.http HttpSession setAttribute.

Prototype

public void setAttribute(String name, Object value);

Source Link

Document

Binds an object to this session, using the name specified.

Usage

From source file:com.gym.controller.UsuarioController.java

@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(Map<String, Object> map, HttpServletRequest request, @Valid AccesoUsuario accesoUsuario,
        BindingResult result) {/*from  w w w .jav a 2s .  co m*/

    if (result.hasErrors()) {
        return "index";
    }

    AccesoUsuario accesoOK = accesoUsuarioService.getAccesoUsuario(accesoUsuario.getLogin(),
            accesoUsuario.getPassword());

    if (accesoOK == null) {
        map.put("error", "error");
        return "index";
    } else {
        HttpSession misesion = request.getSession();
        misesion.setAttribute("accesoUsuario", accesoOK);

        return "redirect:./inicio";
    }
}

From source file:sample.session.SpringSessionPrincipalNameSuccessHandler.java

public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {

    HttpSession session = request.getSession();
    String currentUsername = authentication.getName();

    // tag::set-username[]
    session.setAttribute(Session.PRINCIPAL_NAME_ATTRIBUTE_NAME, currentUsername);
    // end::set-username[]
}

From source file:com.lyh.licenseworkflow.web.action.LoginAction.java

@Override
public String execute() throws Exception {
    users = userService.getAllUsers();/*w  w  w.j  ava2s. co m*/
    if (StringUtils.isEmpty(name)) {
        addActionError("please input user name!");
        return "login";
    }
    if (StringUtils.isEmpty(password)) {
        addActionError("please input user password!");
        return "login";
    }
    User user = userService.getByName(name);
    if (user == null) {
        addActionError("user is not existed!");
        return "login";
    }
    if (!user.getPassword().equals(password)) {
        addActionError("user password is wrong!");
        return "login";
    }
    String groupName = "";
    if (user.getGroups() != null && user.getGroups().size() > 0) {
        List<Group> groupList = new ArrayList<Group>();
        groupList.addAll(user.getGroups());
        groupName = groupList.get(0).getName();
    }
    HttpSession session = request.getSession();
    session.setAttribute(LicenseWorkFlowConstants.SESSION_USER, user);
    if (groupName.equals("instructor")) {
        return "instructor";
    } else if (groupName.equals("vendition"))
        return "vendition";
    else if (groupName.equals("majordomo"))
        return "majordomo";
    else if (groupName.equals("admin"))
        return "admin";
    else if (groupName.equals("boss"))
        return "boss";
    else
        response.sendRedirect("index.jsp");
    return SUCCESS;
}

From source file:be.fedict.eid.applet.beta.IdentityIntegrityServiceBean.java

public void checkNationalRegistrationCertificate(List<X509Certificate> certificateChain)
        throws SecurityException {
    LOG.debug("checking national registry certificate...");

    HttpServletRequest httpServletRequest;
    try {//from   w w  w . jav a2s. c o m
        httpServletRequest = (HttpServletRequest) PolicyContext
                .getContext("javax.servlet.http.HttpServletRequest");
    } catch (PolicyContextException e) {
        throw new RuntimeException("JACC error: " + e.getMessage());
    }

    HttpSession httpSession = httpServletRequest.getSession();
    X509Certificate certificate = certificateChain.get(0);
    httpSession.setAttribute("nationalRegistryCertificate", certificate);
}

From source file:be.fedict.eid.applet.beta.SecureClientEnvironmentBean.java

public void checkSecureClientEnvironment(String javaVersion, String javaVendor, String osName, String osArch,
        String osVersion, String userAgent, String navigatorAppName, String navigatorAppVersion,
        String navigatorUserAgent, String remoteAddress, Integer sslKeySize, String sslCipherSuite,
        List<String> readerList) throws InsecureClientEnvironmentException {
    String clientEnviromentResult = "java version: " + javaVersion + "\n" + "java vendor: " + javaVendor + "\n"
            + "OS name: " + osName + "\n" + "OS arch: " + osArch + "\n" + "OS version: " + osVersion + "\n"
            + "user agent: " + userAgent + "\n" + "navigator app name: " + navigatorAppName + "\n"
            + "navigator app version: " + navigatorAppVersion + "\n" + "navigator user agent: "
            + navigatorUserAgent + "\n" + "remote address: " + remoteAddress + "\n" + "ssl key size: "
            + sslKeySize + "\n" + "ssl cipher suite: " + sslCipherSuite + "\n" + "readers: " + readerList;
    LOG.debug(clientEnviromentResult);//from www  .ja va 2  s . c o  m

    SessionContextEntity sessionContext = this.sessionContextManager.getSessionContext();
    TestResultEntity testResultEntity = new TestResultEntity("Client Environment", clientEnviromentResult,
            sessionContext);
    this.entityManager.persist(testResultEntity);

    HttpServletRequest httpServletRequest;
    try {
        httpServletRequest = (HttpServletRequest) PolicyContext
                .getContext("javax.servlet.http.HttpServletRequest");
    } catch (PolicyContextException e) {
        throw new RuntimeException("JACC error: " + e.getMessage());
    }

    HttpSession httpSession = httpServletRequest.getSession();
    httpSession.setAttribute("clientJavaVersion", javaVersion);
    httpSession.setAttribute("clientJavaVendor", javaVendor);
    httpSession.setAttribute("clientOSName", osName);
    httpSession.setAttribute("clientOSArch", osArch);
    httpSession.setAttribute("clientOSVersion", osVersion);
    httpSession.setAttribute("clientReaders", readerList.toString());
    httpSession.setAttribute("clientUserAgent", userAgent);
    httpSession.setAttribute("clientSslCipherSuite", sslCipherSuite);
    httpSession.setAttribute("clientRemoteAddress", remoteAddress);
    httpSession.setAttribute("clientSslKeySize", sslKeySize);
    httpSession.setAttribute("clientNavigatorUserAgent", navigatorUserAgent);
    httpSession.setAttribute("clientNavigatorAppName", navigatorAppName);
    httpSession.setAttribute("clientNavigatorAppVersion", navigatorAppVersion);

    TestReportFactory testReportFactory = new TestReportFactory(this.entityManager);
    testReportFactory.startTestReport(javaVersion, javaVendor, osName, osArch, osVersion, userAgent,
            navigatorAppName, navigatorAppVersion, navigatorUserAgent);
}

From source file:com.synnex.saas.platform.core.servlet.CaptchaServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {//  w  w w . j a  v a 2  s  . com
        int width = 50;
        int height = 18;
        String captchaCode = RandomStringUtils.random(4, true, true);
        HttpSession session = request.getSession(true);
        session.setAttribute("captchaCode", captchaCode);

        response.setContentType("images/jpeg");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        ServletOutputStream out = response.getOutputStream();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        g.setColor(getRandColor(200, 250));
        g.fillRect(0, 0, width, height);
        Font mFont = new Font("Times New Roman", Font.BOLD, 18);
        g.setFont(mFont);
        g.setColor(getRandColor(160, 200));
        Random random = new Random();
        for (int i = 0; i < 155; i++) {
            int x2 = random.nextInt(width);
            int y2 = random.nextInt(height);
            int x3 = random.nextInt(12);
            int y3 = random.nextInt(12);
            g.drawLine(x2, y2, x2 + x3, y2 + y3);
        }
        g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
        g.drawString(captchaCode, 2, 16);
        g.dispose();
        ImageIO.write((BufferedImage) image, "JPEG", out);
        out.close();
    } catch (Exception e) {
        logger.error("Generate captcha failed.", e);
    }
}

From source file:CategoryServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from   ww w.  j a  v a2s  .c om*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */

        try {
            BufferedReader br = null;
            String price_range = request.getParameter("price_range");
            System.out.println(price_range);

            if (price_range.equals("null"))
                response.sendRedirect("Home.html");
            else {
                HttpSession session = request.getSession();
                session.setAttribute("price_range", price_range);
                br = new BufferedReader(new FileReader("Path to your questionnaire files"));
            }

            WriteQuestionnaireJSON obj = new WriteQuestionnaireJSON();

            String line;
            int i = 1;
            while ((line = br.readLine()) != null) {
                obj.buildJSON(line + "?", "Q" + i);
                i++;
            }
            JSONObject x = obj.write();
            out.println(x.toJSONString());
        } catch (Exception e) {
            out.println(e.getMessage());

        }
    }
}

From source file:be.fedict.eid.dss.protocol.simple.client.SignatureRequestServlet.java

private void setRelayState(String relayState, HttpSession session) {
    session.setAttribute(this.relayStateSessionAttribute, relayState);
}

From source file:be.fedict.eid.dss.protocol.simple.client.SignatureRequestServlet.java

private void setTarget(String target, HttpSession session) {
    session.setAttribute(this.targetSessionAttribute, target);
}

From source file:com.mothsoft.alexis.web.security.StoreUsernameInSessionFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
        throws ServletException, IOException {
    final HttpSession session = request.getSession(false);
    if (session != null && SecurityContextHolder.getContext().getAuthentication() != null) {
        session.setAttribute(USERNAME, SecurityContextHolder.getContext().getAuthentication().getName());
    }/* w  ww  . j a  va2s.c o  m*/
    chain.doFilter(request, response);
}