简体   繁体   中英

problem using UserService of google appengine

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?

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 {

  }

}

web.xml文件中配置servlet后,您将调用在servlet映射中定义的URL。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM