test
Форк от lirfrnk/test
1package servlets;
2
3import java.io.IOException;
4import java.io.PrintWriter;
5
6import javax.servlet.RequestDispatcher;
7import javax.servlet.ServletException;
8import javax.servlet.http.HttpServlet;
9import javax.servlet.http.HttpServletRequest;
10import javax.servlet.http.HttpServletResponse;
11
12import com.bittercode.constant.BookStoreConstants;
13import com.bittercode.constant.db.UsersDBConstants;
14import com.bittercode.model.User;
15import com.bittercode.model.UserRole;
16import com.bittercode.service.UserService;
17import com.bittercode.service.impl.UserServiceImpl;
18
19public class CustomerLoginServlet extends HttpServlet {
20
21UserService authService = new UserServiceImpl();
22
23public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
24PrintWriter pw = res.getWriter();
25res.setContentType(BookStoreConstants.CONTENT_TYPE_TEXT_HTML);
26String uName = req.getParameter(UsersDBConstants.COLUMN_USERNAME);
27String pWord = req.getParameter(UsersDBConstants.COLUMN_PASSWORD);
28User user = authService.login(UserRole.CUSTOMER, uName, pWord, req.getSession());
29
30try {
31
32if (user != null) {
33
34RequestDispatcher rd = req.getRequestDispatcher("CustomerHome.html");
35rd.include(req, res);
36pw.println(" <div id=\"topmid\"><h1>Welcome to Online <br>Book Store</h1></div>\r\n"
37+ " <br>\r\n"
38+ " <table class=\"tab\">\r\n"
39+ " <tr>\r\n"
40+ " <td><p>Welcome "+user.getFirstName()+", Happy Learning !!</p></td>\r\n"
41+ " </tr>\r\n"
42+ " </table>");
43
44} else {
45
46RequestDispatcher rd = req.getRequestDispatcher("CustomerLogin.html");
47rd.include(req, res);
48pw.println("<table class=\"tab\"><tr><td>Incorrect UserName or PassWord</td></tr></table>");
49}
50
51} catch (Exception e) {
52e.printStackTrace();
53}
54}
55
56}