jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | <!-- --> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width , initial-scale=1"> <title>sample</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script> <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> <style type="text/css"> li:nth-child(odd):after{ /* 홀수번째 모두 */ content:' odd'; } li:nth-child(even):after{ /* 짝수번째 모두 */ content:' even'; } li:nth-child(5n):before{ /* 5의 배수 번째 모두 */ content:'5의배수- ' } li:nth-child(-n+3){ /* 앞에서 1번부터 3번까지 */ background-color: red } li:nth-child(n+25){ /* 20번부터 끝까지 */ background-color: green } li:nth-child(n+5):nth-child(-n+8){ /* 앞에서 2번부터 4번 전까지 */ background-color: gold; } </style> </head> <body> <p><br/></p> <div class="container"> <div> <%-- HelloWord --%> HelloWord <ul> <% for(int i =0; i<30; i++){ out.println("<li> "+i+"</li>"); } %> </ul> </div> </div> <p><br/></p> </body> </html> | cs |
jsp 수정 및 테스트
홀수와 짝수의 위치가 안맞아서 확인을 해보니
java에서 for문을 0부터 진행했는데
html에서는 인덱스가 1부터 시작해서 표현상 홀수짝수가 안맞는 현상 발생
그래서 1부터 30이하로 수정
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | <!-- --> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width , initial-scale=1"> <title>sample</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script> <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> <style type="text/css"> .Numcheck{ border: 1px solid black; font-size: 20px; } li:nth-child(odd):before{ /* 홀수번째 모두 */ content:' ( odd 홀수 - 이전 ) '; background-color: #faa; } li:nth-child(even):after{ /* 짝수번째 모두 */ content:' ( even 짝수 -이후 ) '; background-color: #afa; } li:nth-child(odd){ /* 홀수번째 모두 */ color: chocolate; } li:nth-child(5n):before{ /* 5의 배수 번째 모두 */ content:'5의배수- ' } li:nth-child(-n+3){ /* 앞에서 1번부터 3번까지 */ background-color: red } li:nth-child(n+25){ /* 20번부터 끝까지 */ background-color: green } li:nth-child(n+5):nth-child(-n+8){ /* 앞에서 2번부터 4번 전까지 */ background-color: gold; } </style> </head> <body> <p><br/></p> <div class="container"> <div> <%-- HelloWord --%> HelloWord <ul> <% for(int i =1; i<=30; i++){ out.println("<li> <span class='Numcheck'>"+i+"</span> </li>"); } %> </ul> </div> </div> <p><br/></p> </body> </html> | cs |
java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | package jspEducation.task; import java.io.IOException; import java.io.PrintWriter; import java.util.function.Function; 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 task1025 */ @WebServlet( {"/task1025" , "/Task1025" , "/TASK1025" , "/과제1025"}) public class task1025 extends HttpServlet { PrintWriter out; void createScript(PrintWriter outA) { out.println(" <script>"); out.println(" 'use strict';"); out.println(" "); out.println(" $(function() {"); out.println(" let product = $(\"#product\").val();"); out.println(""); out.println(" alert(\"상품명 : \" + product);"); out.println(" });"); out.println("$(function() {"); out.println(" "); out.println(" $(\"#calcBtn\").click(function() {"); out.println(" let product = $(\"#product\").val();"); out.println(" let price = $(\"#price\").val();"); outA.println(" let su = $(\"#su\").val();"); outA.println(" "); outA.println(" if(isNaN(price) || isNaN(su)) {"); outA.println(" alert(\"가격과 수량은 숫자로 입력하세요!\");"); outA.println(" $(\"#price\").select();"); outA.println(" $(\"#price\").focus();"); outA.println(" return;"); outA.println(" }"); outA.println(" let res = price * su;"); outA.println(" //demo.innerHTML = product + \"의 가격은? \" + res;"); outA.println(" let str = '<div class=\"colorblue\">'+product + \"의 가격은? \" + res+'</div>';"); outA.println(" // $(\"#demo\").html(str);"); outA.println(" $(\"#demo\").html(str);"); outA.println(" });"); outA.println("});"); outA.println(" </script>"); } void createStyle(PrintWriter outA) { outA.println(" <style>"); outA.println(".colorblue{"); outA.println("background-color: #aaf;"); outA.println("display: inline-block;"); outA.println("}"); outA.println("</style>"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=utf-8;"); response.getWriter().append("Served at: ").append(request.getContextPath()); out = response.getWriter(); out.println("<!DOCTYPE html>"); out.println("<html lang='ko'>"); out.println("<head>"); out.println(" <meta charset='UTF-8'>"); out.println(" <meta name='viewport' content='width=device-width, initial-scale=1.0'>"); out.println(" <title>test1_선언.html</title>"); out.println(" <link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css'>"); out.println(" <script src='https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js'></script>"); out.println(" <script src=\"https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js\"></script>"); out.println(" <script src=\"https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js\"></script>"); out.println(" <link rel=\"stylesheet\" href=\"https://www.w3schools.com/w3css/4/w3.css\">"); createScript(out); createStyle(out); out.println("</head>"); out.println("<body>"); out.println("<p><br/></p>"); out.println("<div class='container'>"); out.println(" <h2>상품가격 계산하기</h2>"); out.println(" <form name='myform'>"); out.println(" <div>상품명"); out.println(" <input type='text' name='product' id=\"product\" value=\"냉장고\" autofocus class=\"form-control mb-3\" />"); out.println(" </div>"); out.println(" <div>상품가격"); out.println(" <input type=\"text\" name=\"price\" id=\"price\" value=\"1000\" class=\"form-control mb-3\" />"); out.println(" </div>"); out.println(" <div>구매수량"); out.println(" <input type=\"text\" name=\"su\" id=\"su\" value=\"2\" class=\" form-control mb-3\" />"); out.println(" </div>"); out.println(" <div>"); out.println(" <input type=\"button\" value=\"계산하기\" id=\"calcBtn\" class=\"btn-success form-control\" />"); out.println(" </div>"); out.println(" </form>"); out.println(" <hr/>"); out.println(" <div id=\"demo\"></div>"); out.println("</div>"); out.println("</body>"); out.println("</html>"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } | cs |
첫댓글 내용 잘 봤습니다. 수고하셨습니다.