서블릿으로 할때보다 훨씬 더 쉽게 할 수 있음!!
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<style>
.container{
width:300px;
padding :16px;
margin: 0 auto;
background-color : #CEE3F6;
border-radius:8px;
}
#btn{
background-color: pink;
color:white;
border:none;
}
</style>
<body>
<form action="gugudan.jsp" method = "get" name ="gogodan1" class="container">
<p>출력할 구구단의 수를 지정해 주세요.</p>
<label class="input" for="num">출력할 구구단 : </label>
<input type="text" name="dan" id="input"><br>
<button type="submit" id="btn">전송</button>
<!-- <input type="submit" value="전송"/> -->
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>구구단 테이블 입니당</title>
</head>
<body>
<%
int dan = Integer.parseInt(request.getParameter("dan"));
%>
<table border="1px solid" width="200" >
<tr align="center" ><td colspan="2" style="background-color:pink;" ><%=dan %>단 출력</td></tr>
<%
for(int i=1; i<10; i++){
%>
<tr align="center"><td width="100"><%=dan %>*<%=i %></td><td><%=dan*i %></td></tr>
<%}
%>
</table>
</body>
</html>
🔺 서블릿 코드와 비교
package sec01.ex01;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/gogodan")
public class gogodanServlet extends HttpServlet {
private static final long serialVersionUID = 1L; //자바에서 서블릿을 순서대로 나열하기 위해
public void init(ServletConfig config) throws ServletException {
System.out.println("이닛이닛");
}
public void destroy() {
System.out.println("뿌셔뿌셔");
}
// HttpServletRequest request : 웹 브라우저에서 전송한 정보를 톰캣에 넘겨주는 역할
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8"); //1
// 요청에대한 응답에 써내려갈 내용을 담는 객체 생성
PrintWriter output = response.getWriter(); //2
// String user_dan = request.getParameter("user_dan");
// int num = Integer.parseInt(user_dan);
int num = Integer.parseInt(request.getParameter("user_dan"));
String data = "<html>";
data += "<style>"
+ "tbody tr:nth-child(2n) {"
+ "background-color : pink;"
+ "}"
+ "tbody tr:nth-child(2n+1) {"
+ "background-color : white;"
+ "}"
+"</style>";
data += "<body>";
output.print("<table border=1 width=800 align=center>");
output.print("<tr align=center>");
output.print("<td colspan=2 >" +num + "단 출력</td>");
output.print("</tr>");
for(int i=1; i<10; i++) {
output.print("<tr align=center background-color=pink>");
output.print("<td width=400 background-color=pink>");
output.print(num +"*"+i);
output.print("</td>");
output.print("<td width=400>");
output.print(num*i);
output.print("</td>");
output.print("</tr>");
data += "<br/>";
}
output.print("</table>");
data += "</body>";
data += "</html>";
output.print(data);
}
}