第一题:
test1.jsp
<form action="show1.jsp" method="post">
操作数1:<input type="text" name="numone"><br>
//操作符号:<input type="text" name="oper"><br>
操作符号:
<select name="oper">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select><br>
操作数2:<input type="text" name="numtwo"><br>
<input type="submit" value="提交">
</form>
show1.jsp
<%
request.setCharacterEncoding("utf-8");
int numone=Integer.parseInt(request.getParameter("numone"));
String op=request.getParameter("oper");
int numtwo=Integer.parseInt(request.getParameter("numtwo"));
if(op.equals("+")){
out.println(numone+numtwo);
}
else if(op.equals("-")){
out.println(numone-numtwo);
}
else if(op.equals("*")){
out.println(numone*numtwo);
}
else if(op.equals("/")){
if(numtwo!=0){
out.println(numone/numtwo);
}else{
out.println("非法输入");
}
}
%>
数据转换关键代码
String转为int型 方式1
//int height = Integer.parseInt(request.getParameter("height"));
String转为int型 方式2
//int height = Integer.valueof(request.getParameter("height")).intvalue();
//String转为float型
float weight = Float.parseFloat(request.getParameter("weight"));
System.out.println(weight);
第二题:
<%
ServletContext sc=this.getServletContext();
if(sc.getAttribute("count")==null){
sc.setAttribute("count", 1);
out.println("你是第1个用户");
}else{
int i=(int)sc.getAttribute("count");
if(session.isNew()){//解决刷票问题
i++;
}
sc.setAttribute("count", i);
out.println("你是第"+i+"个用户");
//session.invalidate();删除session
}
%>
第三题MVC求三角形面积
input.jsp
<form action="HandleData" method="post">
边A:<input type="text" name="sideA"><br>
边B:<input type="text" name="sideB"><br>
边C:<input type="text" name="sideC"><br>
<input type="submit" value="提交">
</form>
HandleData//servlet页面
Triangle tri=new Triangle();
request.setAttribute("triangle", tri);
double a=Double.parseDouble(request.getParameter("sideA"));
double b=Double.parseDouble(request.getParameter("sideB"));
double c=Double.parseDouble(request.getParameter("sideC"));
double p=(a+b+c)/2.0;
double area=Math.sqrt(p*(p-a)*(p-b)*(p-c));
tri.setSideA(a);
tri.setSideB(b);
tri.setSideC(c);
tri.setArea(area);
if(a+b>c&&a+c>b&&b+c>a){
tri.setTriangle(true);
}else{
tri.setTriangle(false);
}
request.getRequestDispatcher("show.jsp").forward(request, response);
}
Triangle.java
private double sideA,sideB,sideC,area;
boolean isTriangle;
public Triangle() {
// TODO Auto-generated constructor stub
}
show.jsp
<jsp:useBean id="triangle" type="com.yl.Triangle" scope="request"></jsp:useBean>
边A:<%=triangle.getSideA() %><br>
边B:<%=triangle.getSideB() %><br>
边c:<%=triangle.getSideC() %><br>
能否构成三角型:<%=triangle.isTriangle() %><br>
面积:<%=triangle.getArea() %> <br>
第四题:
第四题:JDBC插入数据
关键步骤:
1.加载驱动
2.获得连接对象
3.获得发送SQL语句工具
4.设置动态参数
5.执行SQL,处理结果
6.关闭资源
关键代码:
//1.注册驱动 加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.创建连接
String url = "jdbc:mysql://localhost/company";
String user = "root";
String password = "123456";
Connection connection= = DriverManager. getConnection (url, user, password);
//判断是否连接成功
if(connection!=null){
System.out.println("连接成功!");
}else{
System.out.println("false");
}
//3.获取连接后的对象
Statement statement = connection.createStatement();
//4.编写SQL语句
String sql = "insert into t_jobs(JOB_ID,JOB_TITLE,MIN_SALARY, MAX_SALARY) values('JAVA_mgr','JAVA_Lecturer',4000,12000)";//注意DML语句中字符串是单引号
//5.接收结果
int result = statement.executeUpdate(sql);
System.out.println(result);//执行DML语句 增删改时,返回受影响行数(INT类型)
//6.释放资源 先开后关
statement.close();
connection.close();