본문 바로가기
HTML

42일차// jsp설문지를 작성하여 jsp페이지로 데이터 넘겨주기(파라미터 넘기기)

by aesup 2021. 3. 12.
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>

728x90