Java servlets are Web services run on host servers that provide an interface for developers to work with the internal business objects. Java servlets are written in typical Java code, and they can be used to open Word documents. This is beneficial for programmers who work with Word documents on the Web server for user interaction and Web content. The documents can be created, edited and deleted using the Java servlet code.
- Skill level:
Other People Are Reading
Create your Java output stream variable. This is used to send the Word document to the user and open it in the code. The following code creates the output stream variable:
ServletOutputStream output = res.getOutputStream();
Set the MIME type. The MIME type sets the type of file the output stream expects. In this case, the type is a Word document, but it can be set to any file type. The following code sets the output type:
res.setContentType( "application /msword" );
Set the document you want to open. The following code opens the Word file. It is still not displayed to the user, but it is opened.
res.setHeader("Content-disposition", "attachment; filename=my_doc.doc" );
Output the file to the user's window. The "URL" class is used to set the output to the Word document. This step is only needed if you want to display the document you opened. The following code displays the Word document:
URL url = new URL ( res );
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for