청년취업아카데미/HTML

[01.01.27 JSP]이론

RSpring41 2021. 1. 27. 16:57

 

 

▶ Servlet : 개별로 분리되어 사용됨

 

 

JSP : HTML내부에 Java로 작성되어 사용됨

 

JSP > Servlet > class > html 로 변환되어 실행됨

 

 

 

 

▶ JSP 예제

 

▷ 생성법 

 

<%@page import="org.apache.jasper.tagplugins.jstl.core.ForEach"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
	pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<%
		int result = 0;
		for(int i = 1; i <= 100; i++){ 
		result += i;
	}

	%>
	결과 <%=result %>
	
</body>
</html>

결과 

 

 

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"
	pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<table border="1">
		<tr>
			<%
				for (int i = 1; i <= 10; i++) {
			%>
			<td><%=i%></td>
			<%
				}
			%>
		</tr>
	</table>
	
	<table border="1">
		<tr>
			<%
				for (int i = 1; i <= 10; i++) {
					out.print("<td>"+i+"</td>");
				}
			%>
		</tr>
	</table>

</body>
</html>

결과 

 

▷JSP 에서 out.print를 쓸수 있는 이유

 

해당 워크스페이스 경로에 가보면 실행한 JSP파일이 class와 java파일로 변환되어 있다.

C:\Users\Chosun\Desktop\HTML\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\20210127\org\apache\jsp

 

Java파일 내부에 들어가보면 out이 자동으로 선언되어 있다. > 따라서 out객체 생성 없이 사용 가능하다.

 

▶HTML에서 JSP로 데이터 보내기 예제  >>>>>> (post방식)

▷ HTML                                                       

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<form action="ex12.jsp" method="post">
		<input type="text" name="txt">
		<input type="submit">	
	</form>
</body>
</html>

 

▷  JSP

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<%
		request.setCharacterEncoding("euc-kr");
		String txt = request.getParameter("txt");
	%>
	이름 : <%=txt %>
</body>
</html>

 

 

 

 

 

 

간단한 계산기 예제

▷ HTML                                                    

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>	
	<form action="ex14_calculator.jsp" method="post">
		<input type="text" name="num1"><br>
		<input type="text" name="num2"><br>
		<select name="select">
			<option value="+">+</option>
			<option value="-">-</option>
			<option value="*">*</option>
			<option value="/">/</option>
		</select>
		<input type="submit" value="계산">
	</form>
</body>
</html>

▷  JSP

<%@ page language="java" contentType="text/html; charset=EUC-KR"
	pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<%
	request.setCharacterEncoding("euc-kr");
	
	int result = 0;
	int num1 = Integer.parseInt(request.getParameter("num1"));
	int num2 = Integer.parseInt(request.getParameter("num2"));
	String select = request.getParameter("select");
	
	if(select.equals("+")){
		result = num1 + num2;
	}else if(select.equals("-")){
		result = num1 - num2;
	}else if(select.equals("*")){
		result = num1 * num2;
	}else{
		result = num1 / num2;
	}
	%>
	<%=num1 %> <%=select %> <%=num2 %> = <%=result %>

</body>
</html>

▷ 결과

 

 

마지막 예제

<%@ page language="java" contentType="text/html; charset=EUC-KR"
	pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<table border="1" cellspacing="0" style="width: 800px; height: 500px; ">
		<%for(int i = 1; i < 10; i++){	
			out.print("<tr align=center>");
			for(int j = 1; j < 10; j++){
				if(i%2==0){
					out.print("<td bgcolor=gray>" + i + " * " +  j  + " = " + i*j + "</td>");
				}else{
					if(j%2==0){
						out.print("<td bgcolor=gray>" + i + " * " +  j  + " = " + i*j + "</td>");
					}else{
						out.print("<td>" + i + " * " +  j  + " = " + i*j + "</td>");
					}
				}
			}
			out.print("</tr>");
		}
		%>
	</table>

</body>
</html>

 

결과