• Home
  • About
    • ming photo

      ming

      studying

    • Learn More
    • Twitter
    • Facebook
    • Instagram
    • Github
    • Steam
  • Archive
    • All Posts
    • All Tags
    • All categories
  • categories
    • HTML+CSS+JavaScript
    • JAVA
    • Algorithm
    • DB
    • JSP
    • 정보처리기사
    • Spring
    • Thymeleaf
    • 기술면접
  • Projects

JSP - NewLecture - 학습과제(사용자 입력을 통한 계산요청)

26 Mar 2021

🔷 학습과제(사용자 입력을 통한 계산요청)

✔ 과제 조건

- add로 연결되고, POST 방식을 이용하여 사용자가 x,y값을 입력하면 두 값의 더한 결과를 출력하라
  • html 코드
      <!DOCTYPE html>
      <html>
      <head>
      <meta charset="UTF-8">
      <title>add</title>
      </head>
      <body>
          <form action="add" method="post">
              <div>
                  <label>x : </label>
                  <input type="text" name="x" />
              </div>
              <div>
                  <label>y : </label>
                  <input type="text" name="y" />
              </div>
              <div>
                  <input type="submit" name="계산" />
              </div>
            
          </form>
      </body>
      </html>
    
  • servlet 코드
      package newlecture;
    
      import java.io.IOException;
      import java.io.PrintWriter;
    
      import javax.servlet.ServletException;
      import javax.servlet.annotation.WebServlet;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
    
      /**
      * Servlet implementation class add
      */
      @WebServlet("/add")
      public class add extends HttpServlet {
          private static final long serialVersionUID = 1L;
            
          /**
          * @see HttpServlet#HttpServlet()
          */
          public add() {
              super();
              // TODO Auto-generated constructor stub
          }
    
          /**
          * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
          */
          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              // TODO Auto-generated method stub
              response.getWriter().append("Served at: ").append(request.getContextPath());
          }
    
          /**
          * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
          */
          protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              response.setCharacterEncoding("UTF-8");
              response.setContentType("text/html; charset=UTF-8");
                
              /* 내가 작성한 코드
              * PrintWriter out = response.getWriter();
              * 
              * int x = Integer.parseInt(request.getParameter("x")); int y =
              * Integer.parseInt(request.getParameter("y"));
              * 
              * out.print("덧셈 결과 : " + (x+y));
              */
                
              String x_ = request.getParameter("x");
              String y_ = request.getParameter("y");
                
              // 입력값이 없을경우 대비
              int x = 0;
              int y = 0;
              // 형변환
              if(!x_.equals("")) x = Integer.parseInt(x_);
              if(!y_.equals("")) y = Integer.parseInt(y_);
                
              int result = x + y;
                
              response.getWriter().printf("result : %d\n" , result);
            
          }
    
      }
    

▶ 출력화면 add1

▶ 출력화면 add2

💡 배운점 및 느낀점
내가 작성한 코드도 숫자입력시 계산은 되지만 숫자입력이 되지않았을 경우에는
오류가 발생한다 여러가지 상황을 생각해서 코드를 작성해야 겠다


Share Tweet +1