0
votes

public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { UserService userService = UserServiceFactory.getUserService();

    String thisURL = request.getRequestURI();
    if (request.getUserPrincipal() != null) {
        response.getWriter().println("<p>Hello, " +
                                     request.getUserPrincipal().getName() +
                                     "!  You can <a href=\"" +
                                     userService.createLogoutURL(thisURL) +
                                     "\">sign out</a>.</p>");
        System.out.println("<p>Hello, " +
                request.getUserPrincipal().getName() +
                "!  You can <a href=\"" +
                userService.createLogoutURL(thisURL) +
                "\">sign out</a>.</p>");
    } else {
        response.getWriter().println("<p>Please <a href=\"" +
                                     userService.createLoginURL(thisURL) +
                                     "\">sign in</a>.</p>");
    }
}

}

Where should i call this servlet from?

3

3 Answers

0
votes

After configuring your servlet in the web.xml file, you call the URL you defined in the servlet mapping.

0
votes

Please create new AppEngine project with generated codes, you can find how it works...

You must create LoginService (on GWT client) and implement it on server (class LoginServiceImpl). Then you can call LoginService.getCurrentUser()...

Check it out: http://code.google.com/intl/vi/appengine/docs/java/users/overview.html

0
votes

The Google App Engine providing user service to get the current user who logged-in.So you can use your above code at the starting of your application.You just write a simple servlet filter called login filter and you can check for every request you made whether the user logged in or not. So pass all your initial servlets through Login filter by configured in web.xml.

I hope this will help you.

You can follow following example

public class LoginFilter implements Filter {
  final Logger log = Logger.getLogger(LoginFilter.class.getName());

  @Override
  public void destroy() {

  }

  @Override
  public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
      throws IOException, ServletException {
    long before = System.currentTimeMillis();

    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    HttpServletRequest httpReq = (HttpServletRequest)req;
    String uri = httpReq.getRequestURI();
    if (user == null) {
      log.info("There is no user logged in yet");
      if (uri == null || uri.equals("")) {
          uri = "/servletpath";
      }
      String destUrl = "destUri=" + uri;
      RequestDispatcher dispatcher = req.getRequestDispatcher("/yourlogin.jsp" + "?" + destUrl);
      dispatcher.forward(req, resp);
      return;
    } 
    chain.doFilter(req, resp);
    long after = System.currentTimeMillis();
    String name = "";
    if (req instanceof HttpServletRequest) {
      name = ((HttpServletRequest) req).getRequestURI();
    }
    log.info(name + ": " + (after - before) + "ms");
  }

  @Override
  public void init(FilterConfig arg0) throws ServletException {

  }

}