SessionUtils
package com.slipp;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.slipp.user.LoginServlet;
//중복 제거 클래스
public class SessionUtils {
public static boolean isEmpty(HttpSession session, String key){
Object object=session.getAttribute(key);
if(object==null){
return true;
}
return false;
}
public static String getStringValue(HttpSession session, String key){
if(isEmpty(session, key)){
return null;
}
return (String)session.getAttribute(key);
}
public static String conFirm(HttpServletRequest request, HttpServletResponse response)
throws IOException{
HttpSession session=request.getSession();
String sessionUserId=SessionUtils.getStringValue(session, LoginServlet.SESSION_USER_ID);
if(sessionUserId==null){
response.sendRedirect("/");
return null;
}else{
return sessionUserId ;
}
}
}
class UpdateUserServlet
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;
import com.slipp.SessionUtils;
@WebServlet("/users/update")
public class UpdateUserServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String sessionUerid=SessionUtils.conFirm(request, response);
String userId=request.getParameter("userId");
if(!sessionUerid.equals(userId)){
response.sendRedirect("/");
return;
}
String password=request.getParameter("password");
String name=request.getParameter("name");
String email=request.getParameter("email");
User user=new User(userId, password, name, email);
UserDAO userDAO=new UserDAO();
userDAO.updateUser(user);
response.sendRedirect("/");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
class UpdateFormUserServlet
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.SessionUtils;
@WebServlet("/users/updateform")
public class UpdateFormUserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String sessionUerid=SessionUtils.conFirm(request, response);
UserDAO userDAO =new UserDAO();
User user=userDAO.findByUserId(sessionUerid);
request.setAttribute("user", user);
RequestDispatcher rd=request.getRequestDispatcher("/update_form.jsp");
rd.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
댓글 ( 4)
댓글 남기기