๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐ŸŒ  ์ž๋ฐ” ์„œ๋ฒ„ ํŽ˜์ด์ง€ JSP

JSP - 2. ์„ค๋ฌธ์ง€ ์ž‘์„ฑํ•˜์—ฌ JSP ํŽ˜์ด์ง€๋กœ ๋ฐ์ดํ„ฐ ๋„˜๊ธฐ๊ธฐ

by Meteora_ 2021. 3. 15.
728x90

1. sumbit  ์œผ๋กœ jspํŽ˜์ด์ง€์—์„œ ๋ฐ”๋กœ ์ถœ๋ ฅ

insert.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert</title>
</head>
<body>


<form action = "insertAf.jsp">

์•„์ด๋””:<input type="text" name = "idp"><br>
ํŒจ์Šค์›Œ๋“œ:<input type="text" name = "pass"><br><br>
<p>์ทจ๋ฏธ</p>
<input type ="checkbox" name = "hobby" value = "sleep">์ž ์ž๊ธฐ
<input type ="checkbox" name = "hobby" value = "sing">๋…ธ๋ž˜ํ•˜๊ธฐ
<input type ="checkbox" name = "hobby" value = "game">๊ฒŒ์ž„ํ•˜๊ธฐ
<br><br>
<p>์—ฐ๋ น๋Œ€</p>


<input type ="radio" name = "age" value = "10">10๋Œ€
<input type ="radio" name = "age" value = "20">20๋Œ€
<input type ="radio" name = "age" value = "30">30๋Œ€
<input type ="radio" name = "age" value = "40">40๋Œ€
<input type ="radio" name = "age" value = "50">50๋Œ€
<input type ="radio" name = "age" value = "60">60๋Œ€์ด์ƒ
		
<br><br>
<p>๊ธฐํƒ€ํ•˜๊ณ ์‹ถ์€๋ง</p>
<textarea rows="10" name = "speak" cols ="30"></textarea><br><br>

<input type="submit" value = "์ „์†ก">
<input type="reset" value = "์ทจ์†Œ">

</form>


</body>
</html>

insertAf.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>

<%
String name = request.getParameter("id");
String password = request.getParameter("pass");
String hobby[] = request.getParameterValues("hobby");
String age = request.getParameter("age");
String Box = request.getParameter("speak");
%>
</head>
<body>

<h3>์ „์†ก๋œ ์ •๋ณด</h3>

์•„์ด๋””:<%= name %><br>
ํŒจ์Šค์›Œ๋“œ:<%= password %><br>

<%
for(int  i = 0; i<hobby.length; i++){

%>
์ทจ๋ฏธ: <%=hobby[i] %><br>

<%
}
%>

์—ฐ๋ น๋Œ€:<%= age %><br>
์ƒ์„ธ๋‚ด์—ญ:<%= Box %><br>



<%-- <%//์ž๋ฐ” ์ฝ”๋“œ๋ฅผ ์“ธ ์ˆ˜ ์žˆ๋”ฐ
//java ์˜์—ญ


out.println("์•„์ด๋”” : " + name +"<br>");
out.println("ํŒจ์Šค์›Œ๋“œ : " + password +"<br>");

if(hobby != null){
for(int  i = 0; i<hobby.length; i++){
	out.println("์ทจ๋ฏธ : " + hobby[i] +"<br>");
}
}

out.println("๋‚˜์ด : " + age+"<br>");
out.println("์ƒ์„ธ๋‚ด์—ญ : " + Box +"<br>");
//dao,dto

%> --%>
</body>
</html>

2. dto, setAttribute ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์ „์†ก

teach.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>
<form action = "teach2.jsp">

์•„์ด๋””:<input type="text" name = "idp"><br>
ํŒจ์Šค์›Œ๋“œ:<input type="password" name = "pass"><br><br>
<p>์ทจ๋ฏธ</p>
<input type ="checkbox" name = "hobby" value = "sleep">์ž ์ž๊ธฐ
<input type ="checkbox" name = "hobby" value = "sing">๋…ธ๋ž˜ํ•˜๊ธฐ
<input type ="checkbox" name = "hobby" value = "game">๊ฒŒ์ž„ํ•˜๊ธฐ
<br><br>
<p>์—ฐ๋ น๋Œ€</p>


<input type ="radio" name = "age" value = "10">10๋Œ€
<input type ="radio" name = "age" value = "20">20๋Œ€
<input type ="radio" name = "age" value = "30">30๋Œ€
<input type ="radio" name = "age" value = "40">40๋Œ€
<input type ="radio" name = "age" value = "50">50๋Œ€
<input type ="radio" name = "age" value = "60">60๋Œ€์ด์ƒ
		
<br><br>
<p>๊ธฐํƒ€ํ•˜๊ณ ์‹ถ์€๋ง</p>
<textarea rows="10" name = "speak" cols ="30"></textarea><br><br>

<input type="submit" value = "์ „์†ก">
<input type="reset" value = "์ทจ์†Œ">

</form>
</body>
</html>

teach2.jsp

<%@page import="dto.Human"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
    
<%
String name = request.getParameter("idp");
String password = request.getParameter("pass");
String hobby[] = request.getParameterValues("hobby");
int age = Integer.parseInt(request.getParameter("age"));
String Box = request.getParameter("speak");


Human human = new Human(name,password,hobby,age,Box);

request.setAttribute("human", human);
//๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ด๋™ํ•ด์ค€๋‹ค
pageContext.forward("insertAF1.jsp");
%>
    

dto

package dto;

import java.io.Serializable;
import java.util.Arrays;

public class Human implements Serializable{
	private String id;
	private String pwd;
	private String hobby[];
	private int age;
	private String message;
	public Human() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Human(String id, String pwd, String[] hobby, int age, String message) {
		super();
		this.id = id;
		this.pwd = pwd;
		this.hobby = hobby;
		this.age = age;
		this.message = message;
	}
	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[] getHobby() {
		return hobby;
	}
	public void setHobby(String[] hobby) {
		this.hobby = hobby;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	@Override
	public String toString() {
		return "Human [id=" + id + ", pwd=" + pwd + ", hobby=" + Arrays.toString(hobby) + ", age=" + age + ", message="
				+ message + "]";
	}
}

insertAF1.jsp

<%@page import="dto.Human"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
    <%
    Human human = (Human)request.getAttribute("human");
    
    System.out.println(human.toString());
    
    
    %>
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
์•„์ด๋””:<%=human.getId()%><br>
ํŒจ์Šค์›Œ๋“œ:<%= human.getPwd() %><br>

<%

String hobby[] = human.getHobby();


for(int  i = 0; i<hobby.length; i++){

%>
์ทจ๋ฏธ: <%=hobby[i] %><br>

<%
}
%>

์—ฐ๋ น๋Œ€:<%= human.getAge() %><br>
์ƒ์„ธ๋‚ด์—ญ:<%= human.getMessage() %><br>
</body>
</html>

๋Œ“๊ธ€