[쉽게 배우는 JSP 웹 프로그래밍] 5장 정리, 연습문제

자바 서버 페이지(JSP)에서 사용되는 내장 객체에 대한 설명과 사용 예시를 제공한다. request 내장 객체는 웹 브라우저에서 서버의 JSP 페이지로 정보를 전달하며, response 내장 객체는 사용자의 요청을 처리한 결과를 서버에서 웹 브라우저로 전달한다. 이러한 내장 객체들은 웹 브라우저와 서버 간의 정보 전달을 가능하게 하며, 페이지 간 이동이나 페이지 새로고침 등의 기능을 제공한다. 이 문서는 이러한 내장 객체들의 사용법을 다양한 예제를 통해 설명하고 있다.
Feb 21, 2024
[쉽게 배우는 JSP 웹 프로그래밍] 5장 정리, 연습문제

request.jsp

<%@ page contentType="text/html; charset=utf-8" %> <html> <head> <title>Implicit Objects</title> </head> <body> <form action="process.jsp" method="post"> <p> 이 름 : <input type="text" name="name"> <input type="submit" value="전송"> </form> </body> </html>

process.jsp

<%@ page contentType="text/html; charset=utf-8"%> <html> <head> <title>Implicit Objects</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String name = request.getParameter("name"); %> <p> 이 름 : <%=name%> </body> </html>
 

핵심 키워드

  • request 내장 객체는 JSP에서 가장 많이 사용되는 기본 내장 객체로, 웹브라우저에서 서버의 JSP 페이지로 전달하는 정보를 저장한다.
  • 요청 파라미터는 사용자가 폼 페이지에 데이터를 입력한 후 서버에 전송할 때 전달되는 폼 페이지의 입력된 정보 형태다. 이러한 요청 파라미터는 <name=value> 형식으로 웹브라우저에서 서버의 JSP 페이지로 전송된다.
    • 요청 파라미터 관련 메소드로는 getParameter(), getParameterValues(), getParameterNames(), getParameterMap()이 있다.
 

header01.jsp

<%@ page contentType="text/html; charset=utf-8"%> <html> <head> <title>Implicit Objects</title> </head> <body> <% String hostValue = request.getHeader("host"); String alValue = request.getHeader("accept-language"); out.println("호스트명: " + hostValue + "<br>"); out.println("설정된 언어: " + alValue + "<br>"); %> <% java.util.Enumeration en = request.getHeaderNames(); while (en.hasMoreElements()){ String headerName = (String)en.nextElement(); String headerValue = request.getHeader(headerName); %> <%=headerName %> : <%=headerValue %><br> <% } %> <p> <%=request.getRemoteAddr() %> <p> <%=request.getQueryString() %> </body> </html>
 

핵심 키워드

  • 웹 브라우저는 HTTP 헤더에 부가적인 정보를 담아 서버로 전송한다. request 내장 객체는 헤더 정보나 쿠키 관련 정보를 얻을 수 있는 메소드를 제공한다.
  • request 내장 객체는 웹 브라우저의 요청 및 서버 관련 정보를 얻을 수 있는 메소드를 제공한다.
 

response.jsp

<html> <head> <title>Implicit Objects</title> </head> <body> <% response.sendRedirect("http://www.google.com"); %> </body> </html>

response01.jsp

<%@ page contentType="text/html; charset=utf-8" %> <html> <head> <title>Implicit Objects</title> </head> <body> <form action="response01_process.jsp" method="post"> <p> 아 이 디 : <input type="text" name="id"> <p> 비밀번호 : <input type="text" name="passwd"> <p> <input type="submit" value="전송"> </form> </body> </html>

response01_process.jsp

<%@ page contentType="text/html; charset=utf-8"%> <html> <head> <title>Implicit Objects</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String userid = request.getParameter("id"); String password = request.getParameter("passwd"); if (userid.equals("관리자") && password.equals("1234")) { response.sendRedirect("response01_success.jsp"); } else { response.sendRedirect("response01.jsp"); } %> </body> </html>

response01_success.jsp

<%@ page contentType="text/html; charset=utf-8" %> <html> <head> <title>Implicit Objects</title> </head> <body> 로그인을 성공했습니다!! </body> </html>

response01_failed.jsp

<%@ page contentType="text/html; charset=utf-8" %> <html> <head> <title>Implicit Objects</title> </head> <body> <p>로그인을 실패했습니다. <p> <a href="./response01.jsp">로그인 가기</a> </body> </html>
 

핵심 키워드

  • response 내장 객체는 사용자의 요청을 처리한 결과를 서버에서 웹 브라우저로 전달하는 정보를 저장한다.
  • 사용자가 새로운 페이지를 요청할 때와 같이 페이지를 강제로 이동하는 것을 리다이렉션이라고 한다.
    • 페이지 이동 시에는 문자 인코딩을 알맞게 설정해야 한다.
 

response02.jsp

<%@ page contentType="text/html; charset=utf-8" %> <html> <head> <title>Implicit Objects</title> </head> <body> <p> 이 페이지는 5초마다 새로고침 됩니다. <% response.setIntHeader("Refresh",5); %> <p><%=new java.util.Date() %> </body> </html>
 

핵심 키워드

  • 응답 HTTP 헤더 관련 메소드는 서버가 웹 브라우저에 응답하는 정보에 헤더를 추가하는 기능을 제공한다.
 

연습 문제

practice01.jsp

01 request 내장 객체는 JSP 페이지에서 가장 많이 사용되는 기본 내장 객체로, 웹 브라우저에서 서버의 JSP 페이지로 전달하는 정보를 저장한다. 관련 메소드로는 파라미터 값을 전달받는 getParameter(), 모든 요청 파라미터 값들을 배열 형태로 전달받는 getParameterValues(), 모든 요청 파라미터의 이름과 값을 Enumeraion 객체 타입으로 전달받는 getParameterNames(), 모든 요철 파라미터의 이름과 값을 Map 객체 타입으로 전달받는 getParameterMap()이 있다. 02 sendRedirect() 03 out 내장 객체

practice04.jsp

<%@ page contentType="text/html; charset=utf-8"%> <html> <head> <title>Implicit Objects</title> </head> <body> <form action="practice04_process.jsp" method="get"> <p> 아 이 디 : <input type="text" name="id"> <p> 비밀번호 : <input type="text" name="passwd"> <p> <input type="submit" value="전송"> </form> </body> </html>

practice04.jsp

<%@ page contentType="text/html; charset=utf-8"%> <html> <head> <title>Implicit Objects</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String userid = request.getParameter("id"); String password = request.getParameter("passwd"); %> <p>전송된 요청 파라미터 : <%=userid+"&"+password %> </body> </html>

practice05.jsp

<%@ page contentType="text/html; charset=utf-8"%> <%@ page import="java.util.Calendar"%> <html> <head> <title>Implicit Objects</title> </head> <body> <% Calendar cal = Calendar.getInstance(); int hh = cal.get(Calendar.HOUR); int mm = cal.get(Calendar.MINUTE); int ss = cal.get(Calendar.SECOND); int ampm = cal.get(Calendar.AM_PM); String apName = ""; if(ampm == 0)apName = "AM"; else apName = "PM"; out.println("현재 시간은 "+hh+":"+mm+":"+ss+" "+apName); response.setIntHeader("Refresh", 5); %> <p> <a href="practice05_data.jsp">Google 홈페이지로 이동하기</a> </body> </html>

practice05_data.jsp

<html> <head> <title>Implicit Objects</title> </head> <body> <% response.sendRedirect("http://www.google.com"); %> </body> </html>

practice07_menu

<nav class="navbar navbar-expend navbar-dark bg-dark"> <div class="container"> <div class="navbar-header"> <a class="navbar-brand" href="./practice07.jsp">Home</a> </div> </div> </nav>

practice07_footer

<footer class="container"> <p>&copy; BookMarket</p> </footer>

practice07_books

<%@ page contentType="text/html; charset=utf-8"%> <%@ page import="java.util.ArrayList"%> <%@ page import="ch05.practice.dto.Book"%> <jsp:useBean id="bookDAO" class="ch05.practice.dao.BookRepository" scope="session" /> <html> <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"> <title>도서 목록</title> </head> <body> <jsp:include page="menu.jsp" /> <div class="jumbotron"> <div class="container"> <h1 class="display-3">도서 목록</h1> </div> </div> <% ArrayList<Book> listOfBooks = bookDAO.getAllBooks(); %> <div class="container"> <% for (int i = 0; i < listOfBooks.size(); i++) { Book book = listOfBooks.get(i); %> <div class="text-left"> <h3><%="[" + book.getCategory() + "]" + book.getName()%></h3> <p><%=book.getDescription()%> <p><%=book.getAuthor() + " | " + book.getPublisher() + " | " + book.getUnitPrice() + "원"%> <p><a href="./product.jsp?id=<%=book.getBookId()%>" class="btn btn-secondary" role="button">상세 정보 &raquo;</a> </div> <hr> <% } %> </div> <jsp:include page="footer.jsp" /> </body> </html>

practice07_product

<%@ page contentType="text/html; charset=utf-8"%> <%@ page import="ch05.practice.dto.Book"%> <jsp:useBean id="bookDAO" class="ch05.practice.dao.BookRepository" scope="session" /> <html> <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"> <title>도서 정보</title> </head> <body> <jsp:include page="menu.jsp" /> <div class="jumbotron"> <div class="container"> <h1 class="display-3">도서 정보</h1> </div> </div> <% String id = request.getParameter("id"); Book book = bookDAO.getBookById(id); %> <div class="container"> <div class="row"> <div class="col-md-6"> <h3><%=book.getName()%></h3> <p><%=book.getDescription()%> <p> <b>도서 코드 : </b><span class="badge badge-danger"> <%=book.getBookId()%></span> <p> <b>출판사</b> : <%=book.getPublisher()%> <p> <b>저자</b> : <%=book.getAuthor()%> <p> <b>재고 수</b> : <%=book.getUnitsInStock()%> <p> <b>총 페이지 수</b> : <%=book.getTotalPages()%> <p> <b>출판일</b> : <%=book.getReleaseDate()%> <h4><%=book.getUnitPrice()%>원 </h4> <p> <a href="#" class="btn btn-info"> 도서 주문 &raquo;</a> <a href="./books.jsp" class="btn btn-secondary">도서 목록 &raquo;</a> </div> </div> <hr> </div> <jsp:include page="footer.jsp" /> </body> </html>
 

결론

해당 문제를 풀면서 내장 객체의 개념과 특징, 내장 객체 구성 요소의 사용법을 익힐 수 있었다.
Share article

More articles

See more posts
RSSPowered by inblog