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

Ads
 

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
How do I initialize a byte array in Java?
How do I decompile Java class files?
How do I compare strings in Java?
404 error
404 error
how do i write a java program for this??
How do I do this program? I'm new to Java programming...
how do i provide down a pdf document fecility on my web page using jsp and servlets?
404 error
How do i do the coding for 'leaving a comment' in java
How do I become a data analyst from scratch?
How do I start learning data science from scratch?
How do I write a correct micro-benchmark in Java?
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 read a large file quickly in Java?
How do I upgrade mysql?
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 can i download these java related materials from rose india
How do i get the number of online users from a site using jsp?
how do i make a phone call from my app without quitting my application
How can I do it? .click();

Ads