This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Priyanka Dande
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • 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

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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 http://www.coderanch.com/how-to/java/WhyNotUseScriptlets.
 
Priyanka Dande
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • 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();
}

}
 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why did you not use code tags in your second post?
 
Palak Mathur
Ranch Hand
Posts: 342
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a classpath issue, have you include servlet api in to application class path?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic