Reputation: 8497
Right now the controller servlet seems to handle dispatching correctly, so far as I can tell, in that it dispatches to a specified JSP. It uses an instance of MyToken
, kept in the session, to track the session. The controller dispatches to different JSP's depending on booleans of the MyToken
instance.
I've put all the JSP's in WEB-INF
so that only the controller servlet is exposed -- preferably, all requests/responses go through the servlet.
The intention is to use the MyToken
object as a session bean.
How does a JSP access such a session bean with EL? The login.jsp
page needs set the name value for the bean, and then dispatch back to the controller.
Can that be done with EL? Or, is it only possible to access a session bean with the JSTL?
It's intended as MVC, sans a framework -- just a very simple login with a simple bean for storing simple user data. A properties file contains a list of valid users which the token credentials are checked against.
Controller servlet:
package net.bounceme.dur.servlets;
import filter.PropertiesReader;
import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/controller")
public class Controller extends HttpServlet {
private static final Logger log = Logger.getLogger(Controller.class.getName());
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
log.info("controller processing request..");
String jsp = dispatcherLogic(request.getSession());
request.getRequestDispatcher("/WEB-INF/" + jsp).forward(request, response);
}
private String dispatcherLogic(HttpSession session) {
Properties properties = PropertiesReader.getProps();
MyToken token = (MyToken) session.getAttribute("token");
if (token != null) {
token.setAuthenticated(properties.containsValue(token.getName()));
} else {
token = new MyToken();
}
log.info(token.toString());
session.setAttribute("token", token);
if (token.isAuthenticated()) {
return "success.jsp";
} else {
if (token.isAttemptedLogin()) {
return "fail.jsp";
} else {
return "login.jsp";
}
}
}
private String dispatcherLogic0(HttpSession session) {
Map<String, String> p = PropertiesReader.getPropsAsMap();
Enumeration<String> names = session.getAttributeNames();
for (String s : Collections.list(names)) {
log.info(s);
}
MyToken t = (MyToken) session.getAttribute("token");
for (String s : p.keySet()) {
// t.getName() = p.containsValue(s);
}
return "hello.jsp"; //always to hello page for now
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "controller";
}
}
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>login</title>
</head>
<body>
<form name="bar" action="controller" method="POST">
name <input type="text" name="username" value="Jim" size='20' />
<p>
<input type="submit">
</p>
</form>
</body>
</html>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<filter>
<filter-name>AuthenticateFilter</filter-name>
<filter-class>filter.AuthenticateFilter</filter-class>
<init-param>
<param-name>Name</param-name>
<param-value>Value</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>AuthenticateFilter</filter-name>
<url-pattern>/foo.jsp</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<jsp-config>
<jsp-property-group>
<description>all</description>
<display-name>all</display-name>
<url-pattern>/*</url-pattern>
<scripting-invalid>true</scripting-invalid>
</jsp-property-group>
</jsp-config>
</web-app>
token for login credentials:
package net.bounceme.dur.servlets;
import java.util.logging.Logger;
public class MyToken {//should probably be immutable...
private static Logger log = Logger.getLogger(MyToken.class.getName());
private String name = "nemo";
private String role = "captain";
private String password = "abc";
private boolean authenticated = false; //remove down the road?
private boolean attemptedLogin = false; //remove down the road?
public MyToken() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isAuthenticated() {
return authenticated;
}
public void setAuthenticated(boolean authenticated) {
this.authenticated = authenticated;
}
public boolean isAttemptedLogin() {
return attemptedLogin;
}
public void setAttemptedLogin(boolean attemptedLogin) {
this.attemptedLogin = attemptedLogin;
}
@Override
public String toString() {
return name + authenticated + attemptedLogin;
}
}
Upvotes: 0
Views: 196
Reputation: 28569
Yes you can access you session beans name simply by
${sessionScope.token.name}
http://docs.oracle.com/javaee/6/tutorial/doc/bnaim.html
Upvotes: 1