I am using JS to obtain a variable and then make a POST call to a Servlet. I am not using Ajax explicitly.
$('#download').click(function (e) {
....
$.post('downloadServlet', {'var': variable});
//e.preventDefault();
});
The Servlet has code to read a file and, in theory, send it, but I do not get any response.
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
....
response.setContentType("application/octet-stream");
response.setContentLength(...);
response.setHeader( "Content-Disposition", String.format("attachment; filename=%s", path.getFileName()));
try (OutputStream out = response.getOutputStream()) {
Files.copy(path, out);
out.flush();
}
System.out.println("Done");
My questions is Why is the file not sent/received? From a technical point, I am not sure I understand why it doesn't work, since I am not using Ajax. The POST method seems to execute as any normal submit form. I have read this but doesn't clarify. Any didactic explanation will be appreciated.
Related
I need to implement a requirement, where I need to deliver javascript code securely. My Idea is,
I will make the path as /something.js and in the controller, I will check the authentication, if not authenticate I will deliver console.error("Auth Failed").
How I can achieve the above scenario.
this is the simplest way using common request handler. works 100 %
#GetMapping(value = "/hello.js")
public void resources( HttpServletRequest httpRequest,
HttpServletResponse httpResponse) throws Exception {
//Authentication goes here or using handler interceptor
httpResponse.setContentType("text/javascript");
httpResponse.setHeader("MIME-type", "text/javascript");
httpResponse.getWriter().write(" function alertMan(msg) { \n alert(\"message:\"+msg); \n } ");
}
public String Home(HttpServletRequest request,
HttpServletResponse response, HttpSession session) {
String refer = request.getHeader("Referer");
request.setAttribute("refer", refer);
return "home/visit/editVisit";
}
public String Home2(HttpServletRequest request,
HttpServletResponse response, HttpSession session) {
String refer2 = request.getParameter("refer");
return "home/visit/addVisit";
}
In the first step, I get the function from one Controller to another Controller and since then the request changes in URL.
The new Controller page has several different functions.
I can catch the change there to know from which page I came by the line:
String refer = request.getHeader ("Refer");
But as I continue (by the buttons ...) to the next pages of the project. I don't have access to refer variable.
How can I be constantly available to a variable that would please and not only refer to a specific JSP?
I need quick help,
Thanks for every reply
i want to execute paragraph tag on jsp page from the servlet response.I am able to execute alert of javascript but failed to execute below code. it is redirecting to servlet and then getting execute. I want below code in NewFile.jsp any solution for this.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");//setting the content type
PrintWriter out=response.getWriter();//get the stream to write the data
out.println("<script type=\"text/javascript\">");
out.println("document.write(\"<p>Hello JavaScript by JavaScript</p>\");");
out.println("location='NewFile.jsp';");
out.println("</script>");
out.close();
}
I'm looking for a way to send a redirection with my servlet from my first page to a second page adding into the second page a script tag made inside this servlet.
For example:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("Received a get request");
response.getWriter().append("<script>alert(\"I'm here!\")</script>");
response.sendRedirect("testRedirect.html");
}
This code doesn't work. How can I fix it?
Thanks!
response.sendRedirect writes the redirect header to the output stream. Headers must be sent before any output, this is why your code doesn't work.
Try swapping the two statements, redirecting first and then appending:
response.sendRedirect("testRedirect.html");
response.getWriter().append("<script>alert(\"I'm here!\")</script>");
Thank for your answers I solved with this code:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("received a get message");
PrintWriter out = response.getWriter();
out.println("<script>alert(\"I'm here\");</script>");
request.getRequestDispatcher("testRedirect.html").include(request, response);
out.close();
}
You cannot send the content of the second page inside the redirection response. There are actually two requests.
The first request is handled by your servlet by sending the redirect. There should nothing else in this response.
The browser receives the redirect and does a second request to "testRedirect.html". If you want to include the script tag in the second response the right place to do it is the servlet or JSP which handles this second request.
You can send the user to the desired page by using javascript instead.
out.println("<script> alert(\"I'm here\"); window.location.replace(\"testRedirect.html\"); </script>");
I need help. I have single html-file running on my webserver. Via Javascript I'm sending data to my Java-Servlet on the same server via xhttp-request.
On Java's servlet side I can send a message back to the xhttp-request via a PrintWriter like below:
public void doGet(HttpServletRequest request, HttpServletResponse response)
{
PrintWriter theMessenger = response.getWriter();
theMessenger.print("Response data");
}
That all works fine. But now I need to send a message to the requesting html-file without the use of the servlet-response.
Are there any possibilities for sending data (strings as usual) to the html-file gathering those messages via onmessage-handler for example?
I tried this, but the messange never reaches it target:
String targetURL = CSConfig.GetRootURL() + "/myRequestingPage.html";
URL objUrl = new URL(targetURL);
HttpURLConnection urlCon = (HttpURLConnection) objUrl.openConnection();
urlCon.setRequestMethod("POST");
urlCon.setDoOutput(true);
DataOutputStream dataWriter = new DataOutputStream(urlCon.getOutputStream());
dataWriter.writeBytes(messageContent);
dataWriter.flush();
dataWriter.close();