文档介绍:java ee ; j2ee视频教程- jsp第2讲
作者: 韩顺平
(一)jsp第2讲主要内容:
1 在同一jsp页面提交和接收数据
2 eclipse(myeclipse)开发工具
3 model1模式
4 一个简单的用户登录系统
(二)在同一jsp页面提交和接收数据
快捷键:是一段代码整体右移,先选中,安tab键,往左移先选中按住shift+tab
原本jsp文件:
C:\Apache Software Foundation\apache-tomcat--windows-x86\apache-tomcat-
\webapps\Myjsp
C:\Apache Software Foundation\apache-tomcat--windows-x86\apache-tomcat-
\work\Catalina\localhost\\apache\jsp
代码:
<!-- 这是我的计算器的界面-->
<!-- 思考:如何控制num2不能为空同时也不能为字母!! -->
<!-- 思考:!! -->
<%@ page contentType="text/html;charset=gb2312" %>
<html>
<h1>我的计算器!</h1>
<head>
<!-- 在jsp中添加js代码,防止用户空提交-->
<script language="javascript">
<!--
//写一个函数验证用户是否空提交!
function checkNum()
{
//判断num1是否为空
if(=="")
{
("num1 不能为空!");
return false;
}
//判断num1是否是一个数.
if(() !=)
{
("num1 不是一个数!!");
return false;
}
if(=="")
{
("num2 不能为空!");
return false;
}
//判断num2是否是一个数.
if(() !=)
{
("num2 不是一个数!!");
return false;
}
}
-->
</script>
</head>
<body>
<%
//接受第一个,类似于servlet中的HttpServletRequest
String num1= ("num1");
//接受第二个
String num2= ("num2");
//接受运算符
String oper =("flag");
//计算
int res=0;
//为了防止异常的发生(第一次运行的时候num1\num2为空)
if (num1 !=null && num2 !=null &&oper !=null)
{
int numi1=(num1);
int numi2=(num2);
if (("+"))
{
res =numi1+numi2;
}
else if (("-"))
{
res =numi1-numi2;
}
else if (("*"))
{
res =numi1*numi2;
}
else if (("/"))
{
res =numi1/numi2;
}
}
%>
<form name="form1" action="" method="post">
<hr>
请输入第一个数:<input type="text" name ="num1" value="<%=num1%>