hgk0404
hgk0404.tistory
hgk0404

공지사항

전체 방문자
오늘
어제
  • 전체 카테고리
    • [컴퓨터비전]
    • [Computer Science]
      • [컴퓨터네트워크]
      • [알고리즘]
      • [자료구조 in C]
      • [C & C++]
      • [이산수학]
      • [Math]
    • [머신러닝]
      • [Numpy, Pandas]
    • [Cloud]
      • [AWS]
      • [NCP]
      • [Kubernetes]
      • [Terraform]
    • [Dev]
      • [가상환경]
      • [Linux]
      • [Docker]
    • [Python]
    • [Coding Test]
      • [백준]
      • [프로그래머스]
      • [SQL]
    • [WEB]
    • [자격증, 일상]
    • [엑셀]
    • [금융]

인기 글

최근 글

최근 댓글

250x250
hELLO · Designed By 정상우.
hgk0404

hgk0404.tistory

[JSP] request 내장 객체
[WEB]

[JSP] request 내장 객체

2023. 2. 11. 11:02
728x90

1. 입출력 객체(request, response, out)

 

1-1. request 내장 객체

request 내장 객체는 jsp에서 제공하는 객체로써 웹 브라우저나 클라이언트의 요청 정보를 저장하는 내장객체입니다.

 

request가 제공하는 기능은 다음과 같습니다.

1. 웹 브라우저나 클라이언트와 관련된 정보를 읽을 수 있는 기능
2. 클라이언트가 제공하는 파라미터를 가져와서 읽을 수 있는 기능
3. 클라이언트가 제공하는 헤더 정보를 가져와서 읽을 수 있는 기능
4. 클라이언트가 제공하는 쿠키 정보를 읽을 수 있는 기능

 

request 내장 객체가 가지는 메소드

메소드 설명
String getParameter(name) 파라미터 변수 name에 저장된 변수를 얻어서 읽어내는 메소드이며 반환값은 String이다.
String getParameterValues(name) 파라미터 변수 name에 저장된 변수값을 얻어내는 메소드이며 반환값은 String이다.

 

사용예시

 

test.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="request.jsp" method="post">
    <input type="text" name="userId"> <!-- 1 -->
    <input type="password" name="userPassword"> <!-- 2 -->
    <input type="submit" value="전송">
</form>

</body>
</html>

 

request.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>
	test.jsp에서 입력한 결과를 출력해줍니다. <br>
	사용자 ID: <%= request.getParameter("userId") %> <br> <!-- 1 -->
	사용자 PASSWORD: <% out.println(request.getParameter("userPassword")); %> <!-- 2 -->
</body>
</html>

<!-- 1 -->표현식 <%= %>에서는 세미콜론(;)을 사용하지 않음, 변수 또는 메소드의 결과값을 출력

<!-- 2 -->스크립틀릿 <% %>에서는 세미콜론(;)을 사용, 자바 코드를 삽입하기 위한 코드, 출력을 위해 out내장객체 사용

 

test.jsp

 

request.jsp

 

한글입력할때

 

인코딩을 안해줘서 한글입력이 깨지는 경우

 

<% request.setCharacterEncoding("utf-8"); %> 을 request.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>
<% request.setCharacterEncoding("utf-8"); %> <!-- 3 -->

<body>
	test.jsp에서 입력한 결과를 출력해줍니다. <br>
	사용자 ID: <%= request.getParameter("userId") %> <br>
	사용자 PASSWORD: <% out.println(request.getParameter("userPassword")); %>
</body>
</html>

 

1-2. response 내장객체

response 내장객체는 사용자의 요청을 처리한 결과를 서버에서 웹 브라우저로 전달하는 정보를 저장하고 서버는 요청 결과를 웹 브라우저에 전송합니다.

 

response 내장객체 관련 메소드

sendRedirect(String url)

 

sendRedirect메소드를 이용해서 관리자 계정일때 로그인 완료 페이지를 띄워보겠습니다.

 

test.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="response.jsp" method="post">
    <input type="text" name="userId"> <!-- 1 -->
    <input type="password" name="userPassword"> <!-- 2 -->
    <input type="submit" value="전송">
</form>

</body>
</html>

 

response.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>
<%	request.setCharacterEncoding("utf-8");
	String userId = request.getParameter("userId");
	String userPassword = request.getParameter("userPassword");
	
	if (userId.equals("관리자") && userPassword.equals("1234")) {
		response.sendRedirect("success.jsp");
	}
	else {
		response.sendRedirect("fail.jsp");
	}
%>
</body>
</html>

 

success.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>
	관리자 로그인 성공이에요!
</body>
</html>

 

fail.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>
	관리자 로그인 실패입니다 ㅜ.ㅜ
</body>
</html>

 

 

 

 

아이디를 다르게 적어 실패하는 경우

 

 

 

1-3. out 내장객체

 

 

 

 

728x90
저작자표시 동일조건

'[WEB]' 카테고리의 다른 글

[Java] HashMap을 이용한 전체출력(iterator, entrySet(), .forEach())  (0) 2023.02.14
[JSP] session 내장 객체와 setAttribute, getAttribute 메소드  (0) 2023.02.11
[HTML & CSS] viewport  (0) 2022.10.11
[JSP] 자바빈즈 액션태그  (0) 2022.10.08
[Java] 자바 참조변수  (0) 2022.10.06
'[WEB]' 카테고리의 다른 글
  • [Java] HashMap을 이용한 전체출력(iterator, entrySet(), .forEach())
  • [JSP] session 내장 객체와 setAttribute, getAttribute 메소드
  • [HTML & CSS] viewport
  • [JSP] 자바빈즈 액션태그
hgk0404
hgk0404
공부기록

티스토리툴바