I want to invoke my jsp page from my index.html.This is html code.
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<form action="DownloadFile.jsp">
<body>
<div>Click here Download File from Server...</div>
<input type="submit" name="downloadButton" value="Download..." />
</body>
</form>
</html>
JSP PAGE:
<%
String filename = "Sample1.zip";
String filepath = "e:\\temp\\";
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
java.io.FileInputStream fileInputStream = new java.io.FileInputStream(filepath + filename);
int i;
while ((i=fileInputStream.read()) != -1) {
out.write(i);
}
fileInputStream.close();
%>
But When i pressing Download Button, it just shows the jsp file content as html.it does not start downloading the file anyway. What is the problem here...
and also i cann't download .docx and .jpg files correctly.It says file may be corrupted...
Please Guide me to get out of this both issues...
Is there a common way to download all types of files in jsp?
Your server either does not support JSP or is not configured for it.
You need a JSP capable server.
Have you configured the Servlet engine with you webserver and done the setup for forwarding request for jsp files to server engine.
Actually, you do not need jsp to download you content . Instead, if you want to download from the client end, you can use html5
<!DOCTYPE html>
<html>
<body>
<p>Click on the below hyperlink to download the any such file:<p>
<a href="5.csv" download>
test
</a>
Related
I am trying to integrate my report in JSP page. Report created using jaspersoft studio. But i am getting null pointer exception on runReportToPdf line. I am totally new to this report and web application. So please someone help to resolve this and get excepted result. Quick support will be very much appreciated. Thanks.
<%# page import="java.io.*"%>
<%# page import="java.sql.Connection"%>
<%# page import="java.sql.DriverManager"%>
<%# page import="java.util.HashMap"%>
<%# page import="java.util.Map"%>
<%# page import="net.sf.jasperreports.engine.*"%>
<%# page trimDirectiveWhitespaces="true" %>
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>Report!</h2>
<%
Connection con = null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:#192.168.0.248:1521:incomingqc", "inqc", "megaWIN123$");
System.out.println("i AM IN CONNECTION");
}catch (Exception ex) {
System.out.println("i AM ex para");
ex.printStackTrace();
}
File reportFile = new File(application.getRealPath("Blank_A4_Landscape.jasper"));//your report_name.jasper file
Map parameters = new HashMap();
byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), parameters, con);
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream outStream = response.getOutputStream();
outStream.write(bytes, 0, bytes.length);
outStream.flush();
outStream.close();
%>
</body>
</html>
PDF and HTML content cannot be mixed. When the JSF page generates the HTML, it writes to a temporary buffer. That temporary buffer is sent to the browser once JSF is told that the response is complete.
In the given code, here is a rough idea of what the web browser will receive:
<html><head></head><body><h2>Report!</h2>%PDFNΘs*̶͑̾̾̅ͫ͏̙̤g͇̫͛͆̾ͫ̑͆l͖͉̗̩̳̟̍ͫͥͨe̠̅s...</body></html>
For the browser to display the report there are a few options:
Generate an HTML version of the report and use <h:outputFormat> to inject the report content into the existing web page.
Generate a PDF version of the report and use <h:commandLink> or <h:commandButton> with an action that writes the PDF to the browser, much as you've already done.
Mixing both approaches as shown in the question, however, will not produce the desired result.
See also:
How to provide a file download from a JSF backing bean?
Send a PDF to browser via JSF
http://www.jroller.com/hakan/entry/jasperreports_and_jsf_integration
i am running a simple web server in java that is running on port 8080 on my machine , the server do receive request that come from browsers (when i write this URL (localhost:8080) ) however i wanted to receive request that come from this html page
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form method="post" action="192.168.1.1:8080">
<input type="text" name ="txt">
<input type="submit" name="sub">
</form>
</body>
</html>
and the result was The webpage cannot be displayed
and my second try was this
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form method="post" action="server.java">
<input type="text" name ="txt">
<input type="submit" name="sub">
</form>
</body>
</html>
and after i clicked the submit button it opened the download dialog (download server.java).
however i managed to initiate a request on my own in java
Socket socket = new Socket("localhost",8080);
String request = "get / http/1.1";
OutputStream os = socket.getOutputStream();
os.write(request.getBytes());
os.flush();
os.close();
and my server received that request with no problems , i am not sure what am I missing here and what is that layer that initiate a correct request to a php website for example.
Java is not a scripting language and does not work like php or perl. In your server you should map request URI to handling code (e.g. in web.xml). In your <form action="..."> you should provide an URI which your server will handle.
I'm having troubles with uploading and parsing a file as UTF-8 strings. I use the following code:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Part filePart = request.getPart("file");
InputStream filecontent = filePart.getInputStream();
// ...
}
And my webpage looks like this:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form action="UploadServlet" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" />
</form>
</body>
</html>
I found a great post about UTF-8 encoding in java webapps, but unfortunately it didn't work for me. I still have random symbols in strings in NetBeans debugger, and when I display them on a webpage, although most of them get displayed correctly, some cyrillic letters (я, с, Н, А) get replaced by '�?'
The file upload with a HTML form doesn't use any character encoding. The file is transferred byte by byte as is. See here under "multipart/form-data".
So if the original file at client side is a text file with UTF-8 character encoding, then on the server side it is also UTF-8.
Then you can use an InputStreamReader to decode the bytes as UTF-8 text:
InputStreamReader reader = new InputStreamReader(filecontent, "UTF-8");
That's it.
javax.servlet.http.Part, what you use in the very first line of your code, has a method on it getContentType() which will tell you what the content type of the uploaded form data is. Nothing you have written to date would constrain the uploaded form data to any particular character set; ergo you need to determine the character set and deal with it accordingly.
The goal is to upload large file (video) and get the public shared url for them.
Looks like it's pretty straightforward but i spend a bit more than one day going into the documentation and i didn't find any sample of that.
I get the following code to make an upload to Google Store which works fine, but i would like to add the option in the url to make the acl of the file : "public-read". Either prior the upload in the jsp or after in the servlet.
<%# page import="com.google.appengine.api.blobstore.BlobstoreServiceFactory" %>
<%# page import="com.google.appengine.api.blobstore.BlobstoreService" %>
<%# page import="com.google.appengine.api.blobstore.UploadOptions" %>
<%
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
String uploadUrl = blobstoreService.createUploadUrl("/ajax?act=user&act2=video_upload", UploadOptions.Builder.withGoogleStorageBucketName("vidaao"));
%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Upload Page</title>
</head>
<body>
<h1>Upload v3</h1>
<form name="form1" id="form1" action="<% out.print(uploadUrl); %>" method="post" enctype="multipart/form-data" target="upload_iframe">
<input type="hidden" name="hiddenfield1" value="ok">
Files to upload:
<br/>
<input type="file" name="myFile">
<br/>
<button type="submit">Send</button>
</form>
<iframe id="upload_iframe" name="upload_iframe"></iframe>
</body>
</html>
Then in my servlet somewhere the redirect url ends there with the generation of the blobkey
public String upload(HttpServletRequest req, HttpServletResponse res) throws Exception{
Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
BlobKey blobKey = blobs.get("myFile");
if (blobKey == null) {
throw new Exception("Error file not uploaded");
}
//TODO: HERE get the public shared url of the file
return " blob key = " + blobKey.getKeyString();
}
And at this step i would like to have the public shared url for the Google Cloud Storage, if it's possible.
(I cannot serve the file through a servlet because it might time out)
By default files uploaded to bigstore using createUploadUrl are private. You would need to modify the ACL yourself to make it public.
Also, you can use serve() to return blobs of unlimited size from Google Storage from your servlet without concerns for timeout if you would prefer to do it that way rather than making the blobs public.
This question may betray my total lack of understanding, so be it, I guess. Trying the following code to display an image once the JSP page is running on Tomcat. Note, it's the tag that isn't working for me:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>George Brown GeoQuest - Welcome</title>
</head>
<body>
<img alt="logo" src="WebContent/GBGeoQuestLogo.gif">
</br>
<h1>Welcome, GB Geoquest Team!</h1>
<form action="/Lab4">
<label>Team Name:
<input type="text" name="name">
</label><br>
<input type="submit" value="Login">
<input type="reset" value="Reset">
</form>
</body>
</html>
All I get is the blue image question-mark symbol in the top left instead of my image. Link should be viewable here: http://postimage.org/image/drnl831pz/
Can anyone steer me in the right direction?
First you need to confirm that the image file is in the war file. Once you're sure it's there try removing the "WebContent" from the url in the JSP
The value of the src-attribute is missing the context of the application. Now the hostname is used as the context root, so browser uses the wrong url, e.g. http://localhost:8080/WebContent/GBGeoQuestLogo.gif
Add the context root to the src attribute and the image will be loaded, assuming 1) it's inside the war 2) it's located in WebContent-directory in the war:
src="${pageContext.request.contextPath}/WebContent/GBGeoQuestLogo.gif"
I think your context path is Lab4/WebContent and your image is in WebContent. So in this case you can get image by,
src="${pageContext.request.contextPath}/GBGeoQuestLogo.gif"
It will search image in WebContent because your Context path is Lab4/WebContent. If still have problem than in browser View Pagesource and verify the Image path
Thank you