Madi
Madi

Reputation: 55

Calling a jsp multiple times from single doPost() method

I am using PD4ML libraries for converting my .jsp to pdf files and I need to call the same jsp file for a List of values. I am doing this in my doPost()

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String [] posSelected = request.getParameterValues("selectPOs");
    for(String eachPO: posSelected){

    request.getRequestDispatcher("CreateInvoices.jsp").forward(request,response);

//This does not work as can not create multiple instances of servlet. }}

I get java.lang.IllegalStateException: Cannot forward after response has been committed exception.

How can I invoke same JSP multiple times?

Thanks MekaM

Upvotes: 2

Views: 1308

Answers (4)

Madi
Madi

Reputation: 55

I have used jsoup.connect().get() to achieve what I wanted.

Upvotes: 0

Mudassar
Mudassar

Reputation: 3225

As mentioned by others you can send only one response per request in HTTP protocol hence you need to try another approach.

In your case since pd4ml is mandatory and here you need multiple pdfs hence creating multiple jsp is not the ideal way. Hence rather than converting the jsp to pdf you should create the multiple pdf through the code as shown in the link http://pd4ml.com/examples.

 private void runConverter(String urlstring, File output) throws IOException {

         if (urlstring.length() > 0) {
                if (!urlstring.startsWith("http://") && !urlstring.startsWith("file:")) {
                              urlstring = "http://" + urlstring;
                }

4               java.io.FileOutputStream fos = new java.io.FileOutputStream(output);

5               if ( proxyHost != null && proxyHost.length() != 0 && proxyPort != 0 ) {
                       System.getProperties().setProperty("proxySet", "true");
                       System.getProperties().setProperty("proxyHost", proxyHost);
                       System.getProperties().setProperty("proxyPort", "" + proxyPort);
                }

6               PD4ML pd4ml = new PD4ML();

7               try {                                                              
                       pd4ml.setPageSize( landscapeValue ? pd4ml.changePageOrientation( format ): format );
                    } catch (Exception e) {
                       e.printStackTrace();
                    }

                if ( unitsValue.equals("mm") ) {
                       pd4ml.setPageInsetsMM( new Insets(topValue, leftValue,
bottomValue, rightValue) );
                } else {
                       pd4ml.setPageInsets( new Insets(topValue, leftValue,
bottomValue, rightValue) );
                }

                pd4ml.setHtmlWidth( userSpaceWidth );

8               pd4ml.render( urlstring, fos );
         }
   }

Upvotes: 0

Jafar Ali
Jafar Ali

Reputation: 1114

By using Include instead of Forward on Request Dispatcher. You can call same jsp multiple time suing include.

It will look something like this.

request.getRequestDispatcher("CreateInvoices.jsp").include(request,response);

Upvotes: 0

BalusC
BalusC

Reputation: 1109262

How can I invoke same JSP multiple times?

By including it multiple times.

request.getRequestDispatcher("CreateInvoices.jsp").include(request, response);

Upvotes: 1

Related Questions