I m new in jasper report and eclipse.

Actually I created a receipt in jasper studio (.jrsml file) and I want to change the report properties on run time.

Description:

I want to fetch a report properties in jsp page and user can change this properties at runtime and save this report.

How to way make it using jsp?

please help me.......

Thank you

Recommended Answers

All 19 Replies

Can you please provide more details, what do you mean by properties?
If you want to create report that user can edit you will first need to make a report with fields you will have, then show data on website and let user edit it.
After editting data grab it all put in the list and send it to jasper report so it will generate it ok.

I hope this helps if not please provide more details what exacly do you need.

Kind regards, Mike.

hello mike,
yes I want to fetch report properties in jsp page
for ex.
pagewidth
pageheight etc
user see this properties on running page and also facilate to user can change the properties.
are u getting my question mike.

Thank you mike

Let say like this.

When you create jasper reports you create feilds that will be shown in report.

Let say you want to show user values from table.
So you will create fields like: username, data of birth, name, last name, etc...

If you want that your website user change some things before creating report you will need one .jsp page that will show all data from table.
In that page you can edit that and when user click create report you will call servlet.
That servlet will grab all values from that page and create a list and that list send to jasper report.
Jasper report will do his work and create you a report.

Little summury:
Crate a jasper report with fields that you will have.
Show data on one .jsp page where user can edit it.
After editing submit that data to servlet.
Servlet will create a list from that data and send that list to report.
Jasper will create report and that would be it.

I hope this helps, if you still have problems please provide some coding so we can help you with that.

Regards, Mike.

Thanks mike
your reply is much helpful for me and you perfectly explain your point that I understand very easily.
I realy thanx to you.

I ask one more question to you.
can I done this in only jsp page? means using scriptlet tag and javascript without using servlet?

<jsp:body>
<input type="text" class="k-textbox" style="width:48%" value="Paper Width" disabled>  
       <input type="text" class="k-textbox" style="width:48%;margin-left:-3px" data-bind="value:PageWidth" Onkeypress="changeProprty()"> <br/> 


<script>
        <% JasperReport jasperReport=null; %>
        <% Connection con=null; %>
        <% JasperDesign jasperDesign=null;%>
            $(document).ready(function() {

                 kendo.bind($('body'), viewModel);
               <%

                  try
               {
                            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                            con=DriverManager.getConnection("jdbc:sqlserver://localhost;user=xxx;password=xxxxx;database=xxxxxx");
                              InputStream inputStream=new FileInputStream(new File("E:/receipts.jrxml"));
                             jasperDesign=JRXmlLoader.load(inputStream);

                            jasperReport=JasperCompileManager.compileReport(jasperDesign);

                            int PageWidthForReport=jasperReport.getPageWidth(); 
                            int PageHeight=jasperReport.getPageHeight();
                            JRBand headerHeight=jasperReport.getPageFooter();
                            JRBand footerHeight=jasperReport.getPageFooter();


                            %>

                            var width='<%=PageWidthForReport%>';
                            var height='<%=PageHeight%>';
                            var header='<%=headerHeight%>';
                            var footer='<%=footerHeight%>';
                          //width1=width;
                            viewModel.set("PageWidth",width);
                            viewModel.set("PageHeight",height);
                            viewModel.set("HeaderHeight",header);
                            viewModel.set("FooterHeight",footer);

                           // JasperPrint jasperPrint=JasperFillManager.fillReport(jasperReport,null, con);

                            //JasperViewer.viewReport(jasperPrint);

              <% }
               catch(Exception e)
               {

               }


               %>



         });

         function changeProprty()
         {

            alert(<%=jasperReport.getPageWidth()%>);

           <%

               // int PageWidth = jasperReport.getPageWidth();

                jasperDesign.setPageWidth(PageWidth);
                jasperReport=JasperCompileManager.compileReport(jasperDesign);
                JasperPrint jasperPrint=JasperFillManager.fillReport(jasperReport,null, con);


                //  JasperCompileManager.writeReportToXmlFile(jasperReport, outputStream);
                JasperCompileManager.writeReportToXmlFile(jasperReport, "E:/receipts3.jrxml");

                JasperViewer.viewReport(jasperPrint);
           %>



         }


         var viewModel = kendo.observable({

            PageWidth: 0,
            PageHeight:0,
            HeaderHeight:0,
            FooterHeight:0

         });

         </script>

         </jsp:body>

here is my code;

and i cant pass the javascript variable to java scriptlet.

jasperDesign.setPageWidth(PageWidth);
that line give me the error because i passed (pagewidth) javascript variable.

Actually report properties are display on web page but i cant edit this properties.

I hope now you understand very well what i want.

Thank you.

Hmm, JavaScript is working in browser and Java on server side so you can't mix that two without using servlets or sessions.

You can try to create a call to store that javascript values in database or session then when you go on next page you will get that values easily.
Problem with that call is that they need to use servlet I don't know a way without servlets.

For comunicating between JavaScript and Java you will need AJAX.

So try something like this: link1 or this.

I hope it will help you.

ok i will try this

thank you mike once again.

very gud morning mike

can you help me once again?

how to connect jasper report and jsp through servlet?

please give me example code for servelet and jsp above given code.

You have it on this link.
Last thing in post about jsp is how to generate a jasper report from servlet.
That goes in doPost od doGet it depends what are you calling.

I hope this help, if does give me few tumbs up just other can see that this post is good.
Kind regards, Mike.

hello mike

Now I used servlet and its work properly but i m not understand how to get properties in jsp page from servlet on page loading and how to pass updated properties to servelet on textfield onchange.

Thank you mike

please give me the sample code for servlet and jsp

Hello, you have it all on that link I send it to you.
First you create a servlet that will return you all values you want from db.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        KnjigaManager kmg = new KnjigaManager();
        List<Knjiga> sveKnjige = kmg.listKnjige();
        request.setAttribute("sveKnjige", sveKnjige);
        RequestDispatcher rd = getServletContext().getRequestDispatcher("/izvestaji/prikaziSveKnjige.jsp");
        rd.forward(request, response);       
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

That will grab all books from DB and put it in request. variable is: sveKnjige
This is jsp that will show all books.

<body>
<table border=1>
<thead><td>Naslov</td><td>Autor</td><td>Izdavac</td><td>Godina</td></thead>
<c:forEach var="knjiga" items="${sveKnjige}">
<tr><td>${knjiga.naslov}</td>
    <td>${knjiga.autor}</td>
    <td>${knjiga.izdavac}</td>
    <td>${knjiga.godinaIzdanja}</td></tr>
</c:forEach> </table> </body>

You can create there inut fields and put values inside of that.
And you can put that in formwith subimt button.
Whten you submit you will get all values in new servlet.

hello mike

Thanks for your quick reply but i m not fully understand your last reply.

I used text box in jsp page and I fetched properties values in servlet and also set in servlet. then how can i pass that values to textfields from servlet and again pass all values to servlet on submit button.

Thank you.

You will need to create a page where you will show details from database.
That page will look like this:

<form method="post" action="/AutoKuca/IzmeniAutomobilServlet" class="login">
<div><label>Marka:</label><input type="text" name="marka" value="${vozilo.marka}"></div>
<div><label>Model:</label><input type="text" name="model" value="${vozilo.model}"></div>
<div><label>Boja:</label><input type="text" name="boja" value="${vozilo.boja}"></div>
<div><label>Godina proizvodnje:</label><input type="text" name="godina" value="${vozilo.godinaproizvodnje}"></div>
<div><label>Cena:</label><input type="text" name="cena" value="${vozilo.cena}"></div>

<input type="submit" value="Izmenite automobil!">
</form>

If you can se there is a input type text and value so you grab data from db and show it like this. When you open a page in that text boxes you will have value that your servelt send to you.
After that when you click on submit you will send it to new servelt and do whatever you want with it...

hello mike

Now i display the report properties on jsp page textbox using ajax.
index.jsp code is:

 $(document).ready(function() {
                 kendo.bind($('body'), viewModel);
                $.post( "api/myServlet?getProperty",
                            function(response) {
                                var myObject = JSON.stringify(response);
                                var obj1 = JSON.parse(myObject);
                                var obj = $.parseJSON(obj1);
                                viewModel.set("data",obj);

               });
});

and i m aslo used mvvm viewmodel. so its working properly.

So now I want set editable values to again report on save button click using ajax.

Thank you mike

When I click on save button then How to set editable values to report properties ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.