P09_mvc_redirect
com.web.member.dto
MemberDTO
package com.web.member.dto;
public class MemberDTO {
private String id;
private String pwd;
private String name;
public MemberDTO() {
super();
}
public MemberDTO(String id, String pwd, String name) {
super();
this.id = id;
this.pwd = pwd;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "[id=" + id + ", pwd=" + pwd + ", name=" + name + "]";
}
}
com.web.member
HomeController
package com.web.member;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.web.member.dto.MemberDTO;
@Controller
public class HomeController {
@RequestMapping("/index")
public String index() {
return "re/index";
}
@RequestMapping("/result")
public String resultRedirect(HttpServletRequest request, Model model, RedirectAttributes ra) {
String id = request.getParameter("id");
if(id.equals("abc")) {
ra.addFlashAttribute("result", "로그인 성공");
MemberDTO dto = new MemberDTO();
dto.setId(id);
ra.addFlashAttribute("test", dto);
return "redirect:rsOk";
}
return "redirect:rsNo";
}
@RequestMapping("/rsOk")
public String resultOk(Model model) {
return "re/rsOk";
}
@RequestMapping("/rsNo")
public String resultNo(Model model) {
return "re/rsNo";
}
}
Q03_mvc_quiz 추가
com.web.member...
MemberController
package com.care.member.controller;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.care.member.dao.MemberDAO;
import com.care.member.dto.MemberDTO;
import com.care.member.service.MemberService;
@Controller
public class MemberController {
@Autowired
private MemberService ms;
@GetMapping("/index")
public String index() {
return "/member/index";
}
@GetMapping("/joinForm")
public String join() {
return "/member/joinForm";
}
@PostMapping("/join")
public String join(MemberDTO dto) {
ms.join(dto);
return "/member/index";
}
@GetMapping("/list")
public String list(Model model) {
ms.list(model);
return "/member/memberList";
}
@RequestMapping("/check")
public String check(@RequestParam("id") String id, @RequestParam("pwd") String pwd,
Model model, RedirectAttributes ra) {
ms.check(id, pwd, model);
ra.addFlashAttribute("reMap", model);
return "redirect:/index";
}
}
MemberDAO
package com.care.member.dao;
import java.util.ArrayList;
import org.springframework.stereotype.Component;
import com.care.member.dto.MemberDTO;
import com.care.member.dto.UserCheckDTO;
@Component
public class MemberDAO {
private ArrayList<MemberDTO> list;
public MemberDAO() {
list = new ArrayList<>();
}
public void join(MemberDTO dto) {
list.add(dto);
System.out.println("등록 성공");
}
public ArrayList<MemberDTO> list() {
return list;
}
public UserCheckDTO check(String id, String pwd) {
UserCheckDTO dto = new UserCheckDTO();
if(list.size() != 0) {
for(MemberDTO man : list) {
if(id.equals(man.getId())) {
if(pwd.equals(man.getPwd())) {
dto.setChk(0);
dto.setName(man.getName());
return dto;
} else {
dto.setChk(1);
return dto;
}
}
}
}
if(dto.getChk() == 0) {
dto.setChk(-1);
}
return dto;
}
}
UserCheckDTO
package com.care.member.dto;
public class UserCheckDTO {
private int chk;
private String name;
public int getChk() {
return chk;
}
public void setChk(int chk) {
this.chk = chk;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
MemberService
package com.care.member.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import com.care.member.dao.MemberDAO;
import com.care.member.dto.MemberDTO;
import com.care.member.dto.UserCheckDTO;
@Service
public class MemberService {
@Autowired
private MemberDAO dao;
public void join(MemberDTO dto) {
if(dto.getId()!="" && dto.getPwd()!="" && dto.getName()!="") {
dao.join(dto);
}else {
System.out.println("실패");
}
}
public void list(Model model) {
model.addAttribute("list", dao.list());
}
public void check(String id, String pwd, Model model) {
UserCheckDTO dto = dao.check(id, pwd);
model.addAttribute("UserCheck", dto);
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Index</title>
<script type="text/javascript">
function userCheck(chk) {
if(chk ==1){
alert("비밀번호 오류");
}
else if (chk == -1){
alert("아이디 오류");
}
}
</script>
</head>
<body>
<h1>Index</h1>
<c:choose>
<c:when test="${reMap == null || reMap.UserCheck.chk !=0 }">
<script type="text/javascript">
userCheck(${reMap.UserCheck.chk})
</script>
<form action="check">
<input type="text" name="id" placeholder="id"/> <br>
<input type="text" name="pwd" placeholder="password"/> <br>
<input type="submit" value="login"/>
</form>
<a href="joinForm"> 회원가입</a>
</c:when>
<c:otherwise>
<h2>${reMap.UserCheck.name } 님 안녕하세요</h2>
</c:otherwise>
</c:choose>
<br>
<a href="list"> 모든 회원보기</a>
</body>
</html>
P10_Cookie_Session
com.web.controller
CookieController
package com.web.controller;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class CookieController {
@RequestMapping("/cookie")
public String myCookie(HttpServletResponse response,
@CookieValue(value="myCookie", required=false) Cookie cook, Model model) {
if(cook == null){
System.out.println("cookie : " + cook);
Cookie cookie = new Cookie("myCookie", "쿠키생성");
cookie.setMaxAge(10);
response.addCookie(cookie);
return "cookie";
}
model.addAttribute("cook", cook.getValue());
return "cookie";
}
@RequestMapping("popup")
public String popup() {
return "popup";
}
}
cookie.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>cookie.jsp</title>
<script type="text/javascript">
function popup(){
if("${cook}" == ""){
window.open("popup", "", "top=300, left=500, width=400, height=300")
}
}
</script>
</head>
<body onload="popup()">
<h1>cookie 페이지</h1>
<br/>
<h2>${cookie }</h2>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>popup.jsp</title>
</head>
<body>
<h2>쿠키를 위한 팝업창</h2>
</body>
</html>
SessionController
package com.web.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class SessionController {
@RequestMapping("makeSession")
public String makeSession(HttpServletRequest request) {
HttpSession session = request.getSession();
session.setAttribute("id", "test");
session.setAttribute("age", "20");
session.setAttribute("addr", "서울시 오디?");
return "makeSession";
}
@RequestMapping("resultSession")
public String resultSession() {
return "resultSession";
}
@RequestMapping("deleteSession")
public String deleteSession(HttpSession session) {
session.removeAttribute("id");
session.removeAttribute("age");
session.removeAttribute("addr");
return "deleteSession";
}
}
makeSession.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>makeSession.jsp</title>
</head>
<body>
<h1>세션 생성</h1>
<br/>
<h2>id : ${id }</h2>
<h2>age : ${age }</h2>
<h2>addr : ${addr }</h2>
<a href="resultSession">세션 확인</a> <a href="deleteSession">세션 삭제</a>
</body>
</html>
resultSession.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>세션 확인</h1>
<br/>
<h2>id : ${id }</h2>
<h2>age : ${age }</h2>
<h2>addr : ${addr }</h2>
<a href="makeSession">세션 생성</a> <a href="deleteSession">세션 삭제</a>
</body>
</html>
deleteSession.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>deleteSession.jsp</title>
</head>
<body>
<h1>세션 삭제</h1>
<br/>
<h2>id : ${id }</h2>
<h2>age : ${age }</h2>
<h2>addr : ${addr }</h2>
<a href="makeSession">세션 생성</a> <a href="resultSession">세션 확인</a>
</body>
</html>
LoginController
package com.web.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class LoginController {
@RequestMapping("login")
public String loginGet() {
return "/login/login";
}
@RequestMapping("checkUser")
public String checkUser(HttpServletRequest request) {
String id = "test";
String pwd = "1234";
String name = "테스트";
String reqid = request.getParameter("id");
String reqpwd = request.getParameter("pwd");
HttpSession session = request.getSession();
if(id.equals(reqid) && pwd.equals(reqpwd) || session.getAttribute("loginUser") != null){
session.setAttribute("loginUser", name);
return "/login/main";
}
return "redirect:/login";
}
@RequestMapping("logout")
public String logout(HttpServletRequest request) {
HttpSession session = request.getSession();
session.invalidate();
return "login/logout";
}
}
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login.jsp</title>
</head>
<body>
<h1>로그인</h1>
<c:import url="/cookie/"/>
<c:choose>
<c:when test="${loginUser == null}">
<form action="checkUser" method="post">
<input type="text" name="id" placeholder="id"> <br><br>
<input type="text" name="pwd" placeholder="password"> <br><br>
<input type="submit" value="submit">
</form>
</c:when>
<c:otherwise>
<h3>${loginUser } 님은 로그인 상태입니다.</h3>
<a href="checkUser"> main</a>
</c:otherwise>
</c:choose>
</body>
</html>
logout.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>logout.jsp</title>
<script type="text/javascript">
alert("로그아웃 되었습니다.");
location.href="login";
</script>
</head>
<body>
</body>
</html>
main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>main.jsp</title>
</head>
<body>
<h1>main</h1>
<c:choose>
<c:when test="${loginUser }== null">
<script type="text/javascript">location.href="login"</script>
</c:when>
<c:otherwise>
<h3>${loginUser }님 안녕하세요</h3>
<a href="logout">로그아웃</a> <a href="login">로그인</a>
</c:otherwise>
</c:choose>
</body>
</html>