aspose file tools*
The moose likes JSP and the fly likes Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException" Watch "Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException" New topic
Author

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

Priyanka Dande
Ranch Hand

Joined: May 14, 2012
Posts: 53
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

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

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 http://www.coderanch.com/how-to/java/WhyNotUseScriptlets.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Priyanka Dande
Ranch Hand

Joined: May 14, 2012
Posts: 53
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();
}

}
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

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.


Thanks & Regards, Sumeet
SCJP 1.4, SCWCD 5, LinkedIn Profile
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

Why did you not use code tags in your second post?
Palak Mathur
Ranch Hand

Joined: Jan 29, 2007
Posts: 313

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
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

It's a classpath issue, have you include servlet api in to application class path?


Swastik
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException