java异常:java.lang.IllegalStateException

J2EE 码拜 10年前 (2015-04-19) 1577次浏览 0个评论

后台生成验证码,代码如下:
public class ValidationCodeAction extends ActionSupport implements
ServletResponseAware, ServletRequestAware {

private HttpServletRequest request;
private HttpServletResponse response;
private JspWriter out;
javax.servlet.jsp.PageContext pageContext;
public void setServletResponse(HttpServletResponse arg0) {
// TODO Auto-generated method stub
this.response=arg0;
}

public void setServletRequest(HttpServletRequest arg0) {
// TODO Auto-generated method stub
this.request=arg0;
}
private String codeChars=”%#23456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”;
//随机返回一个颜色(color对象)
private Color getRandomColor(int minColor, int maxColor)
{
Random random=new Random();
//保证minColor和maxColor不会大于255
if(minColor>255)
{
minColor=255;
}
if(maxColor>255)
{
maxColor=255;
}
//获得随机颜色值
int red =minColor +random.nextInt(maxColor-minColor);
int green =minColor +random.nextInt(maxColor-minColor);
int blue =minColor +random.nextInt(maxColor-minColor);
return new Color(red,green,blue);
}
public String create()
{
//System.out.println(“图像尚未发送”);
int charsLength=codeChars.length();
//关闭客户端浏览器的缓冲区
response.setHeader(“Pragma”,”No-cache”); 
response.setHeader(“Cache-Control”,”no-cache”); 
response.setDateHeader(“Expires”, 0);
//图形长宽
int width=90;
int height=20;
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g= image.getGraphics();
Random random=new Random();
g.setColor(getRandomColor(180,250));
//填充背景
g.fillRect(0, 0, width, height);
//设置初始字体
g.setFont(new Font(“Times New Roman”,Font.ITALIC,height));
//随机颜色
g.setColor(getRandomColor(120,180));
StringBuilder validationcode =new StringBuilder();//用于保存随机生成的验证码
String []fonName={“Times New Roman” ,”Book antiqua” ,”Arial”};
//随机生成3-5个验证码
for(int i=0;i<3+random.nextInt(3);i++)
{
g.setFont(new Font(fonName[random.nextInt(3)],Font.ITALIC,height));
char codechar=codeChars.charAt(random.nextInt(charsLength));
validationcode.append(codechar);
g.setColor(getRandomColor(10,100));
//在图形上画验证码字符,x和y都是随机生成
g.drawString(String.valueOf(codechar), 16*i+random.nextInt(7), height-random.nextInt(6));
}
//session的实效时间
HttpSession session=request.getSession();
session.setMaxInactiveInterval(5*60);
//验证码保存在session中
session.setAttribute(“validation_code”, validationcode.toString());
g.dispose();//关闭
ServletOutputStream os;
try {
os = response.getOutputStream();
//以JPEG格式想客户端发送验证码
ImageIO.write(image, “JPEG”,os);
//System.out.println(“图像已经发送”);
os.flush();
os.close();
out.clear();
out=pageContext.pushBody();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;
}

}
在前端jsp页面获取验证码:
<script type=”text/javascript”>
function refresh()
{
var img=document.getElementById(“img_validationcode”);
img.src=”user/validate_code?”+Math.random();
}
</script>
  </head>
  
   <body>
   <div class=”north”> 
   请输入用户注册信息
   </div>
   <div class=”center”>
   <font color=”red”><s:actionerror/></font>
   <s:form action=”register-action” namespace=”/user” method=”post” validate=”true”>
   <span id=”log”>单击此处<a href=”<%=basePath%>user/user-login.jsp”>登陆</a></span>
   <s:textfield label=”用户名” name=”user.username” id=”username” required=”true”/>
   <s:password label=”密码”  name=”user.password” id=”password” required=”true”/>
   <s:password label=”请再次输入密码”  name=”user.repassword” id=”repassword” required=”true”/>
   <s:textfield label=”邮箱”  name=”user.email” id=”email” required=”true”/>
   <s:textfield label=”验证码”  name=”user.validationcode” id=”validationcode” required=”true”/>
   <s:submit name=”submit” value=”注册”/>
   </s:form>
   </div>
   <div class=”south”>
   <table>
   <tr>
   <td>用户验证码:</td>
   <td><img id=”img_validationcode” src=”user/validate_code”/></td>
   <td><a href=”#” onClick=”refresh()”>重新获取验证码</a></td>
   </tr>
   </table>
   <s:debug></s:debug>
   </div>
  <%
   //out.clear();                  
   //out=pageContext.pushBody();
   %>
  </body>
老是抛出异常如下:
java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:451)
at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:707)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
这个怎么解?就连是在哪里抛出的异常都确定不到,大神们给给解。。。

java异常:java.lang.IllegalStateException
问题解决!~~在生成验证码的代码中:return SUCCESS;这代表着转向,需要response的,而response.getOutputStream();在ImageIO.write(image, “JPEG”,os);时已经committed,不能再response一次,当把return SUCCESS;改成return null;所有问题都解决!至于怎样是committed,推荐看看这里:http://jorton468.blog.163.com/blog/static/72588135201102441617287/
java异常:java.lang.IllegalStateException
40分
 这个生成验证码的使用普通servlet就可以,不会有struts2的诸多问题
java异常:java.lang.IllegalStateException
嗯,对,但在用struts2框架时常会有这个异常!

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明java异常:java.lang.IllegalStateException
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!