解答例 - 実習課題2 - 9.ウェルカムページとエラーページ
(実習課題2)
Internal Server Errorを送出するサーブレットを作成し、任意のWebアプリケーションに対して、例外に対するエラーページとして設定しなさい。またわざと例外が発生するサーブレットを作成し、設定したサーブレットが動作する事を確認しなさい。
解答例
package com.techscore.servlet.chapter9.exercise2; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServerErrorServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.sendError(HttpServletResponse.SC_HTTP_VERSION_NOT_SUPPORTED); } }
package com.techscore.servlet.chapter9.exercise2; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletErrorServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { throw new ServletException(); } }
<?xml version="1.0" encoding="Shift_JIS"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <servlet> <servlet-name>servlet9-2_505</servlet-name> <servlet-class>com.techscore.servlet.chapter9.exercise2.ServerErrorServlet</servlet-class> </servlet> <servlet> <servlet-name>servlet9-2_servlet</servlet-name> <servlet-class>com.techscore.servlet.chapter9.exercise2.ServletErrorServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>servlet9-2_505</servlet-name> <url-pattern>/servlet9-2_505</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>servlet9-2_servlet</servlet-name> <url-pattern>/servlet9-2_servlet</url-pattern> </servlet-mapping> <error-page> <error-code>505</error-code> <location>/505.jsp</location> </error-page> <error-page> <exception-type>java.lang.Exception</exception-type> <location>/error/error.jsp</location> </error-page> </web-app>