<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/myweb/eng_greeting">
Name : <input type="text" name="name">
<input type="submit" value="전송">
</form>
<hr>
<form action="/myweb/kor_greeting" method="post">
이름 : <input type="text" name="name">
<input type="submit" value="전송">
</form>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
<description></description>
<display-name>GreetingServlet</display-name>
<servlet-name>GreetingServlet</servlet-name>
<servlet-class>servlet.initparam.GreetingServlet</servlet-class>
<init-param>
<description></description>
<param-name>greeting</param-name>
<param-value>How are you</param-value>
</init-param>
<init-param>
<description></description>
<param-name>label</param-name>
<param-value>Greeting : </param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>GreetingServlet</servlet-name>
<url-pattern>/eng_greeting</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>GreetingKorean</servlet-name>
<servlet-class>servlet.initparam.GreetingServlet</servlet-class>
<init-param>
<param-name>label</param-name>
<param-value>인사말 : </param-value>
</init-param>
<init-param>
<param-name>greeting</param-name>
<param-value>안녕하세요...?</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>GreetingKorean</servlet-name>
<url-pattern>/kor_greeting</url-pattern>
</servlet-mapping>
</web-app>
package servlet.initparam;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GreetingServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private String label;
private String greeting;
// public void init(ServletConfig config) throws ServletException {
// super.init(config);
public void init() throws ServletException {
// TODO Auto-generated method stub
label = getInitParameter("label");
greeting = getInitParameter("greeting");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//1. 요청 파라미터 조회
String name = request.getParameter("name");
//2. 응답
out.println("<html><body>");
out.println(label+greeting+" "+name);
out.println("<br><a href='/myweb/initparam/greeting_form.html'>요청</a>");
out.println("</body></html>");
}
}