jsp 知识点总结
Posted rainbowbridge
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jsp 知识点总结相关的知识,希望对你有一定的参考价值。
JSP 入门案例(计算器的实现)
对于jsp 学习的一些总结
1.使用Dreamweaver 搭建网页
<form id="form1" name="form1" method="post" action="index.jsp?action=Do "> //注意form 调单后面的acton 标签,其Do会成为action的参数被提交,验证时候,名为acton.值为Do
<table width="335" height="309" border="1" align="center">
<tr>
<td colspan="2"><h1 align="center">计算器</h1></td>
</tr>
<tr>
<td width="113">第一个数字</td>
<td width="210">
<input name="firstNumber" type="text" id="textfield" size="30" /></td>
</tr>
<tr>
<td>符号</td>
<td><input type="radio" name="radio" id="radio" value="1" />
<label for="radio">+
<input type="radio" name="radio" id="radio2" value="2" />
-</label>
<input type="radio" name="radio" id="radio3" value="3" />
<label for="radio3">*</label>
<input type="radio" name="radio" id="radio4" value="4" />
<label for="radio4">/</label>
</td>
</tr>
<tr>
<td>第二个数字</td>
<td><input name="secondNumber" type="text" id="textfield2" size="30" /></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input type="submit" name="button" id="button" value="提交" />
<input type="reset" name="button2" id="button2" value="重置" />
</div></td>
</tr>
</table>
</form>
2.使用JSP 获取参数并处理表单
? (
//jsp:useBean 对java代码的获取
scope 标签:作用于整个页面
ID:给java类起一个对象名字
class:java字节码文件的位置
<jsp:useBean id="c" scope="page" class="com.yangtao.Calculate"></jsp:useBean>
<jsp:useBean id="d" scope="page" class="com.yangtao.Opertate"></jsp:useBean>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//验证表单
如果request对象的获取的action参数的值是"Do",则对起处理
if("Do".equals(request.getParameter("action")))
//获取参数
String firstString=request.getParameter("firstNumber");
String secondString=request.getParameter("secondNumber");
String operString=request.getParameter("radio");
//复制给Bean
c.setFirstNumber(Double.parseDouble( firstString));
c.setOprateCharechter(Integer.parseInt( operString));
c.setSecondNumber(Double.parseDouble(secondString));
//计算结果
double resultData=d.doCalculator(c);
out.print("计算结果是:\t"+resultData);
3.java代码对计算逻辑的实现
package com.yangtao;
/**
* 这是计算机实体类
* @author yangtao
*
*/
public class Calculate
/**
* firstNumber 这个是第一个计算参数
*/
private double firstNumber;
private double secondNumber;
private int oprateCharechter;
/**
* 第一个参数的GET方法
* @return firstNumber
*/
public double getFirstNumber()
return firstNumber;
public void setFirstNumber(double firstNumber)
this.firstNumber = firstNumber;
public double getSecondNumber()
return secondNumber;
public void setSecondNumber(double secondNumber)
this.secondNumber = secondNumber;
public int getOprateCharechter()
return oprateCharechter;
public void setOprateCharechter(int oprateCharechter)
this.oprateCharechter = oprateCharechter;
package com.yangtao;
/**
* 这是计算类
* @author lenovo
*
*/
public class Opertate
/**
* 这是计算机类 的变量
*/
private Calculate calc;
/**
* 这是是计算方法
*/
public double doCalculator( Calculate calc)
double first=calc.getFirstNumber();
double second=calc.getSecondNumber();
int opion = calc.getOprateCharechter();
double resulrValues=0.0;
switch (opion)
case 1:
resulrValues= first+second;
break;
case 2:
resulrValues= first-second;
break;
case 3:
resulrValues= first*second;
break;
case 4:
resulrValues= first/second;
break;
default:
break;
return resulrValues;
4.<%@ include file="MyJsp.jsp" %>