Cute Happy Ghost
본문 바로가기
Front/Html, css,jsp

request(요청정보) 세부사용법

by JENN_tech7 2020. 12. 14.
728x90
SMALL

 

 

 

 

**전체코드(view단과 ctrl단을 분리함)

1)parameterValues.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>parameterValues</title>
<script src="https://code.jquery.com/jquery-3.5.1.js" 
		integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" 
		crossorigin="anonymous"></script>
</head>
<body>

<div id ='parameter'>
	<div id = 'result'></div>
	
	<h3>parameter values</h3>
	
	<form name='frm_param' method='post' id = 'frm_param' >
		<label for = 'mid'>아이디</label>
		<input type='text' id = 'mid' name = 'mid' value = '박씨~	' />
		<br/>
		<label>성별</label>
		<label><input type='radio' name = 'gender' value = 'M'>남성</label>
		<label><input type='radio' name = 'gender' value = 'F'>여성</label>
		<br/>
		<label>취미</label>
		<label><input type = 'checkbox' value = '축구' name = 'hobby'/>축구</label>
		<label><input type = 'checkbox' value = '야구' name = 'hobby'/>야구</label>
		<label><input type = 'checkbox' value = '농구' name = 'hobby'/>농구</label>
		<label><input type = 'checkbox' value = '탁구' name = 'hobby'/>탁구</label>
		<label><input type = 'checkbox' value = '배구' name = 'hobby'/>배구</label>
		<br/>
		<label>선택과목</label>
		<select name= 'subject' size = '5' multiple>
			<option value = 'html5'>HTML5</option>
			<option value = 'css'>css</option>
			<option value = 'javascript'>javascript</option>
			<option value = 'java'>java</option>
			<option value = 'jsp'>jsp</option>
			<option value = 'ajax'>ajax</option>
			<option value = 'jquery'>jquery</option>
		</select>
		<br/>
		<label>첨부파일</label>
		<input type = 'file' name = 'attFile' multiple />
		<p/>
		<input type = 'button' value = '실행' id = 'btnRun'/>
	</form>

</div>
<script>
	$('#btnRun').on('click', function() {
		var param = $('#frm_param').serialize();
		$('#result').load('./jsp/parameterValuesCtrl.jsp', param);
		})

</script>
</body>
</html>

 

 

 

2)parameterValuesCtrl.jsp

<%@page import="java.util.Iterator"%>
<%@page import="java.util.Set"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.Enumeration"%>
<%@page import="java.util.Arrays"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>paramaterValuesCtrl</title>
</head>
<body>
<%
	//요청정보와 응답정보에 대한 인코딩끝
	request.setCharacterEncoding("utf-8");
	response.setContentType("text/html;charset-utf-8");
	
	//이 때 post는 대문자로, post일때만 실행해달라
	//get타입 : ?a = 123
	//if(request.getMethod().equals("POST"))
			if(request.getMethod().equals("GET")){
		String mid = request.getParameter("mid"); 
		String gender = request.getParameter("gender"); //체크된것만 넘어가기때문에 단수(하나만 넘어감, values아님)
		
		
		//체크된 여러개가 한꺼번에 넘어가기때문에 반환타입은 배열타입
		String[] hobby = request.getParameterValues("hobby"); 
		String[] subject = request.getParameterValues("subject"); 
		String[] attFile = request.getParameterValues("attFile"); 
		
		out.print("<li>아이디 : " + mid);
		out.print("<li>성별 : " + gender);
		out.print("<li>취미 : " + Arrays.toString(hobby));
		out.print("<li>과목 : " + Arrays.toString(subject));
		out.print("<li>첨부파일 : " + Arrays.toString(attFile));
		
		out.print("<h4>getParameterNames()</h4>");
		Enumeration<String> en = request.getParameterNames();
		while(en.hasMoreElements()){
			String str = en.nextElement(); //다중선택일 시 값이 없을때 객체가 넘어가지않음
			out.print("<li>" + str);
		}
		
		out.print("<h4>getParameterMap()</h4>");
		Map<String, String[]> map = null;
		map = request.getParameterMap(); 
		
		Set<String> set = map.keySet(); //Map이 String타입이었끼 때문에 얘도 String타입
		Iterator<String> it = set.iterator(); //set이 String타입이었기떄문에 얘도 String타입
		
		//looping
		while(it.hasNext()){
			String key = it.next();
			String[] values = map.get(key);
			out.print("<li>" + key + " : " + Arrays.toString(values));
		}
		
		
		
		out.print("<hr/>");
	}
	
	
%>
</body>
</html>

 

 

 

**실행화면(1)

 

 

**실행화면(2)

아이디와 첨부파일은 값을 안넣어도 들어감

나머지는 값을 넣어야지만 들어감

 

 

 

 

 

 

 

**실행화면(3)

 

728x90
LIST

'Front > Html, css,jsp' 카테고리의 다른 글

jstl 객체로 불러온 것을 select box선택시 선택되게 하기  (0) 2021.05.11
팝업창 만들기  (0) 2021.01.04
로그인, 로그아웃 간단구현  (0) 2020.12.11
header, footer모듈화  (0) 2020.12.11
jsp 특징  (0) 2020.12.10

댓글