JSP Response Set Status
In this tutorial you'll learn about how to set the HTTP status of code in JSP.
In Java you can specify a http-specific functionality regarding to send response. An interface HttpServletResponse provides these facility. This interface provides some static final fields to set the status which is indicating their respective status code, Thes E codes is used for indicating their respective operations. There is number of fields is defined some of them fields value with their status code is given here:
Field ' s Name |
Status Code |
sc_accepted |
Status Code (202) which shows a request is admitted for processing, but could is completed. |
Sc_bad_gateway |
Status Code (502) which shows HTTP server got an invalid response from a server it looked up when acting as a proxy or GAT eWAY. |
Sc_bad_request |
Status code (which) shows a request sent by the client is syntactically incorrect. |
Sc_conflict |
Status Code (409) which shows a request could not being completed due to a conflict with the current state of the resource. |
Sc_continue |
Status code (which) shows a client can continue. |
sc_created |
Status Code (201) which shows a request succeeded and created a new resource on the server. |
Sc_expectation_failed |
Status Code (417) which shows the server could not meet the expectation given in the Expect request header. |
Sc_forbidden |
Status Code (403) which shows the server understood the request but refused to fulfill it. |
Sc_found |
Status Code (302) which shows a resource reside temporarily under a different URI. |
Sc_gateway_timeout |
Status Code (504) which shows the server did not receive a timely response from the upstream server while acting as a gate or proxy. |
Sc_gone |
Status Code (410) which shows the resource is no longer available at the server and no forwarding address is known. |
sc_http_version_not_supported |
Status Code (505) which shows the server does not support or refuses to support the HTTP protocol version that is used in The request message. |
Sc_internal_server_error |
Status code which shows an error inside the HTTP server which prevented it from fulfilling the request. |
Sc_length_required |
Status Code (411) which shows a request cannot be handled without a defined content-length. |
sc_moved_permanently |
Status Code (301) which shows a resource have permanently moved to a new location, and so future references should use a The new URI with their requests. |
Sc_moved_temporarily |
Status Code (302) which shows a resource have temporarily moved to another location, and that's future references should stil L Use the original URI to access the resource. |
Sc_not_found |
Status code (404) which shows a requested resource is not available. |
sc_not_implemented |
Status Code (501) which shows the HTTP server does not support the functionality needed to fulfill the request. |
Sc_request_timeout |
Status Code (408) which shows a client did not produce a request within the time, the server was prepared to wait. |
Sc_request_uri_too_long |
Status code (414) which shows the server is refusing to service the request because the Request-uri the SER longer than Ver is willing to interpret. |
Sc_use_proxy |
Status Code (305) which shows a requested resource must is accessed through the proxy given by the location field. |
Above mentioned statuses can be set using SetStatus () method of HttpServletResponse interface.
Syntax of SetStatus () method
void setStatus(int sc)
Argument of this method was a specified field name given in the above table.
Example:
Here I am giving a simple example which would demonstrate you on how to set the HTTP status code. In this example I has created a JSP page where designed a form to take input from the user. In the first input textbox, name of the website (e.g. Google, Facebook, Orkut) has the to is given and in the second input Te Xtbox domain (like COM, in. uk) of that website has the to is given and taken a submit button to submit a form. In further steps used Scriptlet to write the Java code where I had gotten both the values provided in the textboxes and C reated a new URL using URL class of java.net package and converted the newly created URL to a String because in the next Steps where I had used the SetHeader () method to set the response header takes String value only. Then I had used the SetStatus () method of HttpServletResponse interface to set the status and gave the value response. Sc_moved_temporarily (moves resource to another location) as a argument and also used the SetHeader () method toSet the header value. First argument of this method is a String and the value ' location ' is a common response header which specifies where the C Lient should go to get document.
jspsetstatus.jsp
<%@Page language="Java"ContentType="Text/html; Charset=iso-8859-1 "
Pageencoding="Iso-8859-1"%>
<%@PageImport="Java.net.URL, java.util.*" %>
<!DOCTYPE HTML Public"-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta HTTP-Equiv="Content-type"Content="Text/html; Charset=iso-8859-1 ">
<Title>JSP HTTP SetStatus</Title>
</Head>
<Body>
<Form>
<Table>
<Tr>
<Td>EnterWebsite‘s name only: <Br>
(E.G.Devmanuals,Google) </Td>
<Td><input type="Text"Name="Webadd"/></Td>
</Tr>
<Tr>
<Td>EnterDomain of website: <Br>
(Like com,Net,Inch) </Td>
<Td><input type="Text"Name="Domain"/></Td>
</Tr>
<Tr>
<Td></Td><Td><input type="Submit"Value="Submit"/></Td>
</Tr>
</Table>
</Form>
<%
StringWebaddress=Request.GetParameter("Webadd");
StringDom=Request.GetParameter("Domain");
URL Newurl= NewUrl("HTTP//"+Webaddress+"."+Dom);
StringUrl=Newurl.Tostring();
If(Webaddress!= Null)
{
//resource would be moved to Another location
Response. Setstatus (response. Sc_moved_temporarily
Response. ( "location" , URL
}
%>
Span class= "pun" ></body>
</ html>
Output:
When you'll execute the above JSP page output'll be as follows:
1. At first, a page'll is displayed to input the values in the textboxes.
2. When you'll click a Submit button, resource is moved to that website (if available).
JSP Response Set Status