JSP

닭발

psys 2020. 7. 10. 15:12
728x90

설문조사

form.jsp
result.jsp

제출은 보통 button의 submit으로

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>04/ form.jsp</title>
</head>
<body>
	<h3>아래 항목에 답변해 주세요</h3>
	<form action="result.jsp">
		가장 먹고싶은 것은? <input type="text" name="food" value=""><br>
		가장 갖고싶은 것은? <input type="text" name="item" value=""><br>
		<button type="submit">제출</button>
	</form>
</body>
</html>
<%@page import="java.util.Enumeration"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	// 출력 스트림의 인코딩을 UTF-8로 설정
// response.setContentType("text/html; charset=UTF-8");

// 요청 파라미터는 기본이 라틴어(post)
request.setCharacterEncoding("UTF-8");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>04/ result.jsp</title>
</head>
<body>
	<h3>클라이언트 정보</h3>
	IP:
	<%=request.getRemoteAddr()%><br> 웹경로:
	<%=request.getContextPath()%><br> 요청URI:
	<%=request.getRequestURI()%><br> URL:
	<%=request.getRequestURL()%><br>
	<hr>
	<h3>요청 파라미터 정보</h3>
	Food:
	<%=request.getParameter("food")%><br> Item:
	<%=request.getParameter("item")%>
	<hr>
	<h3>요청 헤더 정보</h3>
	사용자 브라우저 정보 :
	<%=request.getHeader("User-Agent")%><br>
	<!-- 이전페이지 -->
	요청 페이지:
	<%=request.getHeader("Referer")%>
	<hr>
	<h3>헤더 목록 출력</h3>
	<%
		Enumeration headerEnum = request.getHeaderNames();
	while (headerEnum.hasMoreElements()) {
		String headerName = (String) headerEnum.nextElement();
		String headerValue = request.getHeader(headerName);
		out.println("headerName" + headerName + "<br>headerValue" + headerValue);
	}
	%>
</body>
</html>

 

이미지 안보여

이미지 경로

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>04/ form.jsp</title>
</head>
<body>
	<hr>
	패트 <img alt="패트1" src="../images/패트1.jpg" width="200"><br>
	패트 <img alt="패트2" src="/images/패트1.jpg" width="200"><br>
	패트 <img alt="패트3" src=".././03/../04/././../images/패트1.jpg" width="200"><br>
	패트 <img alt="패트4" src="/study/images/패트1.jpg" width="200"><br>
	패트 <img alt="패트5" src="http://localhost:8080/study/images/패트1.jpg" width="200"><br>
</body>
</html>

 

이미지 보여

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>04/ form.jsp</title>
</head>
<body>
	<%
		String appPath = request.getContextPath();
	%>

	<hr>
	패트 <img alt="패트1" src="../images/패트1.jpg" width="200"><br>
	패트 <img alt="패트2" src="<%=appPath%>/images/패트1.jpg" width="200"><br>
	패트 <img alt="패트3" src=".././03/../04/././../images/패트1.jpg" width="200"><br>
	패트 <img alt="패트4" src="<%=appPath%>/images/패트1.jpg" width="200"><br>
	패트 <img alt="패트5" src="http://localhost:8080/study/images/패트1.jpg" width="200"><br>
</body>
</html>

 

숫자 사이의 합 조회

input.jsp sum.jsp

input.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>04/ input.jsp</title>
</head>
<body>
	<h4>두 수 사이의 합을 조회</h4>
	<form action="sum.jsp">
		숫자1: <input type="text" name="num1" value=""><br> 숫자2: <input
			type="text" name="num2" value=""><br>
		<button type="submit">계산</button>
	</form>
</body>
</html>

sum.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>04/ sum.jsp</title>
</head>
<body>
	<%
	int numOne = Integer.parseInt(request.getParameter("num1"));
	int numTwo = Integer.parseInt(request.getParameter("num2"));

	int sum = 0;
	for (int i = numOne; i <= numTwo; i++) {
		sum += i;
	}
	out.println("<h2>" + numOne + "부터 " + numTwo + "까지의 합은 " + sum + "</h2>");
	%>
</body>
</html>

 

'JSP' 카테고리의 다른 글

JSP 구구단테이블  (0) 2020.07.10
시간출력입니당  (0) 2020.07.09
eclipse 서버등록  (0) 2020.07.08
웹서버 시작!  (0) 2020.07.08
cmd 관리자모드로 켜기  (0) 2020.07.07