本文整理汇总了Java中javax.servlet.ServletOutputStream.print方法的典型用法代码示例。如果您正苦于以下问题:Java ServletOutputStream.print方法的具体用法?Java ServletOutputStream.print怎么用?Java ServletOutputStream.print使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.servlet.ServletOutputStream
的用法示例。
在下文中一共展示了ServletOutputStream.print方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doTrace
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
/**
* Called by the server (via the <code>service</code> method)
* to allow a servlet to handle a TRACE request.
*
* A TRACE returns the headers sent with the TRACE
* request to the client, so that they can be used in
* debugging. There's no need to override this method.
*
* @param req the {@link HttpServletRequest} object that
* contains the request the client made of
* the servlet
*
* @param resp the {@link HttpServletResponse} object that
* contains the response the servlet returns
* to the client
*
* @exception IOException if an input or output error occurs
* while the servlet is handling the
* TRACE request
*
* @exception ServletException if the request for the
* TRACE cannot be handled
*/
protected void doTrace(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
int responseLength;
String CRLF = "\r\n";
StringBuilder buffer = new StringBuilder("TRACE ").append(req.getRequestURI())
.append(" ").append(req.getProtocol());
Enumeration<String> reqHeaderEnum = req.getHeaderNames();
while( reqHeaderEnum.hasMoreElements() ) {
String headerName = reqHeaderEnum.nextElement();
buffer.append(CRLF).append(headerName).append(": ")
.append(req.getHeader(headerName));
}
buffer.append(CRLF);
responseLength = buffer.length();
resp.setContentType("message/http");
resp.setContentLength(responseLength);
ServletOutputStream out = resp.getOutputStream();
out.print(buffer.toString());
out.close();
return;
}
示例2: onAuthenticationFailure
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
@Override
public void onAuthenticationFailure (HttpServletRequest request,
HttpServletResponse response, AuthenticationException exception)
throws IOException, ServletException
{
response.setContentType ("text/plain");
response.setStatus (HttpServletResponse.SC_UNAUTHORIZED);
ServletOutputStream outputStream = response.getOutputStream ();
outputStream.print (exception.getMessage ());
outputStream.close ();
}
示例3: service
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
final String path = req.getPathInfo();
if( path.equals("/refresh") )
{
languageService.refreshBundles();
resp.setContentType("text/plain");
ServletOutputStream out = resp.getOutputStream();
out.print("Bundles refreshed");
out.close();
return;
}
Matcher m = Pattern.compile("^/([a-zA-Z_]*)/([a-zA-Z-]+)\\.properties$").matcher(path);
if( !m.matches() || m.groupCount() != 2 )
{
throw new ServletException("Bundle Group name is invalid: " + path);
}
final ResourceBundle resourceBundle = languageService.getResourceBundle(LocaleUtils.parseLocale(m.group(1)),
m.group(2));
final Properties text = new Properties();
for( String key : resourceBundle.keySet() )
{
text.put(key, resourceBundle.getString(key));
}
resp.setContentType("text/plain");
resp.setHeader("Content-Disposition", "inline; filename=" + path + ".properties");
text.store(resp.getOutputStream(), null);
}
示例4: saveNewImage
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
/**
* Save file or url imageGallery item into database.
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws IOException
*/
private ActionForward saveNewImage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws IOException {
ImageGalleryItemForm itemForm = (ImageGalleryItemForm) form;
SessionMap<String, Object> sessionMap = (SessionMap<String, Object>) request.getSession().getAttribute(itemForm.getSessionMapID());
ToolAccessMode mode = (ToolAccessMode) sessionMap.get(AttributeNames.ATTR_MODE);
//validate form
boolean isLargeFilesAllowed = mode.isTeacher();
ActionErrors errors = ImageGalleryUtils.validateImageGalleryItem(itemForm, isLargeFilesAllowed);
try {
if (errors.isEmpty()) {
extractFormToImageGalleryItem(request, itemForm);
}
} catch (Exception e) {
// any upload exception will display as normal error message rather then throw exception directly
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage(ImageGalleryConstants.ERROR_MSG_UPLOAD_FAILED, e.getMessage()));
}
if (!errors.isEmpty()) {
ServletOutputStream outputStream = response.getOutputStream();
outputStream.print(errors.get().next().toString());
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
return null;
}
示例5: saveMultipleImages
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
/**
* Save file or url imageGallery item into database.
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws IOException
*/
private ActionForward saveMultipleImages(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws IOException {
MultipleImagesForm multipleForm = (MultipleImagesForm) form;
SessionMap<String, Object> sessionMap = (SessionMap<String, Object>) request.getSession().getAttribute(multipleForm.getSessionMapID());
ToolAccessMode mode = (ToolAccessMode) sessionMap.get(AttributeNames.ATTR_MODE);
//validate form
boolean isLargeFilesAllowed = mode.isTeacher();
ActionErrors errors = ImageGalleryUtils.validateMultipleImages(multipleForm, isLargeFilesAllowed);
try {
if (errors.isEmpty()) {
extractMultipleFormToImageGalleryItems(request, multipleForm);
}
} catch (Exception e) {
// any upload exception will display as normal error message rather then throw exception directly
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage(ImageGalleryConstants.ERROR_MSG_UPLOAD_FAILED, e.getMessage()));
}
if (!errors.isEmpty()) {
ServletOutputStream outputStream = response.getOutputStream();
outputStream.print(errors.get().next().toString());
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
return null;
}
示例6: testLongProcess
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
/**
* Verify that the test result can be returned correctly even when the
* logic in the method to test takes a long time and thus it verifies that
* the test result is only returned after it has been written in the
* application scope on the server side.
*/
public void testLongProcess() throws Exception
{
ServletOutputStream os = response.getOutputStream();
os.print("<html><head><Long Process></head><body>");
os.flush();
// do some processing that takes a while ...
Thread.sleep(3000);
os.println("Some data</body></html>");
}
示例7: doGet
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String payload = String.format("{\"enabled\": %s}%n", getFilter().isEnabled());
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("application/json");
ServletOutputStream outputStream = response.getOutputStream();
try {
outputStream.print(payload);
} finally {
outputStream.close();
}
}
示例8: doTrace
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
/**
* Called by the server (via the <code>service</code> method) to allow a
* servlet to handle a TRACE request.
*
* A TRACE returns the headers sent with the TRACE request to the client, so
* that they can be used in debugging. There's no need to override this
* method.
*
* @param req
* the {@link HttpServletRequest} object that contains the
* request the client made of the servlet
*
* @param resp
* the {@link HttpServletResponse} object that contains the
* response the servlet returns to the client
*
* @exception IOException
* if an input or output error occurs while the servlet is
* handling the TRACE request
*
* @exception ServletException
* if the request for the TRACE cannot be handled
*/
protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int responseLength;
String CRLF = "\r\n";
StringBuilder buffer = new StringBuilder("TRACE ").append(req.getRequestURI()).append(" ")
.append(req.getProtocol());
Enumeration<String> reqHeaderEnum = req.getHeaderNames();
while (reqHeaderEnum.hasMoreElements()) {
String headerName = reqHeaderEnum.nextElement();
buffer.append(CRLF).append(headerName).append(": ").append(req.getHeader(headerName));
}
buffer.append(CRLF);
responseLength = buffer.length();
resp.setContentType("message/http");
resp.setContentLength(responseLength);
ServletOutputStream out = resp.getOutputStream();
out.print(buffer.toString());
out.close();
return;
}
示例9: copy
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
/**
* Copy the contents of the specified input stream to the specified
* output stream, and ensure that both streams are closed before returning
* (even in the face of an exception).
*
* @param resourceInfo The ResourceInfo object
* @param ostream The output stream to write to
* @param ranges Enumeration of the ranges the client wanted to retrieve
* @param contentType Content type of the resource
* @exception IOException if an input/output error occurs
*/
protected void copy(CacheEntry cacheEntry, ServletOutputStream ostream,
Iterator<Range> ranges, String contentType)
throws IOException {
IOException exception = null;
while ( (exception == null) && (ranges.hasNext()) ) {
InputStream resourceInputStream = cacheEntry.resource.streamContent();
InputStream istream =
new BufferedInputStream(resourceInputStream, input);
Range currentRange = ranges.next();
// Writing MIME header.
ostream.println();
ostream.println("--" + mimeSeparation);
if (contentType != null)
ostream.println("Content-Type: " + contentType);
ostream.println("Content-Range: bytes " + currentRange.start
+ "-" + currentRange.end + "/"
+ currentRange.length);
ostream.println();
// Printing content
exception = copyRange(istream, ostream, currentRange.start,
currentRange.end);
istream.close();
}
ostream.println();
ostream.print("--" + mimeSeparation + "--");
// Rethrow any exception that has occurred
if (exception != null)
throw exception;
}
示例10: copy
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
/**
* Copy the contents of the specified input stream to the specified
* output stream, and ensure that both streams are closed before returning
* (even in the face of an exception).
*
* @param resourceInfo The ResourceInfo object
* @param ostream The output stream to write to
* @param ranges Enumeration of the ranges the client wanted to retrieve
* @param contentType Content type of the resource
* @exception IOException if an input/output error occurs
*/
private void copy(ResourceInfo resourceInfo, ServletOutputStream ostream,
Enumeration ranges, String contentType)
throws IOException {
IOException exception = null;
while ( (exception == null) && (ranges.hasMoreElements()) ) {
InputStream resourceInputStream = resourceInfo.getStream();
InputStream istream = // FIXME: internationalization???????
new BufferedInputStream(resourceInputStream, input);
Range currentRange = (Range) ranges.nextElement();
// Writing MIME header.
ostream.println("--" + mimeSeparation);
if (contentType != null)
ostream.println("Content-Type: " + contentType);
ostream.println("Content-Range: bytes " + currentRange.start
+ "-" + currentRange.end + "/"
+ currentRange.length);
ostream.println();
// Printing content
exception = copyRange(istream, ostream, currentRange.start,
currentRange.end);
try {
istream.close();
} catch (Throwable t) {
;
}
}
ostream.print("--" + mimeSeparation + "--");
// Rethrow any exception that has occurred
if (exception != null)
throw exception;
}