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

  • SEARCH
  • Home
  • 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 8676867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:18:26+00:00 2026-06-12T20:18:26+00:00

In my Java servlet, there are instances where I want to return an HTTP

  • 0

In my Java servlet, there are instances where I want to return an HTTP status code signifying an error and I want the client to be able to read and handle the response based on the status code returned. For example, my application deals with the Channel API for the Google App Engine Java runtime. If the requesting client ID has not yet requested a token for the channel, then the servlet will return a 403 status code and the client will handle the response accordingly (request a token immediately from the servlet).

However, when the client reads the HTTP response using HttpURLConnection. Instead of being able to read the response from the input stream, I am thrown this lovely stack trace:

java.io.IOException: Server returned HTTP response code: 403 for URL: http://localhost:8083/rest/fb/echo
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1458)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1452)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1106)
...
Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: http://localhost:8083/rest/fb/echo
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1403)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:397)

While I was writing this post, I began to think that perhaps I should not read from the connection’s input stream if I read a status code from the connection that signified a non-success. Regardless, I’d love to hear what others think about this.

NOTE

My servlet uses Sitebricks to provide my methods with a REST endpoint to listen to. Also, Sitebricks allows me to send the response and status code like so:

MyObject response = ...;
int statusCode = ...;
...
return Reply.with(response).as(Json.class).type("application/json").status(statusCode);
  • 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-06-12T20:18:27+00:00Added an answer on June 12, 2026 at 8:18 pm

    While I was writing this post, I began to think that perhaps I should
    not read from the connection’s input stream if I read a status code
    from the connection that signified a non-success. Regardless, I’d love
    to hear what others think about this.

    I do think you’re right. You’ll have to check status before reading from input stream.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way that I could embed JFreeChart into Java Servlet with the
In a Java Servlet I want to check programmatically whether a user is logged
I'm implementing a long poll http connection using java servlet. How can I know
While I was reading the book - O'Reilly Java Servlet Programming. There was a
Suppose I have a Java Servlet that takes a while to finish computing it's
I have a Java servlet that receives data from an upstream system via a
I have developed Java Servlet on Netbeans IDE and now wish to deploy them
I have a Java Servlet, which is handling a REST request. However, this is
I have this java servlet that grabs information from a form, I need to
I'm writing a Java servlet in Eclipse (to be hosted on Google App Engine)

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.