🌠 μžλ°” μ„œλ²„ νŽ˜μ΄μ§€ JSP

JSP - 2. μ„€λ¬Έμ§€ μž‘μ„±ν•˜μ—¬ JSP νŽ˜μ΄μ§€λ‘œ 데이터 λ„˜κΈ°κΈ°

Meteora_ 2021. 3. 15. 00:24
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>