Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Write file to server

Status
Not open for further replies.

AlexR

Programmer
Feb 19, 2002
17
0
0
US
I have a page with a button on it, when pressed I want a servlet to get some data from the database and then create a file with that data and save it to the server and then prompt the user to download the file.

I have the page with the button on it and I have the servlet that gathers all the data, but what I need to know is how do I call a servlet from a page like invokeBusinessObject("string"), and how do I write my servlet to create the file on the server and prompt the user to download it?

Also when the servlet creates the file where does it put it (/SilverStream/Objectstor/General/).

SS 2.54
Oracle 8i


-alex
 
Don't create the file on the server, but rather stream it directly to the client!

Use resp.setContentType(String) to set the ContentType to something browsers wouldn't know about.

The page is a SilverStream Page or is it a JSP or static HTML?

If the former, create an eventhandler for the button:
Code:
private void handle_<controlname>_pageActionPerformed(ActionEvent evt) throws Exception
{
//... handle buttonclick.
}
Create the Servlet inside the SilverStream environment (as a business object), give it a Page alias to call (triggers->URLs) in the format &quot;SilverStream/Pages/<yourservletalias>.html&quot; so you can then use showPage(&quot;p<yourservletalias>.html&quot;); from pageGenerateBegin().

This would have all the action in the servlet, the event handler need only check the input and maybe set things in the session object for the servlet to process (formdata etc.).

If you're using JSP and Servlet, create them as a J2EE application outside of SilverStream and package them as a WAR file.
Upload this using silvercmd deploywar <server>:<port> <database> <warfile> -f <deploymentdescriptor> -v5 -o
the deployment descriptor takes the form:
Code:
<?xml version=&quot;1.0&quot;?>
<!DOCTYPE warJarOptions PUBLIC &quot;-//SilverStream Software, Inc.//DTD J2EE WAR Deployment Plan//EN&quot; &quot;deploy_war.dtd&quot;>

<warJarOptions>
	<warJar>
		<warJarName>additional.war</warJarName>
		<isEnabled>true</isEnabled>
		<sessionTimeout type=&quot;String&quot;>2</sessionTimeout>
		<URL>
			<el>/extra</el>
		</URL>
		<resourceReferenceList>
			<resourceReference>
				<name>jdbc/mydb</name>
				<type>javax.sql.DataSource</type>
	<dataSource>myDatabase<dataSource> 
			</resourceReference>
		</resourceReferenceList>
	</warJar>
</warJarOptions>

Creating a JNDI entry to the database known as myDatabase.
This is referenced in web.xml as:
Code:
<?xml version=&quot;1.0&quot;?>
<web-app>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<resource-ref>
		<res-ref-name>jdbc/mydb</res-ref-name>
		<res-type>javax.sql.DataSource</res-type>
		<res-auth>Container</res-auth>
	</resource-ref>
</web-app>

the dataSource in the SilverStream deployment descriptor must be a database known to SilverStream (add using the designer for example) so the server can create a connection using the proper JDBC driver.

I think you can probably also use a mixed approach where the Page would call a Servlet in an external web application, but that would entail redirecting the HTTP response to the servlet and you'd possibly (haven't tested this) loose the HTTP session so you'd have to pass the parameters as Servlet request parameters instead (small price to pay for the added flexibility maybe).
You'd then replace showPage(&quot;<yourpage.html>&quot;) with res.sendRedirect(&quot;<yourservleturi>&quot;) which should be placed in pageRequestEnd(req,res) for the Page (you'd set a flag that redirection is to take place and the URI of the servlet in the eventhandler).
 
Thanks, jwenting your reply has helped but I have not explained fully my problem.

Yes what you said will work but I am writing the entire page myself and printing it out to a rawHTMLMode() Label. I then use enumeration to get the parameters. Because I have multiple buttons on the page I cannot use event triggers to call the servlet from the page.

I have managed to get the button working properly calling the servlet in the pageRequestEnd(), but what my servlet does is creates an html page and sets the contenet type to &quot;application/msword&quot; so that the browsers opens the page in MS Word.

My final output has to be in MS Word, and the problem that still exists is that when I call the servlet in the pageRequestEnd() it opens the page as an html document and not as word document. If I call the servlet directly <servleturl> the browser will open the document in word.

How can I call my servlet from the page so that it will open as a MS Word document and not as an html page?
 
You can use event triggers for multiple controls (I've done so myself and it is working).
But indeed if you're writing the buttons as standard HMTL controls rather than placing them in the designer that's unlikely to work (most I've ever done is use rawHTMLMode() labels to add dynamically generated Javascript or language dependent texts).

If the servlet is being shown as HTML despite you setting the contentType yourself to Word it might be SilverStream is messing around resetting it back to HMTL for you.
Quite likely if it's coded as a business Object in SilverStream rather than coded as a J2EE application object and placed in a WAR file.

As to where it writes files, I'm not certain.
Would depend on your setup I guess, but certainly not inside the SilverStream database. It would be somewhere on the server filesystem.
Unless you have another HTTP server running that can read from that location you wouldn't be able to serve the file and you may not want to go that way as it would have to run on another port, something we're trying to get rid of here as some of our customers have indicated they can't access anything except port 80 because of firewall issues (for now we've put in a notice to that regard, in the end we'd like to move the other server to another machine with its own ip address and DNS entries).
 
Hey jwenting thanks for your replies, they've helped.

I figured out a way to call the servlet and show up in the MS Word format. In my buttons that I manually create I wrote some javascript that will fire and open a new window calling the servlet and this proved to be working.

As far as writing the file to the server, I tried this and was able to get it working but when opened on different PC's the format changes. So the workaround we created was that there is only one user who will be pulling the reports from the servlet so that user is responsible for calling the servlet and saving the file as a .doc not .html file in word. When calling the servlet and saving in .doc Word converts the file itself.

As a note when I created the report I first created a template in Word and then saved it as a web page. Then I opened the .html file word created in Notepad and examined the way Word does its conversions for HTML and used these when writing HTML from my servlet.

You may not be interesed in all these facts but I wanted to put them here for reference for anyone else trying to do the same.

-alex
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top