--> -->

JSP

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);
	}

}

 

 

 

 

 

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

무릇 책을 읽음에 있어서는 모름지기 한 책을 정독하여 뜻을 다 알아서 의심이 없은 연후에 다른 책을 읽을 것이요, 다독하는 데 힘써 바쁘게 넘어가지 말 것이니라. -이이

댓글 ( 0)

댓글 남기기

작성