package examples.servlets;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// imported only to support telling the user that the URL needs an
// argument
import weblogic.html.*;

/**
 * This simple servlet sends back a particular error response.
 *
 * @author Copyright (c)1997 by WebLogic, Inc. All Rights Reserved.
 * @author Copyright (c) 1999-2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class ErrorServlet extends HttpServlet {
  
  public ErrorServlet() {}

  /**
   * Implements the servlet service method.
   */
  public void service(HttpServletRequest req, HttpServletResponse res)
       throws ServletException, IOException
  {
    String scode = null;
    if (req.getParameter("code") != null)
      scode = req.getParameter("code");
    if (scode == null) {
      ServletPage sperror = new ServletPage();
      sperror.getBody()
        .addElement("Add a code to your URL like this:<p><pre> http://WebLogicURL:port/error?code=404</pre>");
      sperror.output(res.getOutputStream());
      return;
    }
    
    ServletOutputStream out = res.getOutputStream();
    int code = Integer.parseInt(req.getParameter("code"));
    res.sendError(code);
  }
  
}
