Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6199293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:09:48+00:00 2026-05-24T04:09:48+00:00

I have a java servlet that throws a custom error 403 (Forbidden) when the

  • 0

I have a java servlet that throws a custom error 403 (Forbidden) when the user authenticates with an incorrect user/password.
Java Servlet code:

response.sendError(response.SC_FORBIDDEN, "Login Error: wrong password!");

On the .net side I catch the error via a WebException but when I interrogate the contents of the WebException I don’t see the Login Error custom message unless I do the following:

catch (WebException e)
{
    HttpWebResponse resp = e.Response as HttpWebResponse;
    StreamReader reader = new StreamReader(resp.GetResponseStream());
    string respStr = reader.ReadToEnd();
    reader.Close();
}

The problem with this is that respStr is the HTML string which I would then have to parse to get the “Login Error: wrong password!” string. If I just do a e.Message all I see is “The remote server returned an error: (403) Forbidden.”

Is there an easier way to get the custom message?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:09:49+00:00Added an answer on May 24, 2026 at 4:09 am

    From the servlet side on, this is fully conform the HttpServletResponse#sendError() specification.

    Sends an error response to the client using the specified status and clears the buffer. The server defaults to creating the response to look like an HTML-formatted server error page containing the specified message, setting the content type to “text/html”.

    So there’s really no other way than parsing the HTML yourself in the client (.net) side.


    You could create a custom error page 403.jsp which contains only the exception message

    <%@page isErrorPage="true"%>
    ${exception.message}
    

    and specify that in web.xml as follows:

    <error-page>
        <error-code>403</error-code>
        <location>/403.jsp</location>
    </error-page>
    

    so that the entire response body is just already the whole exception message. But this may be disadvantageous whenever the servlet service is also called by normal webbrowsers. You might want to control the JSP output conditionally based on some request attribute so that it returns either only the message or a fullfledged HTML page.


    Another alternative which I can think of is to set it as a custom response header as well.

    String errorMessage = "Login Error: wrong password!";
    response.setHeader("X-Error-Message", errorMessage);
    response.sendError(HttpServletResponse.SC_FORBIDDEN, errorMessage);
    

    (note that I fixed the static reference to be called on the class rather than the instance)

    On the .net side you do the following to get the custom header:

    catch (WebException e)
    {
        HttpWebResponse resp = e.Response as HttpWebResponse;
        string errorMessage = resp.Headers["X-Error-Message"];
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this java servlet that grabs information from a form, I need to
I have a YUI dialog that submits a form to a Java servlet. The
I have a servlet filter in my Java app to ensure that users are
I have inherited a Java application (servlets) that runs under Tomcat. For historical reasons,
I have a Java servlet which generates XML, translates it with an XSLT stylesheet,
I have a web app built on Java Servlet technology. I am thinking of
I have many years of experience in Java including Swing, Servlet and JDBC, but
I have web application written in java using Eclipse. It has just one servlet
I have java script code to set some of the properties of ajax controls.
I have a Java servlet running on my notebook with Windows Vista, I set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.