2014년 4월 14일 월요일

[Java]Model2타입의 구구단 출력

1. dan_form.html
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="EUC-KR">
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8.     <!--
  9.     3.Three.java - model2
  10.      -->
  11.     <form method="post" action="/Three">
  12.         단을 입력하세요
  13.         <input type="text" name="dan">
  14.         <input type="submit">
  15.     </form>
  16. </body>
  17. </html>
2. GuGu.java
  1. package com.example;
  2.  
  3. public class GuGu {
  4.     public String danPrint(int dan){
  5.         String output="";
  6.         for(int i=1; i<10; i++){
  7.             output += dan+"*"+i+"="+(dan*i)+"<br>";
  8.         }
  9.         return output;
  10.     }
  11. }
3. Three.java
  1. package com.example;
  2.  
  3. import java.io.IOException;
  4.  
  5. import javax.servlet.RequestDispatcher;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.annotation.WebServlet;
  8. import javax.servlet.http.HttpServlet;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11.  
  12. @WebServlet("/Three")
  13. public class Three extends HttpServlet {
  14.    
  15.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  16.         int dan = Integer.parseInt(request.getParameter("dan"));
  17.         GuGu gugu = new GuGu();
  18.         String str = gugu.danPrint(dan);
  19.        
  20.         // request + alpha
  21.         request.setAttribute("answer", str);
  22.        
  23.         RequestDispatcher rd = request.getRequestDispatcher("/three_view.jsp");
  24.         rd.forward(request, response);
  25.     }
  26. }
4. three_view.jsp
  1. <%@ page language="java" contentType="text/html; charset=EUC-KR"
  2.     pageEncoding="EUC-KR"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <%
  11.     String str = (String)request.getAttribute("answer");
  12. %>
  13. <%=str%>
  14. </body>
  15. </html>

댓글 없음:

댓글 쓰기