关于JSP HTTP Status 500 – Internal Server Error,求大神帮一下忙!

mo1256732361 2017-11-12 01:44:54
JSP中的报错信息:HTTP Status 500 – Internal Server Error

--------------------------------------------------------------------------------

Type Exception Report

Message An exception occurred processing JSP page [/play.jsp] at line [11]

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.apache.jasper.JasperException: An exception occurred processing JSP page [/play.jsp] at line [11]

8: <title>Insert title here</title>
9: </head>
10: <body bgcolor = cyan>
11: <jsp:useBean id = "play" class="second.PlayBean" scope = "session"></jsp:useBean>
12: <br>杩欐槸瑕佹樉绀虹殑鍥剧墖
13: <%
14: String webDir = request.getContextPath();


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:584)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:481)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause

java.lang.StringIndexOutOfBoundsException: String index out of range: -1
java.lang.String.substring(String.java:1954)
second.PlayBean.<init>(PlayBean.java:17)
org.apache.jsp.play_jsp._jspService(play_jsp.java:122)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Note The full stack trace of the root cause is available in the server logs.


--------------------------------------------------------------------------------

Apache Tomcat/8.5.20

我的代码如下:
package second;

import java.io.File;

public class PlayBean {

int imageNum = 0,max;
String pictureName[] , playImage;
String webDir = "";
String tomcatDir;


public PlayBean() {
File f = new File("");
String path = f.getAbsolutePath();
int index = path.indexOf("bin");
tomcatDir = path.substring(0, index);

}

public void setImageNum(int n) {
if(n<0) n = max-1;
if(n==max) n=0;
imageNum = n;
}

public void setWebDir(String s) {
webDir = s;
if(pictureName == null) {
File dirImage = new File(tomcatDir+"/webapps/"+webDir+"/image");
pictureName = dirImage.list();

}

if(pictureName != null) {
max = pictureName.length;
}


}

public int getImageNum() {
return imageNum;
}




public String getPlayImage() {
if(pictureName != null) {
playImage = pictureName[imageNum];

}
return playImage;
}



}


在JSP中的代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body bgcolor = cyan>
<jsp:useBean id = "play" class="second.PlayBean" scope = "session"></jsp:useBean>
<br>这是要显示的图片
<%
String webDir = request.getContextPath();
webDir = webDir.substring(1);
%>

<jsp:setProperty name = "play" property = "webDir" value = "<%= webDir %>"/>
<jsp:setProperty name = "play" property = "imageNum" param = " imageNum "/>
<jsp:getProperty name = "play" property = "playImage"/>
<br><img src = "image/op.jpg" width=120 height = 90>
<br>单击"上一张"或"下一张"按钮浏览图片

<form action= "" method = post>
<input type = submit name = "ok" value = "上一张">
<input type="hidden" name = "imageNum" value="<%= play.getImageNum()-1 %>">
</form>

<form action ="" method = post>
<input type = submit name = "ok" value = "下一张">
<input type = "hidden" name = "imageNum" value = "<%= play.getImageNum()+1%>">

</form>

</body>
</html>

我是真的找不出问题出现在哪里,求大神指导一下,困扰很久了,新手上路,文件存放的图片也在这里,谢谢各位!
...全文
1785 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
黑夜的风 2017-11-18
  • 打赏
  • 举报
回复
是不是下一张图片的路径/名字有问题? 找不到内容的时候会报500
什么都不能 2017-11-18
  • 打赏
  • 举报
回复
我感觉是tomcatDir = path.substring(0, index); 这个地方报错了,index 很可能是-1,看看你的path里是不是包含了bin File f = new File(""); String path = f.getAbsolutePath(); int index = path.indexOf("bin"); tomcatDir = path.substring(0, index);

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧