Example usage for javax.servlet ServletException ServletException

List of usage examples for javax.servlet ServletException ServletException

Introduction

In this page you can find the example usage for javax.servlet ServletException ServletException.

Prototype


public ServletException(Throwable rootCause) 

Source Link

Document

Constructs a new servlet exception when the servlet needs to throw an exception and include a message about the "root cause" exception that interfered with its normal operation.

Usage

From source file:com.ixcode.framework.web.struts.StrutsHandler.java

/**
 * Struts returns null if the forward name is not found so this wraps it to make sure we get an exception.
 *//*from  w w w . j  a va  2 s .  c  om*/
public static ActionForward findForward(ActionMapping mapping, String name) throws ServletException {
    ActionForward forward = mapping.findForward(name);
    if (forward == null) {
        if (log.isDebugEnabled()) {
            log.debug("<findForward> : Could not find a forward with the name '" + name + "'");
        }
        throw new ServletException(
                "Could not find the action mapping with name '" + name + "' in struts config ActionMappings");
    }

    if (log.isDebugEnabled()) {
        log.debug("<findForward> : forward name = '" + name + "', returning a forward url of '"
                + forward.getPath() + "'");
    }
    return forward;
}

From source file:MyServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException {

    RequestDispatcher dispatcher = null;
    String param = request.getParameter("go");
    if (param == null)
        throw new ServletException("Missing parameter in Controller.");
    else if (param.equals("weather"))
        dispatcher = getServletContext().getNamedDispatcher("Weather");
    else if (param.equals("maps"))
        dispatcher = getServletContext().getNamedDispatcher("Maps");
    else/*  ww w  .  j  a  v  a 2  s .c  o  m*/
        throw new ServletException("Improper parameter passed to Controller.");
    /*check for a null dispatcher, then 
    dispatch the request to the correct URL*/
    if (dispatcher != null)
        dispatcher.forward(request, response);
    else
        throw new ServletException("Controller received a null dispatcher.");
}

From source file:MyServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException {

    //redirect the user depending on the value of the 'go' param
    String destination = getInitParameter("go");
    String contextPath = request.getContextPath();

    if (destination == null || destination.equals(""))
        throw new ServletException("Missing or invalid 'go' parameter in " + getClass().getName());

    if (destination.equals("weather"))
        //ensure URL rewriting
        response.sendRedirect(response.encodeRedirectURL(contextPath + "/weather"));

    if (destination.equals("maps"))
        //ensure URL rewriting
        response.sendRedirect(response.encodeRedirectURL(contextPath + "/maps"));
}

From source file:SendMp3.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String fileName = (String) request.getParameter("file");
    if (fileName == null || fileName.equals(""))
        throw new ServletException("Invalid or non-existent file parameter in SendMp3 servlet.");

    if (fileName.indexOf(".mp3") == -1)
        fileName = fileName + ".mp3";

    String mp3Dir = getServletContext().getInitParameter("mp3-dir");
    if (mp3Dir == null || mp3Dir.equals(""))
        throw new ServletException("Invalid or non-existent mp3Dir context-param.");

    ServletOutputStream stream = null;/*w ww  .j  a va  2 s .co  m*/
    BufferedInputStream buf = null;
    try {

        stream = response.getOutputStream();
        File mp3 = new File(mp3Dir + "/" + fileName);

        //set response headers
        response.setContentType("audio/mpeg");

        response.addHeader("Content-Disposition", "attachment; filename=" + fileName);

        response.setContentLength((int) mp3.length());

        FileInputStream input = new FileInputStream(mp3);
        buf = new BufferedInputStream(input);
        int readBytes = 0;
        //read from the file; write to the ServletOutputStream
        while ((readBytes = buf.read()) != -1)
            stream.write(readBytes);
    } catch (IOException ioe) {
        throw new ServletException(ioe.getMessage());
    } finally {
        if (stream != null)
            stream.close();
        if (buf != null)
            buf.close();
    }
}

From source file:XsltDomServlet.java

public void init() throws ServletException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {/*from w  w w  .  ja v a  2  s .c  o m*/
        DocumentBuilder db = dbf.newDocumentBuilder();
        dom = db.getDOMImplementation();
    } catch (ParserConfigurationException pce) {
        throw new ServletException(pce);
    }

    // prepare the XSLT transformer
    TransformerFactory tf = TransformerFactory.newInstance();
    StreamSource ss = new StreamSource("/var/www/stylesheets/paramTable.xslt");
    try {
        transformer = tf.newTransformer(ss);
    } catch (TransformerConfigurationException tce) {
        throw new ServletException(tce);
    }
}

From source file:SendWord.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    //get the 'file' parameter
    String fileName = (String) request.getParameter("file");
    if (fileName == null || fileName.equals(""))
        throw new ServletException("Invalid or non-existent file parameter in SendWord servlet.");

    // add the .doc suffix if it doesn't already exist
    if (fileName.indexOf(".doc") == -1)
        fileName = fileName + ".doc";

    String wordDir = getServletContext().getInitParameter("word-dir");
    if (wordDir == null || wordDir.equals(""))
        throw new ServletException("Invalid or non-existent wordDir context-param.");
    ServletOutputStream stream = null;/*w w w. j  a  va 2 s . com*/
    BufferedInputStream buf = null;
    try {
        stream = response.getOutputStream();
        File doc = new File(wordDir + "/" + fileName);
        response.setContentType("application/msword");
        response.addHeader("Content-Disposition", "attachment; filename=" + fileName);
        response.setContentLength((int) doc.length());
        FileInputStream input = new FileInputStream(doc);
        buf = new BufferedInputStream(input);
        int readBytes = 0;
        while ((readBytes = buf.read()) != -1)
            stream.write(readBytes);
    } catch (IOException ioe) {
        throw new ServletException(ioe.getMessage());
    } finally {
        if (stream != null)
            stream.close();
        if (buf != null)
            buf.close();
    }
}

From source file:EmailJndiServlet.java

public void init() throws ServletException {

    Context env = null;//from w w  w.ja v  a 2 s  .c om

    try {

        env = (Context) new InitialContext();

        mailSession = (Session) env.lookup("MyEmail");

        if (mailSession == null)
            throw new ServletException("MyEmail is an unknown JNDI object");

        //close the InitialContext
        env.close();

    } catch (NamingException ne) {

        try {
            env.close();
        } catch (NamingException nex) {
        }

        throw new ServletException(ne);

    }

}

From source file:SendXml.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    //get the 'file' parameter
    String fileName = (String) request.getParameter("file");
    if (fileName == null || fileName.equals(""))
        throw new ServletException("Invalid or non-existent file parameter in SendXml servlet.");

    // add the .doc suffix if it doesn't already exist
    if (fileName.indexOf(".xml") == -1)
        fileName = fileName + ".xml";

    String xmlDir = getServletContext().getInitParameter("xml-dir");
    if (xmlDir == null || xmlDir.equals(""))
        throw new ServletException("Invalid or non-existent xmlDir context-param.");

    ServletOutputStream stream = null;/*  w  w  w . java 2 s.com*/
    BufferedInputStream buf = null;
    try {

        stream = response.getOutputStream();
        File xml = new File(xmlDir + "/" + fileName);
        response.setContentType("text/xml");
        response.addHeader("Content-Disposition", "attachment; filename=" + fileName);

        response.setContentLength((int) xml.length());
        FileInputStream input = new FileInputStream(xml);
        buf = new BufferedInputStream(input);
        int readBytes = 0;
        while ((readBytes = buf.read()) != -1)
            stream.write(readBytes);
    } catch (IOException ioe) {
        throw new ServletException(ioe.getMessage());
    } finally {
        if (stream != null)
            stream.close();
        if (buf != null)
            buf.close();
    }
}

From source file:UrlServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    //get the 'file' parameter
    String fileName = (String) request.getParameter("file");
    if (fileName == null || fileName.equals(""))
        throw new ServletException("Invalid or non-existent file parameter in UrlServlet servlet.");

    if (fileName.indexOf(".pdf") == -1)
        fileName = fileName + ".pdf";

    URL pdfDir = null;// ww w. j a v a  2  s . c  om
    URLConnection urlConn = null;
    ServletOutputStream stream = null;
    BufferedInputStream buf = null;
    try {

        pdfDir = new URL(getServletContext().getInitParameter("remote-pdf-dir") + fileName);

    } catch (MalformedURLException mue) {

        throw new ServletException(mue.getMessage());
    }
    try {

        stream = response.getOutputStream();

        //set response headers
        response.setContentType("application/pdf");
        response.addHeader("Content-Disposition", "attachment; filename=" + fileName);

        urlConn = pdfDir.openConnection();
        response.setContentLength((int) urlConn.getContentLength());

        buf = new BufferedInputStream(urlConn.getInputStream());
        int readBytes = 0;

        //read from the file; write to the ServletOutputStream
        while ((readBytes = buf.read()) != -1)
            stream.write(readBytes);
    } catch (IOException ioe) {
        throw new ServletException(ioe.getMessage());
    } finally {
        if (stream != null)
            stream.close();
        if (buf != null)
            buf.close();
    }
}

From source file:JndiFilter.java

public void init(FilterConfig filterConfig) throws ServletException {

    this.config = filterConfig;

    try {// w ww.  j  av  a 2 s  . c  om

        env = (Context) new InitialContext();

    } catch (NamingException ne) {

        throw new ServletException(ne);

    }

}