liguofeng29’s blog

個人勉強用ブログだっす。

JSP - responseオブジェクト - response.getOutputStream()

 クライアントへの応答する際に使用するオブジェクト。

 

1. 非文字コンテンツの応答

2. リダイレクト

3. cookie追加 

 

通常はoutオブジェクトを使用するが、非キャラコンテンツは応答できない。

 
主なメソッド説明
getOutputStream() byteのoutputストリーム
sendRedirect(String location) リダイレクト

この際には新しいリクエストになるので、

1. リクエストパラメータは継がない

2. URLはリダイレクト後のURLになる

 
getImage.jsp
<%@ page contentType="text/html;charset=Shift_jis" language="java" %>
<html>
<head>
<title>get image.</title>
</head>
<body>
<img src="img.jsp" />
</body>
</html>
 
 
img.jsp
<%@ page contentType="image/png" language="java" %>
<%@ page import="java.awt.image.*,javax.imageio.*,java.io.*,java.awt.*" %>
 
<html>
<head>
<title>return a image.</title>
</head>
<body>
<%
// create BuffredImage
BufferedImage image = new BufferedImage(340, 160, BufferedImage.TYPE_INT_RGB);
 
// get Grapphics
Graphics g = image.getGraphics();
// draw
g.fillRect(0, 0, 400, 400);
g.setColor(new Color(255, 0, 0));
g.fillArc(20, 20, 100, 100, 30, 120);
g.setColor(new Color(0, 255, 0));
g.fillArc(20, 20, 100, 100, 150, 120);
g.setColor(new Color(0, 0, 255));
g.fillArc(20, 20, 100, 100, 270, 120);
 
// draw string
g.setColor(new Color(0, 0, 0));
g.setFont(new Font("Arial Black", Font.PLAIN, 16));
g.drawString("draw string1", 200, 60);
g.drawString("draw string2", 200, 100);
 
g.dispose();
// output
ImageIO.write(image, "png", response.getOutputStream());
%>
</body>
</html>
 
 

URL:http://localhost:8080/webDemo/getImage.jsp