본문 바로가기
Java/시나리오 코드

JSP를 이용한 inch 변환기

by 개발자공부 2024. 7. 3.

이용한 요소

- html header, body, footer 등 file include

- DecimalFormat 클래스 이용

- String 타입을 long 타입으로 변환

+) try catch 를 이용한 방어적 코드도 작성해보기

더보기
더보기
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <header>
        <h1>인치 변환기</h1>
        <div class="h_util">
            <a href="form_page.jsp">홈</a>
            <a href="">로그인</a>
        </div>
    </header>
더보기
더보기
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@ include file = "header.jsp" %>
    <section>
    <div>    
        <p>cm를 인치로 변환</p>
        <form action="result.jsp">
            <label for="cm">cm:</label>
            <input type="number" id="cm" name="cm" required="required">
            <button type="submit">변환</button>
        </form>
    </div>
    </section>
더보기
더보기
<%@page import="java.math.BigDecimal"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.text.DecimalFormat"%>
<%@page import="javax.swing.text.NumberFormatter"%>
<%@ include file = "header.jsp" %>
    <section class="section2">
    <div>
        <p>변환 결과</p>
        <p> <% long cm = (Long.parseLong(request.getParameter("cm")));
        	double inch = 0.393701;
        	
        	double result = cm*inch;
        	
        	DecimalFormat df = new DecimalFormat("###,###,###.##");
        	String format = df.format(result);
        	
        	out.println(cm+"cm의 인치는 "+format+"입니다.");
        %></p>
    </div>
    </section>
더보기
더보기
@charset "utf-8";

*{
    padding: 0;
    margin: 0;
}

body{
    background-color: #eee;
}

header{
    background-color: #000;
    display: flex;
    justify-content: space-between;
    height:  8vh;
    padding: 10px 5px 0;
    box-sizing: border-box;
}

h1{
    color: #fff;
    font-size: 24px;
}

.h_util{
    display: flex;
    padding-top: 8px;
}

.h_util a{
    display: block;
    text-decoration: none;
    color: #fff;
    margin-left: 10px;
}

section{
	display: flex;
	height: 100vh;
    flex-wrap: wrap;
	justify-content: center;
	align-items: center;
}

section div{
    background-color: #fff;
    width: 300px;
    text-align: center;
	padding: 20px;
	border-radius: 10px;
}

.section2 div{
	width:500px;
}

p{
	font-size:24px;
	font-weight: bold;
	margin-bottom: 20px;
}

form{
	display: flex;
	flex-direction: column;
}

label{
	text-align: left;
}

input{
	margin: 3px 0 15px;
}

button{
	display:block;
    width: 100%;
    border: none;
    padding: 5px 0;
	border-radius: 10px;
	font-weight: bold;
}

button:hover{
	color: #fff;
	background-color: #333;
}

실행결과

 

'Java > 시나리오 코드' 카테고리의 다른 글

파일전송  (0) 2024.08.01
JSP 세션 무효화  (0) 2024.07.07
JSP 구구단  (0) 2024.07.07
JSP 폼으로 결과 값 여러 개 받기  (0) 2024.07.04
연습 - 로또 게임  (0) 2024.04.19