๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐ŸŒ  ์ž๋ฐ” ์„œ๋ฒ„ ํŽ˜์ด์ง€ JSP/๐ŸŒŒ ์›น \ ์„œ๋ธ”๋ฆฟ Web , Servlet

Web - Servlet ์‹ค์Šต 3 (์ƒํƒœ์ฝ”๋“œ , html ์ฝ”๋“œ๋ฅผ ๋ฐ›์•„ ์„œ๋ฒ„์—์„œ ํ™•์ธํ›„ ์ถœ๋ ฅ)

by Meteora_ 2021. 3. 14.
728x90

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<h1>์ƒํƒœ์ฝ”๋“œ ํ™•์ธ</h1>
<form action="hello" method="get">

<table>

	<tr>
	
		<td>์ƒํƒœ์ฝ”๋“œ</td>
		<td>
		
		<select name = "code">
			<option value = "200">SC_OK</option>
			<option value = "404">SC_NOT_FOUND</option>
			<option value = "500">SC_INTERNAL_SERVER_ERROR</option>
		</select>
		</td>
	</tr>

</table>
<input type = "submit" value = "์ด๋™">

</form>

</body>
</html>

 

HelloServlet

 

package hello;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		//html code ๋ฐ์ดํ„ฐ๋ฅผ html๋กœ ๋‚ ๋ ค์ค€๋‹ค
		
		resp.setContentType("text/html; charset=utf-8");
		
		PrintWriter pw = resp.getWriter();
		
		pw.println("<html>");
		pw.println("<head>");
			pw.println("<title>์ œ๋ชฉ</title>");
		pw.println("</head>");
		
		pw.println("<body>");
		
			pw.println("<h3>HelloServlet</h3>");
			String code = req.getParameter("code");
			if(code.equals("200")) {
				pw.println("<p>200:SC_OK</p>");
				
			}else {
				if(code.equals("404")) {
					resp.sendError(HttpServletResponse.SC_NOT_FOUND, "๋ชป์ฐพ๊ฒ ๋‹ค๋Š” ๋ง์ž…๋‹ˆ๋‹ค");
					
				}else if(code.equals("500")) {
					resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"์ฝ”๋“œ๊ฐ€ ํ‹€๋ ธ๋‹ค๋Š” ์—๋Ÿฌ์ž…๋‹ˆ๋‹ค");
				}
			}
			
		
		pw.println("</body>");
		
		
		
		pw.println("</html>");
		pw.close();
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
	}

}

 

 

XML

 

๋”๋ณด๊ธฐ
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>sample04</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  	<servlet-name>hello</servlet-name>
  	<servlet-class>hello.HelloServlet</servlet-class>
  </servlet>
  	
  
  <servlet-mapping>
  	<servlet-name>hello</servlet-name>
  	<url-pattern>/hello</url-pattern>
  </servlet-mapping>
  
</web-app>

๋Œ“๊ธ€