본문 바로가기

JSP(Java Server Page)

JSP 기본 예제(사칙연산)

test5.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!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=UTF-8">

<title>Insert title here</title>

</head>

<body>


<form action="test5_ok.jsp" method="post">

<input type="text" name="n1">

<select name="sa">

<option value="">::선택::</option>

<option value="+">더하기</option>

<option value="-">빼기</option>

<option value="*">곱하기</option>

<option value="/">나누기</option>

<option value="%">나머지</option>

</select>

<input type="text" name="n2">

<input type="submit" value="="/>

</form>

</body>

</html>


==============================================================================================================


test5_ok.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

    

  <%

  int n1=Integer.parseInt(request.getParameter("n1"));

  int n2=Integer.parseInt(request.getParameter("n2"));

  

  String sa=request.getParameter("sa");

  String i="";

  int s=0;

  if(sa.equals("+"))

 s=n1+n2;

  if(sa.equals("-"))

 s=n1-2;

  if(sa.equals("*"))

 s=n1*n2;

  if(sa.equals("/"))

 s=n1/n2;

  if(sa.equals("%"))

 s=n1%n2;

  %>

<!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=UTF-8">

<title>Insert title here</title>

</head>

<body>


<%=n1%>

<%=sa%>

<%=n2%>

=

<%=s%>


</body>

</html>