Posts

Showing posts with the label servlets

Create a folder from Servlet and save file

Create a folder from Servlet and save file In my form, user uploads a photo(s) and then I want to store that photo in a network disk (in our local network). The following works fine, when I execute it from Netbeans in my PC: public void saveFiles(List<Part> fileParts,String targetId) { String pathRoot = "A:\ShareDisk1\Photos\"; String date = new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime()); try { int counter = 1; for (Part filePart : fileParts) { String destPath = pathRoot + "\" + targetId; Boolean flag = new File(destPath).mkdirs(); System.out.println("flag = " + flag); File file = new File(destPath, targetId + " " + date + "-" + counter + ".jpg"); try (InputStream input = filePart.getInputStream()) { Files.copy(i...

Download file in Servlet

Download file in Servlet I am trying to download a file in a Servlet to the web client. I keep on getting this error. Exception in thread "Thread-5" java.lang.NullPointerException at org.apache.coyote.http11.Http11OutputBuffer.commit(Http11OutputBuffer.java:347) at org.apache.coyote.http11.Http11Processor.prepareResponse(Http11Processor.java:1402) at org.apache.coyote.AbstractProcessor.action(AbstractProcessor.java:327) at org.apache.coyote.Response.action(Response.java:173) at org.apache.coyote.http11.Http11OutputBuffer.doWrite(Http11OutputBuffer.java:219) at org.apache.coyote.Response.doWrite(Response.java:541) at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:351) at org.apache.catalina.connector.OutputBuffer.flushByteBuffer(OutputBuffer.java:825) at org.apache.catalina.connector.OutputBuffer.append(OutputBuffer.java:730) at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:391) at org.apache.catalina.connector.OutputBuffe...

Handle HTML encoded data in Java

Handle HTML encoded data in Java I need to dynamically generate some bottoms based on the return of ajax get call @.getJSON("SearchByName", {"name":searchstring}, function(data){ data = data.replace(/ /g, "&nbsp"); var pop = "<p>" + data + "</p><br/><button style="border-radius: 4px;" onclick=getDatabyName("" + data +"")>Load Locations</button>"; ..... Since the return data may have space so I have to replace all spaces with &nbsp; &nbsp; When users press the generated bottom to execute getDatabyName , it calls a Servlet to query a database. However, the query within the Java Servlet returns nothing because the space characters are not properly encoded. I tried to do another replace inside getDatabyName but still the same. I have ways to work around this but want to know the proper ways (either in Javascript or in Java) to handle this situation...

Servlet doesnt write JSON Output to AJAX

Servlet doesnt write JSON Output to AJAX Following code for my output: PrintWriter out = response.getWriter(); ObjectMapper objectMapper = new ObjectMapper(); ToJson obj = new ToJson(); String obj1 = objectMapper.writeValueAsString(obj); response.setContentType("application/json"); out.print(obj1); System.out.println(obj1); out.close(); The obj1 looks like this: {"prname1":"P1neu","anz1":"342356","prid1":"1","price1":"25"} {"prname1":"P1neu","anz1":"342356","prid1":"1","price1":"25"} It should send the string out so I can parse it in my AJAX and display it but somwhow it ends up with nothing as console.log/etc doesnt display any data. I had out.append but it also didnt work. out.append 1 Answer 1 ...