Friday 22 August 2014

sendError (int sc, String msg)

public void sendError(int sc, String msg) throws IOException
Sends an error response to the client using the specified status and message provided in the argument and clears the buffer. If the response was committed before this method call, then IllegalStateException thrown. The server will preserve cookies and may clear or update any headers needed to serve the error page as a valid response.

Main.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   
     <title>send Error</title>
    </head>
    <body>
        <form method="get" action="/servlet/SendError">
            <input type="submit" value ="Get Data">
        </form>
    </body>
</html>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>SendError</servlet-name>
        <servlet-class>SendError</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>SendError</servlet-name>
        <url-pattern>/SendError</url-pattern>
    </servlet-mapping>
</web-app>

SendError.java
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 SendError extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{    
        res.sendError(404, "Requested resource not found");
    }   
}

Output




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment