html의 static text값 java로 가져오기

Yodi.Song·2022년 2월 10일
0

jquery, jsp, servlet

목적

totalPrice의 텍스트값을 가져오려고 한다.

<td><span id="totalPrice">0</span></td>

방법

1. JSP에 hidden input 만들기

<input type="hidden" id="price" name="price">
        ...
        
        <table class="orderSum">
            <colgroup>
                <col style="width: 30%;">
                <col style="width: 70%;">
            </colgroup>
            <tbody>
            <tr>
                <th>총 금액</th>
                <td><span id="totalPrice">0</span></td>
            </tr>

먼저 input 엘리먼트 price를 hidden type으로 만든다.

2. js에서 hidden input 초기화


const totalPrice = parseInt($('#totalPrice').text());
$('#price').val(totalPrice);	// to pass the value to OrderServlet
return true;

totalPrice의 text 값을 가져와서 price의 value 값에 넣어준다.

3. servlet에 가져오기

 @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String price = req.getParameter("price");
        
        ...
    }

요약: html에서 input 엘리먼트를 hidden type으로 하나 만들고 js파일에서 값을 넣어준다.

0개의 댓글