JSP
- JSP :: 3주차 2017.09.30
- JSP :: 2주차 2017.09.30
- JSP :: 1주차 2017.09.30
JSP :: 3주차
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!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=EUC-KR"> <title>Insert title here</title> </head> <body> <h1>계산기</h1> <form action="CalculatorResult.jsp" method="post"> <input type="text" name ="firstnum"><p> <input type="text" name ="secondnum"><p> <input type="submit" value = "전송"> </form> </body> </html> | <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!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=EUC-KR"> <title>Insert title here</title> </head> <body> <h1>계산기</h1> <% String firstnum = request.getParameter("firstnum"); String secondnum = request.getParameter("secondnum"); out.print(firstnum + " + "); out.print(secondnum + " = "); out.print(Integer.parseInt(firstnum) +Integer.parseInt(secondnum)); %> </body> </html> |
String 클래스함수인 parseInt를 통해 문자열을 int형으로 형변환
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!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=EUC-KR"> <title>Insert title here</title> </head> <body> <h1>계산기</h1> <form action="CalculatorResult2.jsp" method="post"> <input type="text" name ="firstnum"><p> <input type="text" name ="secondnum"><p> <input type="submit" value = "전송"> </form> </body> </html> | <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!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=EUC-KR"> <title>Insert title here</title> </head> <body> <h1>계산기</h1> <% String firstnum = request.getParameter("firstnum"); String secondnum = request.getParameter("secondnum"); out.print(firstnum + " + "); out.print(secondnum + " = "); out.print(Float.parseFloat(firstnum) +Float.parseFloat(secondnum)); %> </body> </html> |
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!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=EUC-KR"> <title>Insert title here</title> </head> <body> <h1>계산기</h1> <form action="CalculatorResult3.jsp" method="post"> <input type="hidden" name = "cal" value = "+"> <input type="text" name ="firstnum"> + <input type="text" name ="secondnum"> <input type="submit" value = "전송"> </form> <p><p> <form action="CalculatorResult3.jsp" method="post"> <input type="hidden" name = "cal" value = "-"> <input type="text" name ="firstnum"> - <input type="text" name ="secondnum"> <input type="submit" value = "전송"> </form> <p><p> <form action="CalculatorResult3.jsp" method="post"> <input type="hidden" name = "cal" value = "*"> <input type="text" name ="firstnum"> * <input type="text" name ="secondnum"> <input type="submit" value = "전송"> </form> <p><p> <form action="CalculatorResult3.jsp" method="post"> <input type="hidden" name = "cal" value = "/"> <input type="text" name ="firstnum"> / <input type="text" name ="secondnum"> <input type="submit" value = "전송"> </form> </body> </html> |
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!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=EUC-KR"> <title>Insert title here</title> </head> <body> <h1>계산기</h1> <% String cal = request.getParameter("cal"); String firstnum = request.getParameter("firstnum"); String secondnum = request.getParameter("secondnum");
switch(cal){ case("+"): out.print(firstnum + " + "); out.print(secondnum + " = "); out.print(Integer.parseInt(firstnum) + Integer.parseInt(secondnum)); break;
case("-"): out.print(firstnum + " - "); out.print(secondnum + " = "); out.print(Integer.parseInt(firstnum) - Integer.parseInt(secondnum)); break;
case("*"): out.print(firstnum + " * "); out.print(secondnum + " = "); out.print(Integer.parseInt(firstnum) * Integer.parseInt(secondnum)); break;
case("/"): out.print(firstnum + " / "); out.print(secondnum + " = "); out.print(Integer.parseInt(firstnum) / Integer.parseInt(secondnum)); break; } %> </body> </html> |
Input type 중 hidden 변수에 값을 대입시켜 어떤 Form을 전송시켰는지 구별한다.
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!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=EUC-KR"> <title>Insert title here</title> </head> <body> <h1>계산기</h1> <form action="Calculator4.jsp" method="post"> <input type="hidden" name = "cal" value = "+"> <input type="text" name ="firstnum"> + <input type="text" name ="secondnum"> <input type="submit" value = "전송"> </form> <p><p> <form action="Calculator4.jsp" method="post"> <input type="hidden" name = "cal" value = "-"> <input type="text" name ="firstnum"> - <input type="text" name ="secondnum"> <input type="submit" value = "전송"> </form> <p><p> <form action="Calculator4.jsp" method="post"> <input type="hidden" name = "cal" value = "*"> <input type="text" name ="firstnum"> * <input type="text" name ="secondnum"> <input type="submit" value = "전송"> </form> <p><p> <form action="Calculator4.jsp" method="post"> <input type="hidden" name = "cal" value = "/"> <input type="text" name ="firstnum"> / <input type="text" name ="secondnum"> <input type="submit" value = "전송"> </form> <p> <% String cal = request.getParameter("cal"); String firstnum = request.getParameter("firstnum"); String secondnum = request.getParameter("secondnum");
if ( cal != null && firstnum != null && secondnum != null){ switch(cal){ case("+"): out.print(firstnum + " + "); out.print(secondnum + " = "); out.print(Integer.parseInt(firstnum) + Integer.parseInt(secondnum)); break;
case("-"): out.print(firstnum + " - "); out.print(secondnum + " = "); out.print(Integer.parseInt(firstnum) - Integer.parseInt(secondnum)); break;
case("*"): out.print(firstnum + " * "); out.print(secondnum + " = "); out.print(Integer.parseInt(firstnum) * Integer.parseInt(secondnum)); break;
case("/"): out.print(firstnum + " / "); out.print(secondnum + " = "); out.print(Integer.parseInt(firstnum) / Integer.parseInt(secondnum)); break; } } %> </body> </html> |
한 페이지 내에서 사칙연산을 하는 코드로, 초기에 값이 없으므로 이러한 경우를 판단해 코드 실행을 막아 에러를 방지한다.
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!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=EUC-KR"> <title>Insert title here</title> </head> <body> <h1>Select 테스트</h1> <form action="Test001.jsp" method="get"> <select name = "_portal"> <option value="네이버">네이버</option> <option value="다음">다음</option> <option value="구글">구글</option> </select> <p> <input type="submit" value="전송"> </form> </body> </html> |
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!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=EUC-KR"> <title>Insert title here</title> </head> <body> <%= "선택된 포탈 : " + request.getParameter("_portal")%> </body> </html> |
쉽다.
'JSP' 카테고리의 다른 글
JSP :: 2주차 (0) | 2017.09.30 |
---|---|
JSP :: 1주차 (0) | 2017.09.30 |
JSP :: 2주차
<%@ page language="java" contentType="text/html; charset=EUC-KR" |
<%@ page language="java" contentType="text/html; charset=EUC-KR" |
--------------------------------------------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=EUC-KR" |
|
---------------------------------------------------------------------------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=EUC-KR" |
'JSP' 카테고리의 다른 글
JSP :: 3주차 (0) | 2017.09.30 |
---|---|
JSP :: 1주차 (0) | 2017.09.30 |
JSP :: 1주차
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!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=EUC-KR"> <title>Insert title here</title> </head> <body> <% String a = "인하공전 "; String b = "컴퓨터 시스템과"; out.println(a + b + " 홍길동"); %> </body> </html> 스크립트릿 <% %> : 자바 코딩이 가능하다. |
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <%@ page import = "java.util.Date" %> <!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=EUC-KR"> <title>Insert title here</title> </head> <script language = "Javascript"> function viewDate(){ var d = new Date(); document.FormName.Date.value = (d.getYear()+1900)+"년"+(d.getMonth()+1)+"월"+d.getDate()+"일"; } </script> <body> <% Date d = new Date(); out.println((d.getYear()+1900)+"년"+(d.getMonth()+1)+"월"+d.getDate()+"일"); %> <Form Name = "FormName"> Local Date : <Input Type = "Text" Name = "Date"> <Input Type = "Button" Value = "Call Local date" onClick="viewDate()"> </Form> </body> </html> 자바스크립트 또한 가능하다. |
'JSP' 카테고리의 다른 글
JSP :: 3주차 (0) | 2017.09.30 |
---|---|
JSP :: 2주차 (0) | 2017.09.30 |