当前位置: 代码迷 >> J2EE >> java.lang.StringIndexOutOfBoundsException: String index out of range: -1
  详细解决方案

java.lang.StringIndexOutOfBoundsException: String index out of range: -1

热度:587   发布时间:2016-04-22 02:06:19.0
一个简单的Servlet截取URL怎么老是有错,请求解释啊
user.add(username);
request.getRequestDispatcher("/add_success.jsp").forward(request, response);
String requestURI = request.getRequestURI();
String path = requestURI.substring(requestURI.indexOf("/", 1), requestURI.indexOf(".")); //这行有错!
System.out.println("path=" + path);






type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

java.lang.StringIndexOutOfBoundsException: String index out of range: -11
java.lang.String.substring(String.java:1938)
com.TestTest.TestServlet.doPost(TestServlet.java:27)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.33 logs.


------解决方案--------------------
你是想截取/ 到. 之间的字符串吧。如果是这样就应该这样用
String path = requestURI.substring(requestURI.indexOf("/", 1), requestURI.indexOf(".")-requestURI.indexOf("/", 1),);
这样就对了
代码迷推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.daimami.com/search?q=317
  相关解决方案