Header set content type java

Java HttpHeaders CONTENT_TYPE

The field CONTENT_TYPE() from HttpHeaders is declared as:

public static final String CONTENT_TYPE = "Content-Type"; 

Example

The following code shows how to use HttpHeaders from javax.ws.rs.core.

Specifically, the code shows you how to use Java HttpHeaders.CONTENT_TYPE.

import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; public abstract class BaseController < public static Response buildResponse(Object entity) < return Response.ok(entity).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON + "; charset=utf-8") .build(); > >
import javax.ws.rs.client.ClientRequestContext; import javax.ws.rs.client.ClientResponseContext; import javax.ws.rs.client.ClientResponseFilter; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import java.io.IOException; class FixHeadersClientResponseFilter implements ClientResponseFilter < @Override public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException < responseContext.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); > >
import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; /** * Open Tenure exception object */ public class OTRestException extends WebApplicationException < public OTRestException(int statusCode, String message) < super(Response.status(statusCode).entity(message) .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON + "; charset=UTF-8").build()); > >

demo2s.com | Email: | Demo Source and Support. All rights reserved.

Источник

HttpResponse

The HttpServlet class request processing methods take two parameters.

For instance, here is the signature of the HttpServlet.doGet() method:

protected void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

In this text I will look at the HttpResponse object.

The purpose of the HttpResponse object is to represent the HTTP response your web application sends back to the browser, in response to the HTTP request the browser send to your web application.

The HttpResponse object has a lot of methods, so I will just cover the most commonly used here. The rest you can read about in the JavaDoc, if you are interested.

Writing HTML

To send HTML back to the browser, you have to obtain the a PrintWriter from the HttpResponse object. Here is how:

PrintWriter writer = response.getWriter(); writer.write("GET/POST response");

Headers

Just like the request object, the HttpRequest can contain HTTP headers. Headers must be set before any data is written to the response. You set a header on the response object like this:

response.setHeader("Header-Name", "Header Value");

As you can see, a response header is a name, value pair.

Content-Type

The Content-Type header is a response header that tells the browser the type of the content you are sending back to it. For instance, the content type for HTML is text/html . Similarly, if what you send back to the browser is plain text, you use the content type text/plain .

Here is how you set the Content-Type header on the HttpResponse object:

response.setHeader("Content-Type", "text/html");

Writing Text

You can write text back to the browser instead of HTML, like this:

response.setHeader("Content-Type", "text/plain"); PrintWriter writer = response.getWriter(); writer.write("This is just plain text");

First the Content-Type header is set to text/plain . Then a plain text string is written to the writer obtained from the response object.

Content-Length

The Content-Length header tells the browser how many bytes your servlet is sending back. If you are sending binary data back you need to set the content length header. Here is how:

response.setHeader("Content-Length", "31642");

Writing Binary Data

You can also write binary data back to the browser instead of text. For instance, you can send an image back, a PDF file or a Flash file or something like that.

Again, you will first have to set the Content-Type header to the type matching the data you are sending back. For instance, the content type for a PNG image is image/png .

You can search for «mime types» in your favourite search engine to find a list of mime types (content types), so you can find the mime type for the content you are sending back.

In order to write binary data back to the browser you cannot use the Writer obtained from response.getWriter() . Afterall, Writer ‘s are intended for text.

Instead you have to use the OutputStream obtained from the response.getOutputStream() method. Here is how:

OutputStream outputStream = response.getOutputStream(); outputStream.write(. );

Redirecting to a Different URL

You can redirect the browser to a different URL from your servlet. You cannot send any data back to the browser when redirecting. Here is how you redirect:

response.sendRedirect("http://jenkov.com");

Источник

HttpResponse

The HttpServlet class request processing methods take two parameters.

For instance, here is the signature of the HttpServlet.doGet() method:

protected void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

In this text I will look at the HttpResponse object.

The purpose of the HttpResponse object is to represent the HTTP response your web application sends back to the browser, in response to the HTTP request the browser send to your web application.

The HttpResponse object has a lot of methods, so I will just cover the most commonly used here. The rest you can read about in the JavaDoc, if you are interested.

Writing HTML

To send HTML back to the browser, you have to obtain the a PrintWriter from the HttpResponse object. Here is how:

PrintWriter writer = response.getWriter(); writer.write("GET/POST response");

Headers

Just like the request object, the HttpRequest can contain HTTP headers. Headers must be set before any data is written to the response. You set a header on the response object like this:

response.setHeader("Header-Name", "Header Value");

As you can see, a response header is a name, value pair.

Content-Type

The Content-Type header is a response header that tells the browser the type of the content you are sending back to it. For instance, the content type for HTML is text/html . Similarly, if what you send back to the browser is plain text, you use the content type text/plain .

Here is how you set the Content-Type header on the HttpResponse object:

response.setHeader("Content-Type", "text/html");

Writing Text

You can write text back to the browser instead of HTML, like this:

response.setHeader("Content-Type", "text/plain"); PrintWriter writer = response.getWriter(); writer.write("This is just plain text");

First the Content-Type header is set to text/plain . Then a plain text string is written to the writer obtained from the response object.

Content-Length

The Content-Length header tells the browser how many bytes your servlet is sending back. If you are sending binary data back you need to set the content length header. Here is how:

response.setHeader("Content-Length", "31642");

Writing Binary Data

You can also write binary data back to the browser instead of text. For instance, you can send an image back, a PDF file or a Flash file or something like that.

Again, you will first have to set the Content-Type header to the type matching the data you are sending back. For instance, the content type for a PNG image is image/png .

You can search for «mime types» in your favourite search engine to find a list of mime types (content types), so you can find the mime type for the content you are sending back.

In order to write binary data back to the browser you cannot use the Writer obtained from response.getWriter() . Afterall, Writer ‘s are intended for text.

Instead you have to use the OutputStream obtained from the response.getOutputStream() method. Here is how:

OutputStream outputStream = response.getOutputStream(); outputStream.write(. );

Redirecting to a Different URL

You can redirect the browser to a different URL from your servlet. You cannot send any data back to the browser when redirecting. Here is how you redirect:

response.sendRedirect("http://jenkov.com");

Источник

Читайте также:  Php байты в строку
Оцените статью