1、练习生成如下界面:并对可能产生的异常以页面的形式进行处理。
index.html
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <html> <head> <title>division computer</title> </head> <body> <form action="compute.jsp" method="post"> <h2> Please enter an integer concrete </h2> <p> dividend: <input type="text" name="dividend"> <br> divisor: <input type="text" name="divisor"> <br> </p> <p> <input type="submit" value="计算"> </p> </form> </body> </html> compute.jsp
<%@ page language="java" errorPage="error.jsp" pageEncoding="GBK"%>
<html>
<head>
</head>
<body>
<h1>
<%
int first = Integer.parseInt(request.getParameter("dividend"));
int second = Integer.parseInt(request.getParameter("divisor"));
int result = first / second;
out.println("结果为:" + result);
%>
</h1>
</body>
</html>
error.jsp
<%@ page language="java" isErrorPage="true" pageEncoding="GBK"%>
<html>
<head>
</head>
<body>
<%
String message = "";
if (exception instanceof NumberFormatException) {
message = " Please enter a valid integer";
} else if (exception instanceof ArithmeticException) {
message = "Zero can't do the divisor";
} else {
exception.toString();
}
%>
<h3><%=message%></h3>
</body>
</html>
2、练习按JavaBean的形式建立一个Student类,该类包含id、name、age三个属性。
注意:在src文件夹下建立包名为com.bean的包,把Student类放在包中。
package com.bean;
public class Student {
private int student_id;
private String student_name;
private int student_age;
public int getStudent_id() {
return student_id;
}
public void setStudent_id(int studentId) {
student_id = studentId;
}
public String getStudent_name() {
return student_name;
}
public void setStudent_name(String studentName) {
student_name = studentName;
}
public int getStudent_age() {
return student_age;
}
public void setStudent_age(int studentAge) {
student_age = studentAge;
}
}