JSP request.getParameter()

별의개발자커비·2023년 4월 18일
0

JSP

목록 보기
5/31
post-thumbnail

요청 파라미터의 값 받기

  • String 변수 = request.getParameter(가져올 파라미터 이름); 의 형태로

  • 이러한 경우 앞에서 (input type="text" name="id") 로 작성했다면

  • String id = request.getParameter("id") 의 형태로 받아오면 되는 것이다.

<%
	request.setCharacterEncoding("utf-8");
		
	String id = request.getParameter("id");
	String pw = request.getParameter("pw");
	String name = request.getParameter("name");
		
	String phone1 = request.getParameter("phone1");
	String phone2 = request.getParameter("phone2");
	String phone3 = request.getParameter("phone3");
		
	String gen = request.getParameter("gender");
		
	// 선택되지 않은 변수의 값이 null로 출력되는 것이 마음에 들지 않으니 수정
	// String hobby1 =request.getParameter("hobby1");
	// String hobby2 =request.getParameter("hobby2");
	// String hobby3 =request.getParameter("hobby3");
		
	// 들어온 name의 값을 배열로 저장하기
	String hobby[] = request.getParameterValues("hobby");
		
	String comment = request.getParameter("comment");
%>
	
<p>
아이디 : <%= id %> <br>
비밀번호 : <%= pw %> <br>
이름 : <%= name %> <br>
<br>
전화번호 : <%= phone1 %> - <%= phone2 %> - <%= phone3 %> <br>
성별 : <%= gen %> <br>
취미 : <%
		if(hobby != null) {
			for(int i=0; i<hobby.length; i++){
				out.print(" " + hobby[i]);
			}
		}
	 %>
<br>
가입인사 : <%= comment %> <br>

학원 예제

// request2 내장객체
	<form action="process2.jsp" method="post">
		<p> 아이디: <input type="text" name="id">
		<p> 패스워드: <input type="password" name="pwd"> <input type="submit" value="전송">
	</form>
// process 처리
	<%
	String id = request.getParameter("id");
	String pwd = request.getParameter("pwd");
	%>
	<p>
		아이디: <%=id%>
	<p>
		패스워드: <%=pwd%>

include와 섞어서 받는 경우

  • 근데 이런 형태로 받는 경우도 있음
// fisrt.jsp
	<h3>이 파일은 first.jsp입니다.</h3>
	<jsp:include page="./second.jsp">
		<jsp:param value="hong gil dong" name="myname"/>
	</jsp:include>
	<p>java Server Page</p>
// second.jsp
	<h1>이름은: <%=request.getParameter("myname")  %> </h1>
	<h3>이 파일은 second.jsp입니다.</h3>
profile
비전공자 독학러. 일단 쌔린다. 개발 공부👊

0개의 댓글