我用servlet 生成 验证码 但是页面刷新多次 多次访问servlet 就会出现java.lang.IllegalStateException servlet代码如下:
public class VerifyCode extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public VerifyCode() {
super();
} private int width = 80;
private int height = 60;
private int codeCount = 4;
private int xx = 0; // xx
private int fontHeight;
private int codeY;
char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; // codeSequence
public void init() throws ServletException {
String strWidth = this.getInitParameter("width");
String strHeight = this.getInitParameter("height");
String strCodeCount = this.getInitParameter("codeCount"); try {
if (strWidth != null && strWidth.length() != 0) {
width = Integer.parseInt(strWidth);
}
if (strHeight != null && strHeight.length() != 0) {
height = Integer.parseInt(strHeight);
}
if (strCodeCount != null && strCodeCount.length() != 0) {
codeCount = Integer.parseInt(strCodeCount);
}
} catch (NumberFormatException e) {
e.printStackTrace();
} xx = width / (codeCount + 1);
fontHeight = height - 2;
codeY = height - 4; } /**
* @param req
* @param resp
* @throws ServletException
* @throws java.io.IOException
*/
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException { BufferedImage buffImg = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics2D gd = buffImg.createGraphics(); Random random = new Random(); gd.setColor(Color.WHITE);
gd.fillRect(0, 0, width, height); Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);
gd.setFont(font); gd.setColor(Color.BLACK);
gd.drawRect(0, 0, width - 1, height - 1); /* gd.setColor(Color.BLACK);
for (int i = 0; i < 16; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(120);
int yl = random.nextInt(120);
gd.drawLine(x, y, x + xl, y + yl);
}*/ StringBuffer randomCode = new StringBuffer();
int red = 0, green = 0, blue = 0; for (int i = 0; i < codeCount; i++) {
String strRand = String.valueOf(codeSequence[random.nextInt(36)]);
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255); gd.setColor(new Color(red, green, blue));
gd.drawString(strRand, i * xx, codeY); randomCode.append(strRand);
}
HttpSession session = req.getSession();
session.setAttribute("validateCode", randomCode.toString());
System.out.println(randomCode.toString()); resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0); resp.setContentType("image/jpeg"); ServletOutputStream sos = resp.getOutputStream();
ImageIO.write(buffImg, "jpeg", sos);
sos.flush();
sos.close();
return;
}}我正在做三大框架的整合 是通过struts访问的servlet
public class VerifyCode extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public VerifyCode() {
super();
} private int width = 80;
private int height = 60;
private int codeCount = 4;
private int xx = 0; // xx
private int fontHeight;
private int codeY;
char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; // codeSequence
public void init() throws ServletException {
String strWidth = this.getInitParameter("width");
String strHeight = this.getInitParameter("height");
String strCodeCount = this.getInitParameter("codeCount"); try {
if (strWidth != null && strWidth.length() != 0) {
width = Integer.parseInt(strWidth);
}
if (strHeight != null && strHeight.length() != 0) {
height = Integer.parseInt(strHeight);
}
if (strCodeCount != null && strCodeCount.length() != 0) {
codeCount = Integer.parseInt(strCodeCount);
}
} catch (NumberFormatException e) {
e.printStackTrace();
} xx = width / (codeCount + 1);
fontHeight = height - 2;
codeY = height - 4; } /**
* @param req
* @param resp
* @throws ServletException
* @throws java.io.IOException
*/
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException { BufferedImage buffImg = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics2D gd = buffImg.createGraphics(); Random random = new Random(); gd.setColor(Color.WHITE);
gd.fillRect(0, 0, width, height); Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);
gd.setFont(font); gd.setColor(Color.BLACK);
gd.drawRect(0, 0, width - 1, height - 1); /* gd.setColor(Color.BLACK);
for (int i = 0; i < 16; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(120);
int yl = random.nextInt(120);
gd.drawLine(x, y, x + xl, y + yl);
}*/ StringBuffer randomCode = new StringBuffer();
int red = 0, green = 0, blue = 0; for (int i = 0; i < codeCount; i++) {
String strRand = String.valueOf(codeSequence[random.nextInt(36)]);
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255); gd.setColor(new Color(red, green, blue));
gd.drawString(strRand, i * xx, codeY); randomCode.append(strRand);
}
HttpSession session = req.getSession();
session.setAttribute("validateCode", randomCode.toString());
System.out.println(randomCode.toString()); resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0); resp.setContentType("image/jpeg"); ServletOutputStream sos = resp.getOutputStream();
ImageIO.write(buffImg, "jpeg", sos);
sos.flush();
sos.close();
return;
}}我正在做三大框架的整合 是通过struts访问的servlet
解决方案 »
- 欢迎喜欢j2ee的同仁们继续加盟
- 当session save一个对象还没有flush清理缓存 时候
- springMVC在controller里如何提取servlet
- Tomcat部署后 webapps目录的项目目录下没有jsp网页
- Java 如何实现从A项目登陆页面登入B项目
- jbuilder+weblogic出现的问题,小弟实在是搞不定了,请各位帮个忙
- tomcat配置的若干问题的求教!!!请高手赐教!!!小弟不胜感激
- 有人能否帮我弄明白一下servlet与Javabean的区别(高分)
- 菜鸟求救:Lotus服务器中有什么API或Java中有什么API使我们能通过Java访问lotus中的数据?
- 如何复制(读取)System.out内容?
- 泛型,为什么使用T而不使用Object
- 这个算法java怎么实现?
这是登陆页面:
<tr>
<td height="35" class="login-text02">验证图片:<br /></td>
<td><img id="code" src="front/login!verifyCode?t=0.50874596655" width="109" height="40" onclick="showCode(this)"/>
<a href="javascript:showCode()"><font color="red" size="2">看不清,请点击这</font></a></td>
</tr>这是struts.xml
<package name="front" extends="struts-default" namespace="/front">
<action name="login" class="userAction">
<result name="verifyCode">/VerifyCode?t=${t}</result>
<result name="fail">/login.jsp</result>
<result>/index.jsp</result>
<result name="back">/index.jsp</result>
</action>完整错误信息
严重: Servlet.service() for servlet default threw exception
java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407)
at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:834)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:534)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:877)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:594)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1675)
at java.lang.Thread.run(Thread.java:619)还有一个问题就是 当我访问login.jsp 有时还会访问两次servlet