CS6320:  SW Engineering of Web Based Systems

 

GAE: Example Java app that uses servlet and JSP that uses JSTL language for doing Google User Authentication and displaying clock and customized message

Later we will earn how to with the front-end specifications do Google User Authentication via deployment files and not in code

This code uses comp.google.appengine.api.user.* classes like UserService to perform Google User Authentication.

 

Servlet = does user authentication and forwards to JSP

JSP = does presentation layer logic ---interface USES JSTL (Java ) and EL libraries

 

 

 

 

Servlet = ClockServlet, does authentication and passes instance of com.google.appengine.api.users.User to JSP

         package clock;
         import java.io.IOException;
         import java.text.SimpleDateFormat;
         import java.util.Date;
         import java.util.SimpleTimeZone;
         import javax.servlet.RequestDispatcher;
         import javax.servlet.ServletException;
         import javax.servlet.http.*;
         import com.google.appengine.api.users.User;
         import com.google.appengine.api.users.UserService;
         import com.google.appengine.api.users.UserServiceFactory;
       

@SuppressWarnings("serial") public class ClockServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSSSSS"); fmt.setTimeZone(new SimpleTimeZone(0, "")); //grab UserServiceFactory for this app and get intance of UserService and current User UserService userService = UserServiceFactory.getUserService(); User user = userService.getCurrentUser(); //setup URLS for logging in or out and set user as one parameter will forward. String loginUrl = userService.createLoginURL("/"); String logoutUrl = userService.createLogoutURL("/"); req.setAttribute("user", user); req.setAttribute("loginUrl", loginUrl); req.setAttribute("logoutUrl", logoutUrl); req.setAttribute("currentTime", fmt.format(new Date())); resp.setContentType("text/html"); //forward on to JSP for display-presentation layer code RequestDispatcher jsp = req.getRequestDispatcher("/WEB-INF/home.jsp"); jsp.forward(req, resp); } }
    • getRequestDispatcher() method of the request instance takes a path to a JSP and returns its RequestDispatcher. Make sure the path starts with a forward slash (/).

    • To invoke the JSP, ClockServlet calls the forward() method of the RequestDispatcher, passing the HttpServletRequest and HttpServletResponse objects as arguments. The forward() method may throw the ServletException; in this example, we just add a throws clause to doGet().

     

JSP - home.jsp in war/WEB-INF directory, displays a clock and user Google account information (if user logged in)

     >>> uses the JSTL library to grab user information passed from servlet

<c:choose>...</c:choose>, <c:when>...</c:when>, and <c:otherwise>...</ c:otherwise> are examples of JSTL tags. The c: is the prefix associated with the library in the import directive. Here, the <c:choose> structure renders the <c:when> block when the user is signed in, and the <c:otherwise> block otherwise.

    >>> uses the EL library

  • An EL expression can appear in the text of the document, where its value is rendered into the text, or in a JSTL tag attribute, where its value is used by the tag. The expression ${logoutUrl} renders the String value of the logoutUrl attribute set by ClockServlet.

  • ${user != null} is an example of an EL expression. ${user} is the instance of com.google.appengine.api.users.User that represents the Google Account info of the user. ${user != null} shows how an EL expression can use simple operators, in this case producing a boolean value used by the <c:when test="...">.

  • ${user.email} is an example of accessing a JavaBean property of a value -----the result is equivalent to calling the getEmail() method of the User object value passed as an attribute to this JSP by ClockServlet.

     >>> jsp is passed "user" attribute representing instance of com.google.appengine.api.users.User that is passed by ClockServlet which calls it

     >>>jsp is passed "loginUrl" and "logoutUrl" as attributes, displays them as link to the user in case they want to login or if already logged in to logout

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>    this says load the jstl library we use here
<html>
<head>
<title>The Time Is...</title>
</head>
<body>
<c:choose>     jstl library call that says do when block if user is not null
    <c:when test="${user != null}">
        <p>
         52 | Chapter 2: Creating an Application
         Welcome, ${user.email}!      jsp grab of the user object passed, and its email variable
         You can <a href="${logoutUrl}">sign out</a>.
         </p>
    </c:when>
    <c:otherwise>  jstl library call that executes when user is null
        <p>
         Welcome!
         <a href="${loginUrl}">Sign in or register</a> to customize.
         </p>
    </c:otherwise>
</c:choose>


<p>The time is: ${currentTime}</p>   this is another EL command gettting parameter currentTime passed to this JSP
</body>
</html>

 

 

© Lynne Grewe