📄 calledserviceservlet.java
字号:
/*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* http://www.opensource.org/licenses/cddl1.php
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* http://www.opensource.org/licenses/cddl1.php. If
* applicable, add the following below this CDDL HEADER,
* with the fields enclosed by brackets "[]" replaced
* with your own identifying information:
* Portions Copyright [yyyy]
* [name of copyright owner]
*/
/*
* $(@)CalledServiceServlet.java $Revision: 1.1.1.1 $ $Date: 2006/03/15 13:12:10 $
*
* Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
*/
package com.sun.sjc.idtv.vod.server.comm;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* This is the base class that all servlets that implements services
* (i.e. those servlets that pass objects back and forth) should inherit
* from.
*
* @date October 2000
*/
abstract public class CalledServiceServlet extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
try
{
// Read in the param
int len = request.getContentLength();
System.out.println("++++++++++++++++++++ LEN: " + len);
Serializable param = null;
if (len != 0)
{
InputStream in = new BufferedInputStream(request.getInputStream());
byte[] data = new byte[len];
int remain = len;
while (remain > 0)
{
int num = in.read(data, len-remain, remain);
if (num < 0) throw new IOException();
remain -= num;
}
in.close();
param = (Serializable) new ObjectInputStream(new ByteArrayInputStream(data)).readObject();
}
// Call the business method
Serializable retObj = call(request, response, param);
// Write out the return value
// Be sure there's no caching anywhere
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, max-age=0");
response.setIntHeader("Expires", 0);
if (retObj != null)
{
MyByteArrayOutputStream b_out;
ObjectOutputStream o_out = new ObjectOutputStream(b_out = new MyByteArrayOutputStream());
o_out.writeObject(retObj);
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("application/x-java-serialized-object");
response.setContentLength(b_out.getCount());
//System.out.println("++++++++++++++++++++ RETLEN: " + b_out.getCount());
OutputStream out = response.getOutputStream();
b_out.writeTo(out);
out.flush();
out.close();
}
else
{
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("application/x-java-serialized-object");
response.setContentLength(0);
}
}
catch (IOException e)
{
System.out.println("CSS in exception: " + e);
e.printStackTrace();
throw e;
}
catch (Exception e)
{
e.printStackTrace();
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.setContentType("application/x-java-serialized-object");
response.setContentLength(0);
}
}
public abstract Serializable call(HttpServletRequest request, HttpServletResponse response, Serializable param)
throws Exception;
// I created this class because I'm not sure that the size() method
// will return the exact size of the data under all conditions. -- spt
public static class MyByteArrayOutputStream extends ByteArrayOutputStream
{
public int getCount() { return count; }
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -