jsp request 객체 사용 예제

jsp 내장 객체

1. HTTP 프로토콜 관련 메소드 (testjsp.jsp)
<%@ page import="java.util.Date" language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Request HTTP</title>
</head>
<body>

<%
    Date time = new Date();
    out.println(time);
    out.println("Hello JSP! <p>");
   
    out.println("인코딩 request.getCharacterEncoding() : " + request.getCharacterEncoding() + "<br/>");
    out.println("요청 정보 길이 request.getContentLength() : " + request.getContentLength() + "<br/>");
    out.println("요청한 정보를 전송할때 사용한 컨텐트 타입 request.getContentType() : " + request.getContentType() + "<br/>");
    out.println("웹 어플리케이션 콘텍스트 경로 request.getContextPath() : " + request.getContextPath() + "<br/>");
    out.println("정보 전송할때 사용한 방식 request.getMethod() : " + request.getMethod() + "<br/>");
    out.println("프로토콜 request.getProtocol() : " + request.getProtocol() + "<br/>");
    out.println("IP주소 request.getRemoteAddr() : " + request.getRemoteAddr() + "<br/>");
    out.println("URL 경로 request.getRequestURL() : " + request.getRequestURL() + "<br/>");
    out.println("서버이름 request.getServerName() : " + request.getServerName() + "<br/>");
    out.println("포트 request.getServerPort() : " + request.getServerPort() + "<br/>");
%>

</body>
</html>

2. Parameter 관련 메소드
3. Header 관련 메소드 (form1.jsp, getParameter.jsp)
form1.jsp에서 getParameter.jsp 를 불러온다.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>FORM</title>
</head>
<body>
<form action="getParameter.jsp" method="get">
이름 : <input type="text" name="name"> </input> <br/>
성 : <input type="text" name="familyname"> </input> <br/>
<input type="submit" value ="submit" name="submit">
</form>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="java.util.Enumeration"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Request Parameter, Header</title>
</head>
<body>
<%
    String name, familyname;
    name = request.getParameter("name");
    familyname = request.getParameter("familyname");
    out.println("당신의 이름은 " + familyname + name + "입니다. ");
   
    Enumeration e = request.getHeaderNames();
    String head;
    while(e.hasMoreElements()) {
        head = (String)e.nextElement();
        out.println(head + " : " + request.getHeader(head) + "<br/>");
    }
%>
</body>
</html>
브라우저 인코딩 설정을 해줘야 할 경우
<% request.setCharacterEncoding("euc-kr"); %>

한글문제는 UTF-8로 해결. tomcat의 server.xml의 URIEncoding설정을 UTF-8로 하는것을 잊지 말아야 한다.
저작자 표시 비영리 변경 금지
Tag //
|  1  | ...  88  |  89  |  90  |  91  |  92  |  93  |  94  |  95  |  96  | ...  108  |