Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Tomcat 5.5 is giving me the exception as StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response,
i want to show excel file to the client from the browser, i am able to open the excel file in to client browser but i am receiving the above exception, kindly help me to fix this problem this is urgent to me. Kindly find below error

 
Sheriff
Posts: 67592
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Step 1 is to remove this code from a JSP and move it to a servlet. After you've done that, post back with the new code and any problems you are having with it.

Please read https://coderanch.com/how-to/java/WhyNotUseScriptlets.
 
Priyanka Dande
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply!



Below is the error i am getting after converting jsp to java



package javax.servlet does not exist
import javax.servlet.*;
^
ExcelDownload.java:5: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
ExcelDownload.java:7: cannot resolve symbol
symbol : class HttpServlet
location: class ExcelDownload
public class ExcelDownload extends HttpServlet{
^
ExcelDownload.java:9: cannot resolve symbol
symbol : class ServletConfig
location: class ExcelDownload
private ServletConfig config;
^
ExcelDownload.java:11: cannot resolve symbol
symbol : class ServletConfig
location: class ExcelDownload
public void init(ServletConfig config)
^
ExcelDownload.java:12: cannot resolve symbol
symbol : class ServletException
location: class ExcelDownload
throws ServletException{
^
ExcelDownload.java:15: cannot resolve symbol
symbol : class HttpServletRequest
location: class ExcelDownload
public void doPost(HttpServletRequest request, HttpServletResponse response)
^
ExcelDownload.java:15: cannot resolve symbol
symbol : class HttpServletResponse
location: class ExcelDownload
public void doPost(HttpServletRequest request, HttpServletResponse response)
^
ExcelDownload.java:16: cannot resolve symbol
symbol : class ServletException
location: class ExcelDownload
throws ServletException,IOException{
^
ExcelDownload.java:49: cannot resolve symbol
symbol : class ServletOutputStream
location: class ExcelDownload
ServletOutputStream outs = response.getOutputStream();
^





Below is the java code for v.jsp

import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ExcelDownload extends HttpServlet{

private ServletConfig config;

public void init(ServletConfig config)
throws ServletException{
this.config=config;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException{

PrintWriter out = response.getWriter();
// String url = "jdbc:mysql://localhost:3306/forum";
Connection con=null;
ResultSet rs;
String filePath=request.getParameter("ff");
String filetoshow=request.getParameter("f");
response.setContentType("text/html");
try {
System.out.println("********************************F I L E P A T H "+filePath);
System.out.println("********************************F I L E P A T H "+filetoshow);

File f = new File (filePath);


String TypDOc = new String(filetoshow);

System.out.println("TypDOc----------------------------------->"+TypDOc.lastIndexOf("."));
String filetype=TypDOc.substring(TypDOc.lastIndexOf(".")+1,TypDOc.length());
System.out.println("filetype----------------------------------->"+filetype);

if (filetype.equals("xls")||filetype.equals("xlx") || filetype.equals("excel") )
response.setContentType ("application/vnd.ms-excel");
if (filetype.equals("ppt")||filetype.equals("pps") )
response.setContentType("application/ms-powerpoint");

response.setHeader ("Content-Disposition", "attachment;filename="+filetoshow);


String name = f.getName().substring(f.getName().lastIndexOf("/") + 1,f.getName().length());

InputStream in = new FileInputStream(f);
//ServletOutputStream outs = response.getOutputStream();
//ServletOutputStream outs = out;


int bit = 256;
int i = 0;
try {
while ((bit) >= 0) {
bit = in.read();
response.getWriter().write(bit);
out.write(bit);
}

} catch (IOException ioe) {
ioe.printStackTrace(System.out);
}
out.flush();
out.close();
in.close();
}

}
 
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please check the path in your application. Do you think something is missing there?
Hint It seems the code is not able to refer to the servlet api.
 
Bear Bibeault
Sheriff
Posts: 67592
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why did you not use code tags in your second post?
 
Ranch Hand
Posts: 343
Mac OS X Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags.

Priyanka Dande wrote:Thank you for your reply!



Below is the error i am getting after converting jsp to java




^





Below is the java code for v.jsp

 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a classpath issue, have you include servlet api in to application class path?
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic