wood burning stoves 2.0*
The moose likes JSP and the fly likes Using the JSP parameters to fill detail in a word document Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Using the JSP parameters to fill detail in a word document" Watch "Using the JSP parameters to fill detail in a word document" New topic
Author

Using the JSP parameters to fill detail in a word document

Rekha Pande
Ranch Hand

Joined: Jan 29, 2004
Posts: 145
hi all...
I have this JSP file which takes certain information from the user...for example :
User name
Order Amount
Ordered Items
and so on ...
now i have a word document which consists of rows and coloums for username,order amount , and ordered items ...
how would i pass the parameters from this JSP file to the word document .... and then send this word document as an attachment through the mail ......!!!???
please ... any one with working ideas.....please help..!!
...
thanxs.....
Jasmine kaur
Ranch Hand

Joined: Nov 25, 2003
Posts: 157
hello ,
Well if you want the data to be sent to word file this show there should be database connectivity as you can do this in excel sheet , amke a table in excel and then make a connection and send the data to this excel sheet or vice or versa.
Thanks


jasmine kaur
john smith
Ranch Hand

Joined: Mar 04, 2004
Posts: 75
There are a number of ways you could try this:
- to manipulate the Word doc. directly you'd need a Java-COM bridge (such as JIntegra). However, all these bridging api's cost quite a bit of money (though JIntegra is quite good).
- alternatively, you could try to access Word with JNI - which is a big job.
- or write the word integration in a MS native language/script (WSH using CDO would do), export your data to the file systems and call the MS script using java's Runtime class. Though this is asking for errors.
Which ever you consider trying to do this is non-trivial, so I'd recommend trying a different format for you mail message. HTML would do fine, since it has formatted tables. You could either send the message as HTML or add the table as a seperate Part of a Mime message (see Java Mail Service API if you don't understand what this means).
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
In addition to the method suggested, I think the most direct way is to study the document format of MS Word, say you put a template Word document, and mark some special *characters* in Word, by knowing that what format are them be in the raw Word file.
Then, you can use FileInputStream to read the file, and detect all special *characters* and replace them with the form values accordingly.
Of course, this is also a big learning. And it maybe very painful. I did similar thing in the past, while PDF documents, and I think it may not be that worst unless there is a strong reason.
Nick.


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
The last method is probably the most flexible too. By making the parser a generic interface a different document type can be (relatively) easily plugged in at a later date if and when needed.
It also makes it possible to run the application on a machine without MS Word installed, which is often paramount for serverside applications as those usually run on dedicated machines many of which will be running some form of Unix.


42
Rekha Pande
Ranch Hand

Joined: Jan 29, 2004
Posts: 145
my god.... every thing sounds so complicated...... can plz some guide me in the coding part....or some site that gives a tutorial for writing codes to generate word or pdf documents and then send parameters to these documents ...plz.....!!!
i need help for coding...!!!plz help..
thanxs....for all the replies...!
Rekha Pande
Ranch Hand

Joined: Jan 29, 2004
Posts: 145
i have this code .. that makes the PDF and then writes into the PDF.... but how would i pass values to this code from JSP..........??? please any one..help..!
If i use this code directly into the JSP it doesn't work .... if i make a seprate method ... to create the PDF .. then how would i call it in the main ... please any help would be appreciated..!!!
package test1;
import java.io.*;
import java.awt.Color;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
public class Chap0101 {
public static void main(String[] args) {
System.out.println("Chapter 1 example 1: Hello World");
Document document = new Document();
try {

PdfWriter.getInstance(document, new FileOutputStream("create.pdf"));
document.open();
document.add(new Paragraph("Hello World"));
}
catch(DocumentException de) {
System.err.println(de.getMessage());
}
catch(IOException ioe) {
System.err.println(ioe.getMessage());
}
document.close();
}
}
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093

place this in a class you can call from your JSP.
From the JSP (or anywhere) call createDocument("hello.pdf", "Hello World");
Mind that you will need to provide a path to write to!
You can get the path of the webapp using getServletContext().getRealPath("/WEB-INF") which would return the path on disk to your WEB-INF directory.
Rekha Pande
Ranch Hand

Joined: Jan 29, 2004
Posts: 145
i applied the code that you had posted ... but if the Document is not prefixed with the word public then i cannot access the method in the JSP .. so i have added the word public to the method ... now i can acceess the method in JSP .... but when i try to execute the program .. .i get this error ...

java.lang.IllegalAccessError: try to access method atmmachine.pp.createDocument(Ljava/lang/String;Ljava/lang/String Lcom/lowagie/text/Document; from class org.apache.jsp.Detail_0005fInterface$jsp
at org.apache.jsp.Detail_0005fInterface$jsp._jspService(Detail_0005fInterface$jsp.java:113)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:202)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:382)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:474)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2343)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1012)
at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1107)
at java.lang.Thread.run(Thread.java:484)

my class file code is as follows
package atmmachine;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;

public class pp {
public Document createDocument(String filename, String helloString)
{
Document document = new Document();
try
{
PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();
document.add(new Paragraph(helloString));
}
catch(DocumentException de)
{
System.err.println(de.getMessage());
}
catch(IOException ioe)
{
System.err.println(ioe.getMessage());
}
document.close();
return document;
}
}
and my JSP code is
<%@ page contentType="text/html; charset=windows-1252" language="java" import="java.sql.*,java.io.*,java.util.*,java.sql.*,java.text.*,java.util.Date,java.*,com.lowagie.text.*,com.lowagie.text.pdf.PdfWriter" %>
<%@ page session = "true"%>
<jsp:useBean id="text123" scope="session" class="atmmachine.pp" />
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns ="urn:schemas-microsoft-com ffice ffice" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>ADD DETAILS - INTERFACE</title>
</head>
<body>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://128.1.2.3:3306/itsupport","cbqnet","cbqnet");
atmmachine.pp create = new atmmachine.pp();
String filename = "test123.pdf";
String helloString = "created the acrobat document";
create.createDocument(filename, helloString);
%>
</body>
</html>
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
Hi,

Why you these 2 statements?
You have already defined a JavaBean instance by:

The instance name of the JavaBean is text123.
So, you can simply replace your JSP as:

In addition, you also need to import the package atmmachine.pp in the page directive.
See whether this work.
Nick.
Rekha Pande
Ranch Hand

Joined: Jan 29, 2004
Posts: 145
THANK YOU SOOOOOOOOOOOOOOOOOOOOOOOOOOOO MUCH ...IT WORKED..!
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Glad we could help. Good luck with your project.
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
Hi Rekha,
That's great for you.
Best wishes to your project.
Nick.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using the JSP parameters to fill detail in a word document
 
Similar Threads
Controlling Microsoft Document Box
Errors while compiling
my last question for this program.
Readme.txt
howto generate dynamic field in jsp