JSP开发中出现不能把age的String类型转换为int类型,可是我设置的明明就是int型的,什么问题呢?



 int age = request.getParameter("Age");

总是这句报错。
图片描述


 <%
    request.setCharacterEncoding("utf-8");
    String sno = request.getParameter("Sno");
    String sname = request.getParameter("Sname");
    int age = request.getParameter("Age");
    String sex = request.getParameter("Sex");
    String sdept = request.getParameter("Sdept");   
    Student s = new Student(sno,sname,age,sex,sdept);
    StudentDaoImpl sdi=new StudentDaoImpl();
        if(sdi.deleteStudent(s)>0){
            //response.sendRedirect("userlist.jsp");
            out.print("<script type='text/javascript'>alert('成功删除此行记录!');</script>");
        }
        else
            out.println("<script type='text/javascript'>alert('没有此用户');location.replace('userlist.jsp');</script>");

 %>

建表和初始化都是整形,为什么会出现这种错误?


 
create table App.student(sno varchar(10), sname varchar(40), sex varchar(2), age int, sdept varchar(50), primary key(sno));

 public Student(String sno, String sname, int age,String sex,  String sdept) {
        super();
        this.sno = sno;
        this.sname = sname;
        this.sex = sex;
        this.age = age;
        this.sdept = sdept;
    }

java JSP java-ee

开飞机的猛男 9 years, 5 months ago

可以看下ServletRequest的api:java.lang.String getParameter(java.lang.String name),返回值类型就行String

二重黒死蝶 answered 9 years, 5 months ago

这个方法返回类型就是String。做一下转换吧。用封装类将String转成int。数据有效性校验什么的,放到js中做吧。

rabket answered 9 years, 5 months ago

Your Answer