- 숫자 하나 입력 받아 구구단 출력하기
- gugu.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>
<h1>구구단의 단수를 입력하세요</h1>
<form action="gugu2.jsp">
출력할 구구단 : <input name = "num"> <br>
<button>출력하기</button>
</form>
</body>
</html>
- 서블릿(Servlet)으로 구구단 출력하기
<%@ 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>
out.println("<table>");
int num = Integer.parseInt(request.getParameter("num"));
out.println("<tr><th colspan='2'>" + num + " 단 출력</th></tr>");
for(int i = 1 ; i <= 9 ; i++) {
out.println("<tr>");
// out.println("<h2>" + num + " * " + i + " = " + num*i + "</h2>");
out.println("<th>" + num + " * " + i + "</th>");
out.println("<th>" + num * i + "</th>");
out.println("</tr>");
}
out.println("</table>");
</body>
</html>
- 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>
<%
int num = Integer.parseInt(request.getParameter("num"));
%>
<table style="width: 800px; margin: 0 auto;" border = "1">
<tr>
<th colspan="2"><%=num + " 단 출력" %>
</tr>
<%
for(int i = 1 ; i <= 9 ; i++) {
%>
<tr>
<th><%=num + " * " + i %></th>
<th><%=num * i %></th>
</tr>
<%
}
%>
</table>
</body>
</html>
- EL(Expression Language)로 구구단 출력하기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<th>${param.num} 단 출력</th>
</tr>
<c:forEach begin="1" end="9" var="i">
<tr>
<th>${param.num} * ${i}</th>
<th>${param.num * i}</th>
</tr>
</c:forEach>
</table>
</body>
</html>