class DefaultServlet extends HttpServlet
{ // Servlet fuer alle Requests, denen kein
// anderes Servlet zugeordnet ist
public String getServletInfo()
{
return "DefaultServlet/1.0/ServletServer/1.0";
}
protected void doGet(HttpServletRequest req, HttpServletResponse
res)
throws ServletException, IOException
{ // fuer GET, HEAD und POST
try
{
// die gefragte Datei lesen
File file = new File(req.getPathTranslated());
FileInputStream fis = new FileInputStream(
req.getPathTranslated());
int length = (int)file.length();
byte[] data = new byte[length];
fis.read(data);
fis.close();
res.setStatus(200,"OK");
res.setContentLength(length);
res.setContentType(req.getContentType());
// geg. Datei senden; getOutputStream() ruft
SendHeader auf
ServletOutputStream os = res.getOutputStream();
if (! req.getMethod().equals("HEAD"))
os.write(data);
// Verbindung schliessen
os.close();
log(new java.util.Date()+ ", " + req.getPathInfo());
}
catch (IOException e)
{
// Fehlermeldung senden, loggen und Verbindung
schliessen
res.sendError(404,"File Not Found");
log(new Date() + ", 404 File Not Found"
+ req.getPathTranslated());
res.getOutputStream().close();
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse
res)
throws ServletException, IOException
{ // fuer POST
// restliche Sendung ignorieren
try
{
ServletInputStream is = req.getInputStream();
is.skip(req.getContentLength());
}
catch (IOException e)
{}
doGet(req,res);
}
}