Skip to content

Commit

Permalink
login added + Listener added
Browse files Browse the repository at this point in the history
  • Loading branch information
lalyos committed May 9, 2012
1 parent 6728b5f commit b8e7751
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 28 deletions.
25 changes: 25 additions & 0 deletions log.roo
@@ -0,0 +1,25 @@
// Spring Roo 1.3.0.BUILD-SNAPSHOT [rev 566af23] log opened at 2012-05-09 16:38:26
version
// [failed] version
version -jaime
// [failed] version -jaime
version jaime
// [failed] version jaime
version jaime
// [failed] version jaime
exit
// Spring Roo 1.3.0.BUILD-SNAPSHOT [rev 566af23] log closed at 2012-05-09 16:40:02
// Spring Roo 1.3.0.BUILD-SNAPSHOT [rev 566af23] log opened at 2012-05-09 16:40:02
// [failed] exit
// Spring Roo 1.3.0.BUILD-SNAPSHOT [rev 566af23] log closed at 2012-05-09 16:40:02
// Spring Roo 1.2.1.RELEASE [rev 6eae723] log opened at 2012-05-09 16:40:12
version
version jaime
version -jaime
help
version -jaime
version jaime
exit
// Spring Roo 1.2.1.RELEASE [rev 6eae723] log closed at 2012-05-09 16:41:25
// Spring Roo 1.1.5.RELEASE [rev d3a68c3] log opened at 2012-05-09 16:41:35
version jaime
70 changes: 70 additions & 0 deletions src/main/java/com/epam/kaka/MyLostener.java
@@ -0,0 +1,70 @@
package com.epam.kaka;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

/**
* Application Lifecycle Listener implementation class MyLostener
*
*/
public class MyLostener implements ServletContextListener, HttpSessionAttributeListener {

private int loggedInUsers = 0;
/**
* Default constructor.
*/
public MyLostener() {
// TODO Auto-generated constructor stub
}

/**
* @see HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent)
*/
public void attributeRemoved(HttpSessionBindingEvent se) {
// TODO Auto-generated method stub
}

/**
* @see HttpSessionAttributeListener#attributeAdded(HttpSessionBindingEvent)
*/
public void attributeAdded(HttpSessionBindingEvent se) {
if ("username".equals(se.getName())){
Object user = se.getValue();
System.out.println(user + "\n###\n###\n### is about to log in");
System.out.println("\n###\n###\n### logged in users:" + ++loggedInUsers);
}
}

/**
* @see ServletContextListener#contextInitialized(ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent sce) {
ServletContext context = sce.getServletContext();
Map<String,String> users = new HashMap<String,String>();
users.put("jeno", "pwd");
users.put("odon", "pwd");
users.put("valeria", "pwd");
context.setAttribute("users", users);
}

/**
* @see HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent)
*/
public void attributeReplaced(HttpSessionBindingEvent se) {
attributeAdded(se);
}

/**
* @see ServletContextListener#contextDestroyed(ServletContextEvent)
*/
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
}

}
25 changes: 25 additions & 0 deletions src/main/java/com/github/lalyos/mvc/LoginController.java
@@ -0,0 +1,25 @@
package com.github.lalyos.mvc;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class LoginController {

@RequestMapping("/login")
public String login(String username, String password, HttpSession session) {
Map<String,String> users = (Map<String, String>) session.getServletContext().getAttribute("users");

String origPassword = users.get(username);
if (password != null && password.equals(origPassword)) {
session.setAttribute("username", username);
}
return "redirect:/lunch/list";
}
}
13 changes: 10 additions & 3 deletions src/main/webapp/WEB-INF/views/footer.jsp
@@ -1,3 +1,10 @@
<hr/>
<h1>Footer ...</h1>
<hr/>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:url value="/login" var="loginUrl" />
<form class="well form-inline" action="${loginUrl}">
<input type="text" name="username" class="input-small" placeholder="Username">
<input type="password" name="password" class="input-small" placeholder="Password">
<label class="checkbox">
<input type="checkbox"> Remember me
</label>
<button type="submit" class="btn">Sign in</button>
</form>
22 changes: 0 additions & 22 deletions src/main/webapp/WEB-INF/views/list.jsp
Expand Up @@ -56,26 +56,4 @@

</div>


<c:set var="mapToDebug" scope="request" value="${param}" />
<jsp:include page="/WEB-INF/views/debugMap.jsp" >
<jsp:param name="mapTitle" value="params"/>
</jsp:include>

<c:set var="mapToDebug" scope="request" value="${requestScope}" />
<jsp:include page="/WEB-INF/views/debugMap.jsp" >
<jsp:param name="mapTitle" value="request"/>
</jsp:include>



<c:set var="mapToDebug" scope="request" value="${sessionScope}" />
<jsp:include page="/WEB-INF/views/debugMap.jsp" >
<jsp:param name="mapTitle" value="sessionScope"/>
</jsp:include>

<c:set var="mapToDebug" scope="request" value="${applicationScope}" />
<jsp:include page="/WEB-INF/views/debugMap.jsp" >
<jsp:param name="mapTitle" value="application"/>
</jsp:include>

9 changes: 6 additions & 3 deletions src/main/webapp/WEB-INF/views/menu.jsp
@@ -1,3 +1,5 @@
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:url value="/login" var="loginUrl" />
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
Expand All @@ -9,11 +11,12 @@
<a class="brand" href="#">Project name</a>
<div class="nav-collapse">
<ul class="nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
<li class="active"><a href="#">${not empty sessionScope.username ? sessionScope.username : 'Anonym'}</a></li>
<li><a href="${loginUrl}">Login</a></li>
</ul>
</div><!--/.nav-collapse -->


</div>
</div>
</div>
3 changes: 3 additions & 0 deletions src/main/webapp/WEB-INF/web.xml
Expand Up @@ -91,5 +91,8 @@



<listener>
<listener-class>com.epam.kaka.MyLostener</listener-class>
</listener>

</web-app>

0 comments on commit b8e7751

Please sign in to comment.