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









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
java http 404 status error - JSP-Servlet
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
404 error
How do I do this program? I'm new to Java programming...
Still getting the http 404 error - Java Beginners
"compilation error " & "throw an exception" - Java Beginners
how do i write a java program for this??
servlet7
servlet3
servlet4
servlet5
How do i start to create Download Manager in Java - JSP-Servlet
servlet2
How do I write a correct micro-benchmark in Java?
How do I upgrade mysql?
How do I resolve this Java Class not found exception?
How do I handle the reaction of a circle and a semi-circle colliding in java?
How do i create the node for target SMO in java..???
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 do it? .click();
servlet6
servlet6
loginform giving 404 error
How can i search and insert within a query
how do i make a phone call from my app without quitting my application
How do i get the number of online users from a site using jsp?
How do I read a large file quickly in Java?
how do i solve this problem?
how do i solve this question?
How do servlets work? Instantiation, session variables and multithreading
I developed a simple java web project in Struts now If I have to import the project in Eclipse Indigo how can I do it

Ads