752
No
class LoginServlet
package com.slipp.user;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.slipp.PasswordMismatchException;
import com.slipp.db.Database;
@WebServlet("/users/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userId=request.getParameter("userId");
String password=request.getParameter("password");
User user=Database.findByUserId(userId);
HttpSession session=request.getSession();
try{
User.login(userId, password);
session.setAttribute("userId", userId);
response.sendRedirect("/");
}catch(NotFoundUserException e){
forwardJSP(request, response, "존재하지 않는 사용자 입니다. 다시 로그인하세요.");
}catch(PasswordMismatchException e){
forwardJSP(request, response, "비밀번호가 틀립니다. 다시 로그인하세요.");
}
}
private void forwardJSP(HttpServletRequest request, HttpServletResponse response, String errorMessage)
throws ServletException, IOException {
request.setAttribute("errorMessage", errorMessage);
RequestDispatcher rd=request.getRequestDispatcher("login.jsp");
rd.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
class LogoutServlet
package com.slipp.user;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/users/logout")
public class LogoutServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session=request.getSession();
System.out.println("로그아웃 되었습니다.");
session.invalidate();
response.sendRedirect("/");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
class SaveUserServlet
package com.slipp.user;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.slipp.db.Database;
@WebServlet("/users/save")
public class SaveUserServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String userId=req.getParameter("userId");
String password=req.getParameter("password");
String name=req.getParameter("name");
String email=req.getParameter("email");
User user=new User(userId, password, name, email);
Database.addUser(user);
resp.sendRedirect("/");
}
}
댓글 ( 4)
댓글 남기기