JSP Response Set Status
JSP Response Set Status
In this tutorial you will learn about how to set the HTTP status 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 are indicating their respective status code, these codes are used for indicating their respective operations. There are number of fields are defined some of them fields value with their status code are given here :
| Field's Name | Status Code |
|---|---|
| SC_ACCEPTED | Status code (202) which shows a request was admitted for processing, but could not be 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 gateway. |
| SC_BAD_REQUEST | Status code (400) which shows a request sent by the client is syntactically incorrect. |
| SC_CONFLICT | Status code (409) which shows a request could not be completed due to a conflict with the current state of the resource. |
| SC_CONTINUE | Status code (100) 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 gateway 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 was used in the request message. |
| SC_INTERNAL_SERVER_ERROR | Status code (500) 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 has permanently moved to a new location, and that future references should use a new URI with their requests. |
| SC_MOVED_TEMPORARILY | Status code (302) which shows a resource has temporarily moved to another location, but that future references should still 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 that 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 is longer than the server is willing to interpret. |
| SC_USE_PROXY | Status code (305) which shows a requested resource MUST be 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 is a specified field name given in the above table.
Example :
Here I am giving a simple example which will demonstrate you about how to set the HTTP status code. In this example I have 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) have to be given and in the second input textbox domain (like com, in. uk) of that website have to be given and taken a submit button to submit a form. In further steps used scriptlet to write the java code where I have gotten both the values provided in the textboxes and created a new url using URL class of java.net package and converted the newly created url into a String because in the next steps where I have used the setHeader() method to set the response header takes String value only. Then I have 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 an argument and also used the setHeader() method to set the header value. First argument of this method is a String and the value "location" is a common response header which specifies where the client should go to get document.
jspSetStatus.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="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>Enter website's name only : <br>
(e.g. devmanuals, google)</td>
<td><input type="text" name="webAdd"/></td>
</tr>
<tr>
<td>Enter domain of website : <br>
(like com, net, in)</td>
<td><input type="text" name="domain"/></td>
</tr>
<tr>
<td></td><td><input type="submit" value="submit"/></td>
</tr>
</table>
</form>
<%
String webAddress = request.getParameter("webAdd");
String dom = request.getParameter("domain");
URL newUrl = new URL("http://"+webAddress+"."+dom);
String url = newUrl.toString();
if(webAddress != null)
{
// resource will be moved to another location
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader("Location", url);
}
%>
</body>
</html>
Output :
When you will execute the above JSP page output will be as follows :
1. At first, a page will be displayed to input the values in the textboxes.

2. When you will click a submit button, resource will be moved to that website (if available).

JSP Response Set Status的更多相关文章
- 【FAQ】【JSP】HTTP Status 500 - Summary(问题排查时候应该仔细分析所有的错误打印说明)
Question 1.HTTP Status 500 - Unable to compile class for JSP:'***' cannot be resolved to a type 原因分析 ...
- Jsp——response对象
<%@ page language="java" contentType="text/html; charset=UTF-8" import=" ...
- JSP response.setCharacterEncoding与response.setContentType的区别
问题描述 昨天在参考别人的项目时,发现页面引用js,css等文件总是乱码,后来才发现是MIME类型统一设置为text/html,并且仅仅编码设置了浏览器端的解析编码.另外,可以先通过文本编辑器(如no ...
- jsp response对象
所属接口:javax.servlet.http.HttpServletResponse,其父接口是ServletResponse,而且 ServletResponse也现在只有唯一一个HttpServ ...
- request 和response
当今web程序的开发技术真是百家争鸣,ASP.NET, PHP, JSP,Perl, AJAX 等等. 无论Web技术在未来如何发展,理解Web程序之间通信的基本协议相当重要, 因为它让我们理解了We ...
- Servlet&jsp基础:第二部分
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- HTTP Response Splitting攻击探究 <转>
第一小节:HTTP Basics:使用Proxy软件(例如Webscarab)来截断浏览器(客户端)和Server之间的HTTP通信,之后任意篡改得到预期结果即可. 第二小节:HTTP Splitti ...
- JSP处理AJAX
register.jsp: <script type="text/javascript"> var req; function validate() { var idF ...
- #HTTP协议学习# (一)request 和response 解析
注:本文转自:http://www.cnblogs.com/TankXiao/archive/2012/02/13/2342672.html , 粉字[]内内容为个人笔记 当今web程序的开发技术真是 ...
随机推荐
- [YNOI2017][bzoj4811][luogu3613] 由乃的OJ/睡觉困难综合症 [压位+树链剖分+线段树]
题面 BZOJ题面,比较不清晰 Luogu题面,写的比较清楚 思路 原题目 我们先看这道题的原题目NOI2014起床困难综合症 的确就是上树的带修改版本 那么我们先来解决这个原版的序列上单次询问 二进 ...
- session-cookie 和token登录验证
最近研究了下基于token的身份验证,并将这种机制整合在个人项目中.现在很多网站的认证方式都从传统的seesion+cookie转向token校验.对比传统的校验方式,token确实有更好的扩展性与安 ...
- 洛谷 P3747 [六省联考2017]相逢是问候 解题报告
P3747 [六省联考2017]相逢是问候 题目描述 \(\text {Informatik verbindet dich und mich.}\) 信息将你我连结. \(B\) 君希望以维护一个长度 ...
- iOSCompile
https://wiki.videolan.org/iOSCompile iOSCompile Contents [hide] 1 Development environment 2 Get the ...
- 编写COOL编译器
Coursera上面有Stanford的课程“Compilers”,该课程使用“龙书”作为参考书,并有一个编程项目,完成一个完整的编译器.这个编译器支持的语言称为COOL,是一个面向对象的用于教学的语 ...
- Sqlite插入、修改、删除表里面的数据
转载 2014年05月10日 10:38:21 标签: sqlite3 / 数据库 8688 转自:http://www.cnblogs.com/myqiao/archive/2011/07/13/2 ...
- (十)stm32 GPIO口复用,重映射 RCC_APB2Periph_AFIO
什么时候需要用到RCC_APB2Periph_AFIO--复用IO时钟的使用 需要用到外设的重映射功能时才需要使能AFIO的时钟 外部中断(EXTI)中与AFIO有关的寄存器是AFIO-EXTICR1 ...
- Laravel中setAttribute和queryScope的用法
setAttribute使用场景: 数据在存入数据库的时候需要进行预先处理,每次都会写很多重复代码,使用 setAttribute之后就可以在数据填充时自动完成. setAttribute的写法:se ...
- typescript项目配置路径别名(路径映射)
在vue项目中,我们可以利用“@”来指代src目录,在普通webpack项目中,我们也可以通过配置webpack的config来指定路径别名,但是在typescript+webpack项目中我们该怎么 ...
- POJ 2528.Mayor's posters-线段树(成段替换、离散数据、简单hash)
POJ2528.Mayor's posters 这道题真的是线段数的经典的题目,因为数据很大,直接建树的话肯定不可以,所以需要将数据处理一下,没有接触离散化的时候感觉离散化这个东西相当高级,其实在不知 ...