π μλ° μλ² νμ΄μ§ 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>