我首先尝试将数据保存到数据库,然后打开一个jasper报告PDF.Below是我的Servlet代码。
public class insertMedicationSrvlt extends HttpServlet
{
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
MedicationsTbl medTbl=new MedicationsTbl();
int idPatient = Integer.parseInt(request.getParameter("idPatient"));
boolean actInact=Boolean.valueOf(request.getParameter("activeInactive"));
String insertResult="NA";
String ptInstructionsTxt=request.getParameter("ptInstructionsTxt").trim();
int quantityTxt=0;
if(!request.getParameter("quantityTxt").isEmpty())
{
quantityTxt=Integer.parseInt(request.getParameter("quantityTxt"));
}
String drug=request.getParameter("drugTxt").trim();
String dispenseFromTxt=request.getParameter("dispenseFromTxt").trim();
String refilTxt=request.getParameter("refilTxt").trim();
String exerciseTxt=request.getParameter("exerciseTxt").trim();
String therapyTxt=request.getParameter("therapyTxt").trim();
String save = request.getParameter("save");
System.out.println(save);
Date startedDateTxt=null;
if(!request.getParameter("startedDateTxt").isEmpty())
{
startedDateTxt=Date.valueOf(request.getParameter("startedDateTxt"));
}
try
{
if(!( quantityTxt==0&& ptInstructionsTxt.isEmpty() &&drug.isEmpty() && refilTxt.isEmpty()&& exerciseTxt.isEmpty()&&therapyTxt.isEmpty() &&startedDateTxt==null))
{
insertResult=medTbl.insertMedication(idPatient, actInact, ptInstructionsTxt, quantityTxt, drug, dispenseFromTxt, refilTxt, exerciseTxt, therapyTxt, startedDateTxt);
request.getSession().setAttribute("resultMessage", insertResult);
try
{
if(save.equals("print"))
{
int idUser=Integer.parseInt(request.getSession(false).getAttribute("UserID").toString());
int idSubUser=Integer.parseInt(request.getSession(false).getAttribute("SubUserID").toString());
//Get company info
UserTable userTable = new UserTable();
UserBean userBean = userTable.findUserDetails(idUser);
//Get Doctor info
SubUserTable subUserTable = new SubUserTable();
SubUserBean subUserBean = subUserTable.findSubUser(idSubUser);
//Get Patient info
Demographics demographics = new Demographics();
List<DemographicsBean> demographicsBean = demographics.findDemo(idPatient);
//Print the report
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("companyName",userBean.getCompanyName().trim());
params.put("companyPhone",userBean.getPhone().trim());
request.getSession().setAttribute("data", params);
Connection connection = DBMaster.getInstance().getConnection();
JasperPrint jasperPrint=JasperFillManager.fillReport(getClass().getResourceAsStream("/ReportResources/Prescription2.jasper"), params,connection);
byte[] exportReportToPdf = JasperExportManager.exportReportToPdf(jasperPrint);
ServletOutputStream sout = response.getOutputStream();
response.setContentType("application/pdf;");
JRPdfExporter jRPdfExporter = new JRPdfExporter();
jRPdfExporter.setParameter(JRPdfExporterParameter.PDF_JAVASCRIPT, "this.print();");
jRPdfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
jRPdfExporter.setParameter(JRExporterParameter.OUTPUT_WRITER, sout);
jRPdfExporter.setParameter(JRPdfExporterParameter.PDF_JAVASCRIPT, "this.print();");
request.setAttribute("exportIndentObject", jRPdfExporter);
//response.setHeader("Content-Disposition", "attachment;filename=" + "myReport.pdf");
response.setContentType("application/pdf");
response.setHeader("Content-disposition","inline; filename=\"myReport.pdf\"");
sout.write(exportReportToPdf);
sout.flush();
sout.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
catch(Exception ex)
{
request.getSession().setAttribute("resultMessage", Common.SAVE_ROLLBACK);
ex.printStackTrace();
}
finally
{
response.sendRedirect("medicationsDisplayAllSrvlt?idPatient="+idPatient+"&activeStatus=true");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo()
{
return "Short description";
}// </editor-fold>
}
下面是调用上述servlet的表单。
<form id="addForm" class="form-horizontal" method="post" action="insertMedicationSrvlt?idPatient=${param.idPatient}">
<!--form elements-->
<button type="submit" name = "save" class="btn btn-primary" value="save">Save</button>
<button id="printBtn" type="submit" name = "save" class="btn btn-primary" value="print" >Save and Print</button>
<button type="reset" class="btn btn-default">Clear</button>
</form>
现在,当我按下“保存并打印”按钮时,数据将被保存并且“在同一窗口中”,PDF将打开。
我在下面尝试摆脱这个。
在表单标记中使用target="_blank"
:这为PDF打开了一个窗口,但是“modal”表单仍然在另一个标签中打开,直到我手动刷新该页面,新数据没有出现。之所以发生这种情况,是因为我猜不会调用sendredirect
。
从insertMedicationSrvlt
servlet调用“Print”servlet:这也不起作用,PDF在同一窗口中打开。
现在我真正需要做的是;
insertMedicationSrvlt
,数据已保存。insertMedicationSrvlt
调用刷新JSP页面的servlet。我该怎么做?