Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Page can't displayed - problem of configuring error location with servlet name in DD

 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am learning how to set to set servlet name in the location of
<error-page> in web.xml, I do not why I get page 'Page Can not displayed ' when I invoked
http://localhost:8080/myWeb/practicse/error2

Here's my snippet

========== ErrorTest2.java
public void doGet( HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
System.out.println("In ErrorTest2 class, throw ServletException exception.");
throw new IOException("wrong match");
}

========== MyErrorServlet2.java
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
String msg = (String) req.getAttribute ("javax.servlet.error.message");
System.out.println(" my error message is: " + msg );
PrintWriter out = res.getWriter();
res.setContentType("text/html");
out.println("<html><body>");
out.println("<br>Ha, error message is: " + msg );
out.println("</body></html>");

}
========== web.xml ================
<servlet>
<servlet-name>ET202</servlet-name>
<servlet-class>coreservlets.ErrorTest2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ET202</servlet-name>
<url-pattern>/practicse/error2</url-pattern>
</servlet-mapping>
<error-page>
<exception-type>java.io.IOException</exception-type>
<location>/servlet/MyErrorServlet2</location> <!-- correct?? -->
</error-page>
<servlet>
<servlet-name>MyErrorServlet2</servlet-name><!-- correct ?? -->
<servlet-class>coreservlets.MyErrorServlet2</servlet-class>
</servlet>
=============

Anybody can help?

Thanks in advance!
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are missing the servlet-mapping for MyErrorServlet2. Try adding:

<servlet-mapping>
<servlet-name>MyErrorServlet2</servlet-name>
<url-pattern>/servlet/MyErrorServlet2</url-pattern>
</servlet-mapping>
 
Grace Yang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's still not working withe servelt mapping.

I am interested in this questions too. However | don't why:-(
 
Atul Sawant
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to make a jsp page and use it as an error page instead of using a servlet and check if it is going well.

I think generally it practised to have an error page NOT to be a servlet.
 
Grace Yang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using a jsp page seems no problem.

But what's wrong inside the web.xml causing '.. can not displayed page'??

Anyboby know?
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it myself and it works just fine.

Here is a copy of my web.xml:



I changed the names a little, but in the end it the same web.xml.
 
Grace Yang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know it's working using a jsp page, but I am interested in knowing technically how to put correct servlet name inside <location> .. </location> to display the error contents inside the servlet.

I'd like to know what's wrong in the original Tiffny's posting of using a servlet in the <location> to handle the error?


thanks
 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any body knows MyErrorServlet2 servelt is invoked, but browser gives me '..can not be displayed'?

Thanks
 
Mirko Bonasorte
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using JBoss and jsp as error pages, I discovered it uses RequestDispatcher.forward() to reach the error page, which should work with any web resource (considering that JBoss internally uses Tomcat 4.1, maybe it's the same with Apache tomcat standalone).
However, I'm not sure but I seem to remember that it's not a mandatory behaviour: the Application Server may send a 'sendRedirect()' to the client.
Can your browser directly access to your error page?
 
Mirko Bonasorte
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to use a servlet as an error page and everything went ok.

----> web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>Schiop</servlet-name>
<servlet-class>myservlet.Schiop</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Schiop</servlet-name>
<url-pattern>/schiop/*</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>ErrorManager</servlet-name>
<servlet-class>myservlet.ErrorManager</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ErrorManager</servlet-name>
<url-pattern>/error/*</url-pattern>
</servlet-mapping>
<error-page>
<exception-type>java.io.IOException</exception-type>
<location>/error</location>
</error-page>
</web-app>
<---- web.xml

----> ErrorManager.java:

package myservlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class ErrorManager extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter out = res.getWriter();

out.println("<html><body>pagina di errore</body></html>");
}
}
<---- ErrorManager.java


----> Schiop.java
package myservlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class Schiop extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
throw new IOException("Tonka!");
}
}
<---- Schiop.java

Deployment structure:

tomcat/webapps/prova/WEB-INF/web.xml
tomcat/webapps/prova/WEB-INF/classes/ErrorManager.class
tomcat/webapps/prova/WEB-INF/classes/Schiop.class
 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mirko Bonasorte !!

I got clue from your post. I missed servlet mapping part which causing '.. website can not be displayed.'

It works now, I feel great, thanks all bodies who replied agian.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic