본문 바로가기

Programming/Servlet & JSP

숫자를 html로 입력받고 구분하여 사진을 servlet에서 보여주기(div, pNum, if, img)

Pokemon.java

package com;

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;

@WebServlet("/Pokemon")
public class Pokemon extends HttpServlet {
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String num = request.getParameter("pokemon");
		int pNum = Integer.parseInt(num);

		response.setCharacterEncoding("EUC-KR");
		PrintWriter out = response.getWriter();

		out.print("<html>");
		out.print("<head>");
		out.print("<style> img{width:1000px; height:1000px;} div{text-align:center;}</style>");
		out.print("</head>");
		out.print("<body>");
		out.print("<div>");
		if (pNum == 1) {
			out.print("<img src = dinosaur.PNG>");
		} else if (pNum == 2) {
			out.print("<img src = turtle.png>");
		} else if (pNum == 3) {
			out.print("<img src = frog.PNG>");
		} else {

		}
		out.print("</div>");
		out.print("</body>");
		out.print("</html>");
	}
}

 

selectNum.html

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>

<style type="text/css">
div {
	width: 100%;
	text-align: center;
}
</style>
</head>
<body>
	<div>
		<h1>원하는 포켓몬을 입력해주세요</h1>
		<br>
		<p>* 하나만 입력할 것</p>
		<p>1. 파이리 2. 꼬부기 3. 이상해씨</p>
		<form action="Pokemon">
			<input type="text" name="pokemon"> <input type="submit"
				value="확인">
		</form>
	</div>
</body>
</html>