I have a TranslateServlet
that takes some text, creates an instance of a Cracker
class and calls a getCrackedResult
method which returns a HashMap
of keys and values:
public class TranslateServlet extends HttpServlet {
public static void main(String[] args) {
Cracker cracker = new Cracker("German", "English");
HashMap<String, ArrayList<Cracker.EntrySet<String, ArrayList<String>>>> crackedResults =
cracker.getCrackedResult("Ich bin");
for (String category: crackedResults.keySet()) {
System.out.print(category); //works fine
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
Cracker cracker = new Cracker("German", "English");
HashMap<String, ArrayList<Cracker.EntrySet<String, ArrayList<String>>>> crackedResults =
cracker.getCrackedResult("Ich bin");
for (String category: crackedResults.keySet()) {
response.getWriter().print(category); //doesn't work
}
response.getWriter().print("DONE"); //works fine
}
}
However, while iterating over a HashMap
inside the main
method works fine, when trying to get the key values through doPost()
method, I only get DONE
as an output, and no other values. Why??
EDIT: "Doesn't work" means doesn't output categories. If the main method outputs "Dog", "Cat", the jsp page only outputs "Done", instead of "Dog", "Cat", "Done".
What is wrong with my question to suffice closing it? Thank you anonymous downvoter too.
EDIT: I checked the tomcat's log and there is a java.io.FileNotFoundException: some_folder/some_file.bin
error every time the servlet is called.
I realised that the function getCrackedResult
makes use of a local file like this:
InputStream inputPOS = new FileInputStream("some_folder/some_file.bin");
I tried moving the file to WEB-INF
and changing the file path:
InputStream inputPOS = new FileInputStream("WEB-INF/some_folder/some_file.bin");
But I still get the exception: java.io.FileNotFoundException: WEB-INF/some_folder/some_file.bin
.
How should I deal with the file so that Cracker
class, specifically its getCrackedResults
function could get it after deployment. My file structure looks like this:
Project Root
------------src
---------------com
------------------util
----------------------Cracker.java
------------------web
---------------------TranslateServlet.java
------------target
------------------classes
-------------------------com
----------------------------util
--------------------------------Cracker.class
----------------------------web
-------------------------------TranslateServlet.class
------------imgs
------------some_folder
-----------------------some_file.bin
------------WEB-INF
-------------------classes
-------------------lib
-------------------some_folder
------------------------------some_file.bin (tried putting it here)