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
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?
404 error
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?
java http 404 status error - JSP-Servlet
How do i do the coding for 'leaving a comment' in java
"compilation error " & "throw an exception" - Java Beginners
How do I do this program? I'm new to Java programming...
how do i write a java program for this??
404 error
Still getting the http 404 error - Java Beginners
servlet7
How do i start to create Download Manager in Java - JSP-Servlet
servlet3
servlet4
servlet5
How do I write a correct micro-benchmark in Java?
How do I upgrade mysql?
servlet2
How do i create the node for target SMO 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 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?
servlet6
servlet6
How can i search and insert within a query
How do I read a large file quickly in Java?
loginform giving 404 error
how do i solve this problem?
how do i solve this question?
what should i do next?? - Java Beginners
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