root / client / src / com / gonnatrak / LoginServlet.java @ 1

View | Annotate | Download

1
/**
2
 * Created by Andrey Khalzov
3
 * 30.07.2008 0:22:51
4
 */
5
package com.gonnatrak;
6
7
import com.gonnatrak.middleware.ejb.UserWorkerLocal;
8
import com.gonnatrak.middleware.exception.LoginFailedException;
9
import com.gonnatrak.middleware.util.ResourceHelper;
10
import com.gonnatrak.middleware.util.ServiceLocator;
11
import com.gonnatrak.util.ServletHelper;
12
13
import javax.servlet.ServletException;
14
import javax.servlet.http.HttpServlet;
15
import javax.servlet.http.HttpServletRequest;
16
import javax.servlet.http.HttpServletResponse;
17
import java.io.IOException;
18
19
public class LoginServlet extends HttpServlet {
20
21
    protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
22
        doPost(httpServletRequest, httpServletResponse);
23
    }
24
25
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
26
        final UserWorkerLocal userWorkerLocal = ServiceLocator.lookupLocal(UserWorkerLocal.JNDI_NAME);
27
28
        final String sessionId = request.getSession().getId();
29
        final String login = request.getParameter("login");
30
        final String password = request.getParameter("password");
31
        try {
32
            userWorkerLocal.loginUser(sessionId, login, password);
33
            response.sendRedirect("/subjects");
34
        } catch (LoginFailedException e) {
35
            String indexPage = ResourceHelper.getResourceForClass(getClass(), "/old_index.html");
36
            indexPage = indexPage.replaceFirst(
37
                    "<div class=\"error\"></div>", "<div class=\"error\">Неверный логин или пароль</div>"
38
            );
39
            ServletHelper.writeResponse(response, indexPage);
40
        }
41
    }
42
}