Sevlets « API « Java I/O Q&A





1. Changing Writer File Name    coderanch.com

2. printwriter    coderanch.com

sorry i posted this in multiple forums but this just drives me crazy right now. I can't figure out why i can't get an output. Sorry again for post many times. here is the query. hi i have this method in my code that i wrote to send a message to clients. Now if i change the code to use PrintWriter ...

3. intercepting the response stream    coderanch.com

hello. what i want to do is something like RequestDispatcher.forward method. but i want to change the forward url's result stream before client browser gets it. and then the modified result stream would be sent to client browser. can i set the outputstream of response to memory instead of client socket? i tried to find that kind of method, but there ...

6. skip() method of InputStream???    coderanch.com

7. FileOutputStream    coderanch.com

8. ClassLoader's getResourceAsStream is caching the InputStream object    coderanch.com

public void updateStyleSheets(){ ClassLoader loader = this.getClass().getClassLoader(); TransformerFactory transFact = TransformerFactory.newInstance(); for (int i = 0; i < roles.length ; i++) { String filePath = PATH + roles[i] + ".xsl"; /* InputStream xslIs = loader.getResourceAsStream(filePath);*/ URL url = loader.getResource(filePath); InputStream xslIs= null; Templates templates = null; try{ xslIs = url.openStream(); Source source = new StreamSource(xslIs); templates = transFact.newTemplates(source); xslIs.close(); xslIs = ...

9. contentType, PrintWriter Q?    coderanch.com

Well the getWriter() is not same as sending an output back to the client. i understand the situation where you have all of your out.println() statements and then u set contentType, in which case ur content goes first before the headers, but how does it matter if i call res.setContentType before i get the writer. As long as i dont' have ...





11. PrintWriter / ResponseWriter throwing an excpetion.    coderanch.com

This is a part of my product's code: Object oData = oPartData.getPartContent(); if( oData instanceof String ) { out.write( (String)oData ); } else { InputStream oPartToDisplayStream = (InputStream)oData; try { byte[] buf = new byte[4 * 1024]; // 4K buffer int bytesRead; while ((bytesRead = oPartToDisplayStream.read(buf)) != -1) out.write( new String(buf), 0, bytesRead); } finally { if (oPartToDisplayStream != null) oPartToDisplayStream.close(); ...

12. Truncated Strings in PrintWriter println    coderanch.com

When I have a string with a blank in it, I am only getting the last part of the s.toString() returned to the browser. I am guessing I have to do something creative with quotes to resolve this problem but it is not jumping out at me. import java.io.* ; import com.javaranch.common.* ; public class ReverseServlet2 extends LogServlet { public void ...

13. streaming output    coderanch.com

response.setContentType( "text/html" ); java.io.PrintWriter out = response.getWriter(); out.println( "Example 5" ); out.println( "" ); out.println( "

Countdown

" ); for ( int i = 10; i > 0; i-- ) { out.print( "

" ); out.print( i ); out.println( "

" ); out.flush(); // make sure the browser see the count try { Thread.sleep( 1000 ); } catch ( InterruptedException e ) {} } out.println( ...

14. overriding the outputstream of container    coderanch.com

outputstream overriding to get the byte stream for caching, didn't succeed. There is some problem of container that is not allowing to override the ouputstream implementation of it. This all is done to cache the outputstream bytes to save the rendering time. Is this correct ? Whether this rendering problem (caching of bytes) will really improve the performance.

15. Streaming media    coderanch.com

Hi, I'm looking for a way to make Java stream video. There are two approaches: 1) Make Java actually stream it, as in spew bytes out over the net 2) Make Java control some other streaming server like Helix or Darwin Streaming Server Does anybody have any suggestions on which one will work best, and/or how to do either one? I ...

16. PrintWriter    coderanch.com





17. problem in reading from an InputStream when sending data across two applications    coderanch.com

When is the data set? The JSP is executed when the user makes a request for the JSP's URL. If the user is supposed to fill in a form and submit the data, it isn't there to be sent. In this case your servlet which processes the POST of the form should forward the data to the second app. Perhaps you ...

18. getResourcesAsStream Versus new FileInputStream    coderanch.com

Just for my interest, I've seen a lot of posts where people can't get an xml file or say a properties file in a war. new FileInputStream doesn't work but getClass().getResourceAsStream() does, could someone explain succinctly to me why this is the case. What is the difference between these mechanisms that makes the latter work. Also I've seen posts where the ...

19. PrintWriter    coderanch.com

20. Stream and writer    coderanch.com

Hello guys, Happy Passover. what are exactly stream (especially OutputStream) and writer, and what are the differences between them? For example, what a stream does that a writer cannot and vice versa? I am trying to understand the following GZIPResponseWrapper: (among many things, I do not understand why they need both a stream and a writer ). import java.io.*; import java.util.*; ...

21. Streaming large content to client    coderanch.com

Hello Ranchers, How y'all doing today? Need your help. I am generating some pdf files from a base pdf template on fly and streaming them to client. When I do this just for one file it is working fine, but now I need to generate several files and stream all of them to client at once. So in this situtaion what ...

22. Why PrintWriter is not buffered ?    coderanch.com

It is a part of architecture design. They have logic behind doing so. Read this The initial JspWriter object is associated with the PrintWriter object 34 * of the ServletResponse in a way that depends on whether the page is or 35 * is not buffered. If the page is not buffered, output written to this 36 * JspWriter object will ...

24. Do i need to close PrintWriter    coderanch.com

25. Problem in OutputStream    coderanch.com

As far as I know, reading in files over a web request is not that straight forward. This is so because the request contains additional information (like content-length, encoding etc) and the entire information (including the actual data stream) is available as one big chunk in the request's input stream. That roughly is what is meant by 'multipart/form-data'. The servlet api ...

26. PrintWriter object's unusual error::    coderanch.com

public class ReadCompleteErrorReport extends HttpServlet { //static PrintWriter out; PrintWriter out; public void doGet(HttpServletRequest req,HttpServletResponse res){ try{ PrintWriter out = res.getWriter(); this.out = out;//assigning to globally declared variable // Some other code ....not relavent to this error } catch (Throwable t){ t.printStackTrace(); } } public void printData(String data){ System.out.println(""+data);// i am getting this data properly..no error with this part out.println("From PrintData1"+data);//[B]Showing ...

28. Reading a PrintWriter    coderanch.com

Hi Martin, Although I understand the literal meaning of the question asked I am not certain of the larger issue that is addressed by finding a solution to the question. Can you provide more information about the nature of the issue that has lead up this question? A servlet filter can be used to post-process a response before it reaches the ...

29. how to get LastModified time of a file system file and webserver file    coderanch.com

my task is that i have to compare modified time of two file.one is in file system other is database file . which is most recently updated (which is newer). details : we have to check that which file is most recently updated .if database 's file is newer then we copy that file in to file system other wise we ...

31. Editing the contents of the PrintWriter    coderanch.com

32. OutputStream and HTML Title    coderanch.com

Hi, I use an output stream in my servlet and write a PDF document. The PDF is given a link in the jsp page. When the link is clicked the PDF gets opened. It opens as an "inline" and not an attachment, as expected. The "title" of the html page shows the whole url like "http://localhost:8080/myapp/test.jsp". How can I change this ...

33. An Input stream from the request    coderanch.com

34. Uploading File - Need to read as stream    coderanch.com

35. BufferedWriter for Mulitple file type    coderanch.com

Hi All, I want multiple files to upload to Web(or)FTP Server. So, now what i have been done is i carried whichever files and wherever location from client browser to servlet file. Here is my servlet code. My problem is i don't know how to write multiple file to server. If i need to use BufferedWriter to write multiple file, how ...

36. PrintWriter    coderanch.com

37. Request input stream    coderanch.com

Hi everyone, I'm having a small issue trying to grab a parameter value from a request. I thought it would just as easy as doing request.getParameter("parameter"); but it isn't in this case. The parameter I'm looking for is stored in the input stream, I've actually iterated through the request inputstream and found the value there, but it's not in the request ...

39. printwriter vs system.out.println    coderanch.com

import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class SimpleServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) { try { response.setContentType("text/html"); PrintWriter printWriter = response.getWriter(); printWriter.println("

"); printWriter .println("If you are reading this, your application server is good to go!"); printWriter.println("

"); } catch (IOException ioException) { ioException.printStackTrace(); } } } in the program above why printwriter is ...

40. PrintWriter's println not working properly for me    coderanch.com

Hi, Can anyone please provide me any hints as to why following piece of code is not working as I think it should? public void go(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter pw = response.getWriter(); Enumeration paramNames = request.getParameterNames(); pw.println("Start"); //this is printed on browser ArrayList params = new ArrayList(); String str = ""; while(paramNames.hasMoreElements()) { String name = ...

41. outputStream gives wrong file name    coderanch.com

i am trying to send a jar file to the client. here is the code for it.but it change the file name and there is no any file type. public void service(HttpServletRequest req,HttpServletResponse res)throws javax.servlet.ServletException, java.io.IOException { res.setContentType("application/jar"); OutputStream os = res.getOutputStream(); byte[] bytearray = new byte[1024]; ServletContext context = getServletContext(); URL url = context.getResource("/files/test.jar"); InputStream is = url.openStream(); int bytesread ...

42. using BufferedReader.readLine()    coderanch.com

Please UseCodeTags. Have you profiled the code at all to see where the code is taking time? Even System.currentTimeMillis() might be enough to help. Obviously it doesn't take "hours" to upload a 4M file, and even complicated SQL processing for a file that small should be a matter of minutes (depending, of course). We have processes here that process files of ...

45. How to Stream video    coderanch.com

Hello, I want to make one application. The concept is I want to make Java web application which shows live video at my web side. what I have to do and which instrument required for that. Concept are I want to cover good place and that place contain camera or ip camera. which shows live video at my web site. Please ...

46. Commons fileupload streaming api: FileItemIterator not being able to iterate twice    coderanch.com

Hi, We are using the CFU streaming api to upload multiple image files. Our form has input text fields as well as input file fields. Before uploading the files we need to validate our text fields. This requires us to iterate over the FileItemIterator twice, once while validating to get the text field values and the second time to acutally get ...

47. Text file file reader in real time    coderanch.com

Hi all, I would like to write a utility that will allow me to view some text files in a browser. I usually logon to the server to view the log files. What i would like to do is just load a browser and view the files in real time. The files are usually quite large so i only need to ...

49. Java's PrintWriter to write javascript onto an html page: why?    coderanch.com

Is the application you talk about from "pre JSP" era? Does it use JSP at all? I have had the unwanted experience of having to debug old Servlet applications that do not use JSP. So do you really ask "Why didn't they place the javascript to JSP pages instead?" or "What does javascript add to static html page?"... Answer to the ...

50. getting stream for resource    coderanch.com

Hi, IMO, the 1st one is better. The 2nd one ties you to a specific type of file system, and requires an absolute path. If your server location change, that code may not work anymore. In addition, because it uses the normal Java IO, it can open any file in your server - to me, it's a security risk.

52. how to use servelt output stream in the program    coderanch.com

no i have created a mvc model this is just one part of that code........and i have succesfully compiled it using print writer but now i want to learn use of outputstream instead of printwriter.......so need a example for that as m getting confused how to write that...so m trying to implement in that code only

53. PrintWriter no html to response    coderanch.com

I cannot understand why the following code does not write the HTML to response. the debugger executes each line, no errors, but no html to the response either. ANy suggestions on how to troubleshoot this ? Thank you public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); /* * Write the HTML to the response ...

54. download stream gets broken    coderanch.com

Hi all, I have a file download servlet running on Tomcat, the code is public void service(HttpServletRequest request, HttpServletResponse response) throws IOException { byte buf[] = new byte[1024 * 4]; try { File file = new File(realFilename); int size = (int) file.length(); BufferedInputStream realFile = new BufferedInputStream(new FileInputStream(file)); OutputStream out = response.getOutputStream(); response.setContentLength(size); while (true) { int count = realFile.read(buf); if ...