반응형
Notice
Recent Posts
Recent Comments
Link
관리 메뉴

bro's coding

JSP.Sample Code 본문

[IT]/JSP

JSP.Sample Code

givemebro 2021. 4. 12. 23:17
반응형


1. form

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>form text 연동</title>
</head>
<body>
	<%-- method를 별도로 명시하지 않으면 get 요청방식
	   action 은 처리할 서버 프로그램 url 
	   
	   아래 전송 버튼을 누르면 
	   step1-2-form-action.jsp 가 
	   당신의 아이디는 머머머 입니다
	   당신이 사는 곳은 수지입니다 
 --%>
	<form action="step1-2-form-action.jsp">
		아이디 <input type="text" name="userId" required="required"><br>
		주소 <input type="text" name="userAddr" required="required"><br>
		<input type="submit" value="전송">
	</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>form data 처리</title>
</head>
<body bgcolor="yellow">
	<%
	String id = request.getParameter("userId");
	%>
	당신의 아이디는
	<%=id%>
	입니다
	<br> 당신이 사는 곳은
	<%=request.getParameter("userAddr")%>
	입니다
</body>
</html>

2.query string

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>step2-1-querystring.jsp</title>
</head>
<body>
	<%--
			query string 방식 : 서버url?name=value&name=value
 --%>
	<a href="step2-2-querystring-action.jsp?no=1&food=쫀드기">step2 action
		test1</a>
	<br>
	<a href="step2-2-querystring-action.jsp?no=2&food=달고나">step2 action
		test2</a>
	<br>
	<%--
			step2-2-querystring-action.jsp
			글번호  1번 
			음식명  쫀드기 
 --%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>step2-2-querystring-action.jsp</title>
</head>
<body>
	글번호
	<%=request.getParameter("no")%>번
	<br> 음식명
	<%=request.getParameter("food")%>쫀드기
</body>
</html>


3. post

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>step3-1-post.jsp</title>
</head>
<body>
	<form method="post" action="step3-2-post-action.jsp">
		이름 <input type="text" name="userName" required="required"><br>
		나이 <input type="number" name="userAge" required="required" min="1"><br>
		<input type="submit" value="등록">
	</form>
	<%--
		 step3-2-post-action.jsp
		 19세 이상이면 
		 성인 or 미성년 
		 이름 권민경
		 나이 19세 
 --%>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>step3-2-post-action.jsp</title>
</head>
<body>
	<%
	// post 요청방식 한글처리 
	request.setCharacterEncoding("utf-8");
	int age = Integer.parseInt(request.getParameter("userAge"));
	if (age >= 19) {
	%>
	성인
	<%
	} else {
	%>
	미성년
	<%
	}
	%>
	<br> 이름
	<%=request.getParameter("userName")%><br> 나이
	<%=age%>세

</body>
</html>


4. radio

<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>step4-1-radio</title>
</head>
<body>
	<%
	ArrayList<String> list = new ArrayList<String>();
	list.add("게살볶음밥");
	list.add("느타리버섯볶음밥");
	list.add("김치볶음밥");
	%>
	<form action="step4-2-action.jsp">
		<%
		for (int i = 0; i < list.size(); i++) {
		%>
		<input type="radio" name="menu" value="<%=list.get(i)%>"
			required="required"><%=list.get(i)%>
		<br>
		<%
		}
		%>
		<input type="submit" value="주문">
	</form>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>step4-2-action</title>
</head>
<body>
	<%=request.getParameter("menu")%>메뉴를 선택하셨습니다
</body>
</html>

5. select1

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>step5-1-select</title>
</head>
<body>
	<form action="step5-2-action.jsp">
		<select name="line" required="required">
			<option value="">-호선-</option>
			<%
			for (int i = 1; i < 10; i++) {
			%>
			<option value="<%=i%>"><%=i%>호선
			</option>
			<%
			}
			%>
		</select> <input type="submit" value="선택">
	</form>
	<%--
			1호선
			2호선
			...
			9호선 
			실제 전송되는 정보는 1 or 2 ~~ 9 
			
			선택을 누르면  step5-2-action.jsp 에서는 
			1~4호선 까지는   
			코레일 3호선 선택하셨습니다! 
			5~9호선 까지는 
			도시철도 6호선 선택하셨습니다! 
 --%>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>step5-2-action</title>
</head>
<body>
	<%
	int line = Integer.parseInt(request.getParameter("line"));
	String name = null;
	if (line < 5)
		name = "코레일";
	else
		name = "도시철도";
	%>
	<%=name%>
	<%=line%>
	호선을 선택하셨습니다!
</body>
</html>

6. select2

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>step6-1-select</title>
<script type="text/javascript">
	function sendLine() {
		let f = document.getElementById("lineForm");
		if (f.line.value == "") {
			alert("호선을 선택하세요!");
		} else {
			f.submit();
		}
	}
</script>
</head>
<body>
	<form action="step5-2-action.jsp" id="lineForm">
		<select name="line" required="required" onchange="sendLine()">
			<option value="">-호선-</option>
			<%
			for (int i = 1; i < 10; i++) {
			%>
			<option value="<%=i%>"><%=i%>호선
			</option>
			<%
			}
			%>
		</select>

	</form>
	<%--
			1호선
			2호선
			...
			9호선 
			실제 전송되는 정보는 1 or 2 ~~ 9 
			
			선택을 누르면  step5-2-action.jsp 에서는 
			1~4호선 까지는   
			코레일 3호선 선택하셨습니다! 
			5~9호선 까지는 
			도시철도 6호선 선택하셨습니다! 
 --%>
</body>
</html>


7. checkbox

<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>checkbox</title>
<script type="text/javascript">
	function checkForm() {
		let ma = document.getElementsByName("menu");
		let flag = false;
		for (let i = 0; i < ma.length; i++) {
			if (ma[i].checked) {
				flag = true;
				break;
			}
		}
		if (flag == false)
			alert("메뉴를 선택하세요");
		return flag;
	}
</script>
</head>
<body>
	<%
	ArrayList<String> list = new ArrayList<String>();
	list.add("삼겹살");
	list.add("꽃등심");
	list.add("광어회");
	%>
	<%--	step1. ArrayList를 활용해 checkbox를 동적으로 생성한다 
        step2. javascript로 체크박스를 하나 이상 선택했을 때 서버에 전송한다 
        		만약 아무 메뉴도 선택하지 않을 경우 alert으로 메뉴를 선택하세요 띄우고 
        		전송하지 않는다 
        step3. step7-2-checkbox-action.jsp 에서 전송받는 메뉴 정보를 테이블 형식으로 
        		화면에 보여준다 
 --%>
	<form action="step7-2-checkbox-action.jsp"
		onsubmit="return checkForm()" method="post">
		<%
		for (int i = 0; i < list.size(); i++) {
		%>
		<input type="checkbox" name="menu" value="<%=list.get(i)%>"><%=list.get(i)%>
		<br>
		<%
		}
		%>
		<input type="submit" value="주문">
	</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>step7-2</title>
<style type="text/css">
table {
	border-collapse: collapse;
}

table, td, th {
	border: 1px solid black;
}

td, th {
	padding: 10px;
}
</style>
</head>
<body>
	<%
	request.setCharacterEncoding("utf-8");//post 요청방식 한글처리 
	String menu[] = request.getParameterValues("menu");
	%>

	<table>
		<thead>
			<tr>
				<th>NO</th>
				<th>MENU</th>
			</tr>
		</thead>
		<tbody>
			<%
			for (int i = 0; i < menu.length; i++) {
			%>
			<tr>
				<td><%=i + 1%></td>
				<td><%=menu[i]%></td>
			</tr>
			<%
			}
			%>
		</tbody>
	</table>
</body>
</html>

8. form

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>step8-1-form</title>
</head>
<body>
	<%--
        step8-2-action.jsp
		19세 이상이면 
		누구님 즐거운 성인영화 관람되세요!
		미만이면 
		alert() 으로 나이 더 먹고 오세요!  확인누르면 
		step8-child.jsp로 이동시켜준다 	
 --%>
	<form action="step8-2-action.jsp">
		이름 <input type="text" name="customerName" required="required">
		나이 <input type="number" name="customerAge" min="1" required="required">
		<button type="submit">입장하기</button>
	</form>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>step8-2-action</title>
</head>
<body bgcolor="pink">
	<%
	int age = Integer.parseInt(request.getParameter("customerAge"));
	if (age >= 19) {
	%>
	<%=request.getParameter("customerName")%>님 즐거운 성인영화 관람되세요!
	<%
	} else {
	%>
	<script type="text/javascript">
				alert("<%=request.getParameter("customerName")%>
		님 나이 더 드시고 오세요!");
		//location.href="step8-child.jsp";
	</script>
	<%
	}
	%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>아동영화</title>
</head>
<body bgcolor="yellow">
	<marquee direction="down"> 즐거운 어린이 영화입니다^^ </marquee>
</body>
</html>
반응형

'[IT] > JSP' 카테고리의 다른 글

JSTL.if  (0) 2021.04.23
JSTL.taglib  (0) 2021.04.23
EL.ServletContext  (0) 2021.04.23
EL.session  (0) 2021.04.23
EL.Class 접근  (0) 2021.04.23
EL.parameter  (0) 2021.04.22
EL  (0) 2021.04.22
JSP.basic  (0) 2021.04.08
Comments