回 帖 发 新 帖 刷新版面

主题:今天刚遇到的编译问题,大家都来看看

源程序1(名字为:client.html):
<!--客户端和服务器的程序 假如在客户端输入一个整数,要求在服务器端计算该整数加1的值,服务器并把结果返回给客户端显示出来;如果是字符串的话,服务器端的程序就直接把结果返回给客户端并显示出来。-->
<html>
<head>
<title>a simple example</title>
</head>
<body>
<h2>客户端</h2><br>
<form name="fm" action="transform.jsp" method="post">
<p>输入你所要的值:
<input type="text" name="inputvalue">

<input type="submit" value="提交"></p>
</form>
</body>
</html>


源程序2(名字为transform.jsp):
<%@page import="java.io.*"%>
<%
try{
    String inputvalue = request.getParameter("inputvalue");
    String outputvalue="";
    
    int str_length = inputvalue.length();
    int i = 1;
    int flag = 1;
    int integerNum = 0;
    //校验输入值
    if(!inputvalue.equals(""))
    {
        while(i <= str_length && flag ==1)
        {
            if(inputvalue.charAt(i)>'9' || inputvalue.charAt(i)<'0')    flag = 0;
    
            if(flag == 1)    {
                        integerNum = Integer.parseInt(inputvalue) + 1;
                        outputvalue = Integer.toString(integerNum);                    
                    }
            else    outputvalue = inputvalue;
            i++;
        }
    }
    else{
%>
    输入值为空!
    <a href="client.html">请点击这里重新输入</a>
<%
    }
%>
        <html>
        <%@ page contentType="text/html;charset=gb2312" %>
        <body>
        <title>Answer</title>
        <h2>服务器返回给客户端的值</h2>
        输出值:<%=outputvalue%>
        </body>
        </html>
<%
}
catch(NumberFormatException e)
{
%>
<a href="client.html">请点击这里重新输入</a>
<%
}
%>


3.错误报告如下:

type Exception report

message

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

exception

org.apache.jasper.JasperException: String index out of range: 2
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

java.lang.StringIndexOutOfBoundsException: String index out of range: 2
    java.lang.String.charAt(String.java:444)
    org.apache.jsp.transform_jsp._jspService(transform_jsp.java:59)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


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


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

Apache Tomcat/5.0.28

回复列表 (共2个回复)

沙发

我还是自己来改自己的程序错误之处吧,以下是正确的程序

源程序2(名字为transform.jsp):
<%@page import="java.io.*"%>
<%!
String str;
public String GBK2UNI(String s) throws UnsupportedEncodingException{
String temp = s;
temp = new String(s.getBytes("GBK"),"ISO8859_1");
return temp;
}
public String UNI2GBK(String s) throws UnsupportedEncodingException{
String temp = s;
temp = new String(s.getBytes("ISO8859_1"),"GBK");
return temp;
}
public String send(String s){
return s;
}
%>
<%
try{
String inputvalue = request.getParameter("inputvalue");
String outputvalue=inputvalue;

int str_length = inputvalue.length();
int i = 0;
int flag = 1;
int integerNum = 0;
//校验输入值
if(!inputvalue.equals(""))
{
while(i < str_length && flag ==1){
if(inputvalue.charAt(i)>'9' || inputvalue.charAt(i)<'0') flag = 0;
i++;
}
if(flag == 1) {
integerNum = Integer.parseInt(inputvalue) + 1;
outputvalue = Integer.toString(integerNum);
}
}
else{
%>
输入值为空!
<a href="client.html">请点击这里重新输入</a>
<%
}
%>
<html>
<%@ page contentType="text/html;charset=gb2312" %>
<body>
<title>Answer</title>
<h2>服务器返回给客户端的值</h2>
输出值:<%=UNI2GBK(outputvalue)%>
</body>
</html>
<%
}
catch(NumberFormatException e)
{
%>
<%=e.toString()%><BR>
<a href="client.html">请点击这里重新输入</a>
<%
}
%>

我犯的最重要的错误是把string的索引搞错了java.lang.StringIndexOutOfBoundsException: String index out of range: 2
可以看出,
另外就是循环有一点小问题
呵呵.

板凳




我也出现这样的错误,但是我还是不知道改哪个地方,能不能请楼主指明在哪个地方改啊,标示出来显眼一点好吗 ??

type Exception report


message 

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

exception 

javax.servlet.ServletException: String index out of range: 2
    org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    com.liyou.m8.common.web.filters.LogonFilter.doFilter(LogonFilter.java:50)
    com.liyou.m8.common.web.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:29)


root cause 

java.lang.StringIndexOutOfBoundsException: String index out of range: 2
    java.lang.String.getChars(String.java:484)
    com.liyou.m8.common.util.CryptUtil.decode(CryptUtil.java:290)
    com.liyou.m8.struts.action.LogonAction.execute(LogonAction.java:55)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    com.liyou.m8.common.web.filters.LogonFilter.doFilter(LogonFilter.java:50)
    com.liyou.m8.common.web.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:29)

我来回复

您尚未登录,请登录后再回复。点此登录或注册