Here i found the answer guys.Thanks for your replies.
first i will show you the folder structure.
enter image description here
first you have to add the above shown libraries to your application.
Then this is the jsp file code.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="resources/bootstrap/bootstrap-theme.css"/>
<link rel="stylesheet" type="text/css" href="resources/bootstrap/bootstrap-theme.min.css"/>
<link rel="stylesheet" type="text/css" href="resources/bootstrap/bootstrap.css"/>
<link rel="stylesheet" type="text/css" href="resources/bootstrap/bootstrap.min.css"/>
<title>Jasper Report Example</title>
</head>
<body>
<form class="form-horizontal" class="form-inline" method="post" action="GetUserData" style="margin-left: px;margin-right: 5px;">
<div style="padding-bottom: 25px;"></div>
<div class="form-group">
<label class="control-label col-sm-3">First Name*</label>
<div class="col-sm-6">
<input name="firstname" id="idfirstname" class="form-control input-sm">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3">Last Name*</label>
<div class="col-sm-6">
<input name="lastname" id="idlastname" class="form-control input-sm">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3">Email*</label>
<div class="col-sm-6">
<input name="email" id="idemail" class="form-control input-sm">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3">Phone Number*</label>
<div class="col-sm-6">
<input name="phonenumber" id="idphonenumber" class="form-control input-sm">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3">NIC*</label>
<div class="col-sm-6">
<input name="nic" id="idnic" class="form-control input-sm">
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-5 col-xs-10">
<button type="submit" class="btn btn-primary " style="padding-left: 25px;padding-right: 25px;">Sumbit</button>
</div>
</div>
</form>
</body>
As you seen in the folder structure you have to make the pojo class.
This is the UserDetails.java pojo class.
public class UserDetails {
private String firstname;
private String lastname;
private String email;
private String phonenumber;
private String nic;
/**
* @return the firstname
*/
public String getFirstname() {
return firstname;
}
/**
* @param firstname the firstname to set
*/
public void setFirstname(String firstname) {
this.firstname = firstname;
}
/**
* @return the lastname
*/
public String getLastname() {
return lastname;
}
/**
* @param lastname the lastname to set
*/
public void setLastname(String lastname) {
this.lastname = lastname;
}
/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return the phonenumber
*/
public String getPhonenumber() {
return phonenumber;
}
/**
* @param phonenumber the phonenumber to set
*/
public void setPhonenumber(String phonenumber) {
this.phonenumber = phonenumber;
}
/**
* @return the nic
*/
public String getNic() {
return nic;
}
/**
* @param nic the nic to set
*/
public void setNic(String nic) {
this.nic = nic;
}
}
After creating the simple pojo class now you have to make the controller class.As seen in the folder structure report.controller package contain the controller class GetUserData.java servlet file.
This is the GetUserData.java servlet file code.
public class GetUserData extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, JRException {
//response.setContentType("application/pdf");
//PrintWriter out = response.getWriter();
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String email = request.getParameter("email");
String phonenumber = request.getParameter("phonenumber");
String nic = request.getParameter("nic");
HashMap parametermap = new HashMap();
parametermap.put("firstname", firstname);
parametermap.put("lastname", lastname);
parametermap.put("email", email);
parametermap.put("phonenumber", phonenumber);
parametermap.put("nic", nic);
GenerateReport generatereportinstance = new GenerateReport();
byte[] outputarray = generatereportinstance.GenerateReport(request.getServletContext(),parametermap);
OutputStream outStream = response.getOutputStream();
response.setHeader("Content-Disposition", "filename=UserDetailsReport.pdf");
response.setContentType("application/pdf");
response.setContentLength(outputarray.length);
outStream.write(outputarray, 0, outputarray.length);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (JRException ex) {
Logger.getLogger(GetUserData.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (JRException ex) {
Logger.getLogger(GetUserData.class.getName()).log(Level.SEVERE, null, ex);
}
}
Now the last coding part.create the business logic in the package report.utility package.
Here is the GenerateReport.java class file code.
public class GenerateReport {
JasperPrint jasperPrint = null;
public byte[] GenerateReport(ServletContext servletContext, HashMap parametermap) {
byte[] outputFile = null;
try {
try {
String file = servletContext.getRealPath("/WEB-INF/report/UserDetails.jasper");
System.out.println("Generating PDF Report");
JasperPrint jasperPrint =JasperFillManager.fillReport(file,parametermap,new JREmptyDataSource());
outputFile =JasperExportManager.exportReportToPdf(jasperPrint);
System.out.println("User Details.pdf has been generated!");
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return outputFile;
}
}
Now You to do something more.create report is the next one.
I have created the UserDeatails.jasper report using the iReport 5.6.0.
After creating the basic structure of your report using iReport ,
right click on the report Goto --> properties --> language (select java in the language)
Other wise report may occur errors.
Then compile the report.and the compiled .jasper file to the project as shown in the folder structure.
I will add image of jasper report.
enter image description here
That is all guys.Again thanks for your replies.