How do I throw a 404 error from within a java servlet?

How do I throw a 404 error from within a java servlet?

How you can write code in Servlet for throwing the httpd error 404?

How to add the configuration for handling the not found error in the web.xml file?

Thanks

View Answers

July 7, 2014 at 12:28 PM

Hi,

Add the following code in the web.xml file:

<error-page>
  <error-code>404</error-code>
  <location>/WEB-INF/views/notfound.jsp</location>
</error-page>

and in the servlet you can use the following code:

public void doGet(HttpServletRequest request, HttpServletResponse response) {
    response.sendError(HttpServletResponse.SC_NOT_FOUND);
}

Check more Servlet tutorials.

Thanks

Ads









Related Tutorials/Questions & Answers:
How do I throw a 404 error from within a java servlet?
How do I set environment variables from Java?
Advertisements
How do I learn Java programming in one day from zero?
How do I learn Java programming in one day from zero?
how I do select from select in hql
404 error
404 error
404 error
how do i provide down a pdf document fecility on my web page using jsp and servlets?
How do I decompile Java class files?
How do I initialize a byte array in Java?
How do I compare strings in Java?
How do I read a variable from a file in shell script
How do I become a data analyst from scratch?
How do I start learning data science from scratch?
How do i do the coding for 'leaving a comment' in java
How do I do this program? I'm new to Java programming...
how do i write a java program for this??
How do I write a correct micro-benchmark in Java?
How do I upgrade mysql?
How do I handle the reaction of a circle and a semi-circle colliding in java?
How do I resolve this Java Class not found exception?
How do I learn Spring Framework?
How do I learn Spring Framework?
How do I learn Spring Framework?
How do I learn Spring Framework?
How do I learn Spring Framework?
how do i make a phone call from my app without quitting my application
How can I do it? .click();
How do i get the number of online users from a site using jsp?
How can i search and insert within a query

Ads