web.xmlとサーブレットの連携してサービスを提供します。
WebxmlsampleServlet.java
package webxmlsample;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.http.*;
@SuppressWarnings("serial")
public class WebxmlsampleServlet extends HttpServlet
{
@SuppressWarnings("unchecked")
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException
{
resp.setContentType("text/plain");
resp.getWriter().println("Hello, world");
String param1 = getServletConfig().getInitParameter("param1");
resp.getWriter().println("param1=" + param1);
String key = "";
for (Enumeration<Object> e = getServletConfig().getInitParameterNames(); e.hasMoreElements();)
{
key = e.nextElement().toString();
resp.getWriter().println(key + getServletConfig().getInitParameter(key));
}
}
}
web.xml
<web-app>
<servlet>
<servlet-name>Webxmlsample</servlet-name>
<servlet-class>webxmlsample.WebxmlsampleServlet</servlet-class>
<init-param>
<param-name>param1</param-name>
<param-value>val1</param-value>
</init-param>
<init-param>
<param-name>param2</param-name>
<param-value>val2</param-value>
</init-param>
<init-param>
<param-name>param3</param-name>
<param-value>val3</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Webxmlsample</servlet-name>
<url-pattern>/webxmlsample</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<error-page>
<error-code>500</error-code>
<location>/err/err500.jsp</location>
</error-page>
</web-app>
0 件のコメント:
コメントを投稿