본문 바로가기
국비 학원 가서 개발새발

국비학원 29일차) mvc

by 휴일이 2022. 11. 9.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	
<h3>회원가입</h3>	
<form action="stepmembertest7.jsp" method="post">
id <input type="text" name="id"> </br>
pwd <input type="password" name="pwd"> </br>
name <input type="text" name="name"> </br>
email <input type="text" name="email"> </br>
<input type="reset" value="초기화">
<input type="submit" value="가입">
</form>	
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR" import="model.Member"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	
	<% 
	String id=request.getParameter("id");
	String pwd=request.getParameter("pwd");
	String name=request.getParameter("name");
	String email=request.getParameter("email");
	
	Member m = new Member(id,pwd,name,email);
	%>
	
	<h3>회원정보</h3>
	<table border="1">
	<tr>
	<th>id</th>
	<th>pwd</th>
	<th>name</th>
	<th>email</th>
	</tr>
	
	<tr>
	<td><%= m.getId() %></td>
	<td><%= m.getPwd() %></td>
	<td><%= m.getName() %></td>
	<td><%= m.getEmail() %></td>
	</tr>
	
	<tr>
	<td><%= id %></td>
	<td><%= pwd %></td>
	<td><%= name %></td>
	<td><%= email %></td>
	</tr>
	</table>
	
</body>
</html>

 

package model;

public class Member {
	
	private String id;
	private String pwd;
	private String name;
	private String email;
	
	public Member() {
		super();
	}

	public Member(String id, String pwd, String name, String email) {
		super();
		this.id = id;
		this.pwd = pwd;
		this.name = name;
		this.email = email;
	}

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

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Override
	public String toString() {
		return "Member [id=" + id + ", pwd=" + pwd + ", name=" + name + ", email=" + email + "]";
	}
	
	
}

 

 

 

 

 

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"
	pageEncoding="EUC-KR" import="model.Member"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>

	<%!
	
	public void jspInit() {
		
		Member m = new Member("id1","pwd2","name1","email1");
		ServletContext context = getServletContext();
		context.setAttribute("m", m); //스코프의 객체의 하위 메서드 데이터 셋팅
	}
	
	%>
<h3>jspInit() 메서드에서 생성한 객체</h3>
	<%
	
	Member m = (Member)application.getAttribute("m");
	out.println("id "+m.getId()+"</br>");
	out.println("pwd "+m.getPwd()+"</br>");
	out.println("name "+m.getName()+"</br>");
	out.println("email "+m.getEmail()+"</br>");

	%>
</body>
</html>

 

 

 

 

 

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR" import="model.Member"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%!
	
	public void jspInit() {
		
		Member m = new Member("id1","pwd2","name1","email1");
		ServletContext context = getServletContext();
		context.setAttribute("m", m); //스코프의 객체의 하위 메서드 데이터 셋팅
	}
	
	%>
	
	<%
	 Member m = (Member)application.getAttribute("m");
	
	%>
	
	<form action="">
	
	<table border="1">
	<tr></tr>
	<th>id</th>
	<td><input type="text" name="id" value="<%= m.getId() %>"></td>
	<tr></tr>
	
	<tr></tr>
	<th>pwd</th>
	<td><input type="text" name="pwd" value="<%= m.getPwd() %>"></td>
	<tr></tr>
	
	<tr></tr>
	<th>name</th>
	<td><input type="text" name="name" value="<%= m.getName() %>"></td>
	<tr></tr>
	
	<tr></tr>
	<th>email</th>
	<td><input type="text" name="email" value="<%= m.getEmail() %>"></td>
	<tr></tr>
	</table>
	
	</form>
</body>
</html>

 

 

 

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	
	<h3>리다이렉트로 이동한 페이지</h3>
	
	<% 
	int age=0;
	String name=request.getParameter("name");
	String age_s=(String)request.getAttribute("age");
	
	if(age_s != null && !age_s.equals("")) {
		age=Integer.parseInt(age_s);
	}
	%>
	
	name : <%= name %>
	age : <%= age %>
</body>
</html>

 

 

 

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	
	<%
	pageContext.setAttribute("pageScope", "page data");
	request.setAttribute("requestScope","request data");
	session.setAttribute("sessionScope", "session data");
	application.setAttribute("applicationScope", "application data");
	
	String myPage=(String)pageContext.getAttribute("pageScope");
	String myRequest=(String)request.getAttribute("requestScope");
	String mySession=(String)session.getAttribute("sessionScope");
	String myApplication=(String)application.getAttribute("applicationScope");
	
	pageContext.forward("6.scopeTest.jsp");
	%>
	
</body>
</html>

 

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	
	<%
	String myPage=(String)pageContext.getAttribute("pageScope");
	String myRequest=(String)request.getAttribute("requestScope");
	String mySession=(String)session.getAttribute("sessionScope");
	String myApplication=(String)application.getAttribute("applicationScope");
	%>
	
myPage <%= myPage %>
myRequest <%= myRequest %>
mySession <%= mySession %>
myApplication <%= myApplication %>
	
</body>
</html>

 

 

 

 

 

<th> 굵게
<td> 얇게



redirect 서버에 부담이 덜함
forward 더 빠름


pageContext 현재 페이지 내에서만 존재
request 요청에 대한 응답이 있을 때까지만 존재
session 클라이언트와 연결이 끊길 때까지 존재
application 웹 어플리케이션이 종료될 때까지 존재

setAttribute(String name, Object value) Map형태
스코프 객체에 정보 저장

getAttribute(String name)
스코프 객체에 저장한 정보 읽기



Model 애플리케이션에서 사용하는 데이터(Java DB)
View 사용자와 상호작용할 UI(HTML CSS JAVASCRIPT JSP)
Controller 프로그램 흐름 제어(Servlet)

728x90