JSp小程序编写

JSp小程序编写,第1张

你好,两个jsp文件,test02.jsp,test03.jsp.代码如下:

<%@ page contentType="text/htmlcharset=gb2312" %>

<%

String num = (String)request.getParameter("num")

try{

int n = Integer.parseInt(num)

if(n%2==0)

request.setAttribute("type","偶数")

else

request.setAttribute("type","奇数")

request.getRequestDispatcher("test03.jsp").forward(request,response)

}catch(Exception e){

e.printStackTrace()

}

%>

<%@ page contentType="text/htmlcharset=gb2312" %>

<% String type = (String)request.getAttribute("type")%>

<html>

<head><title>页面一</title></head>

<body>

<form action="test02.jsp" method="get">

请输入一个数字:<input type="text" name="num" />

<input type="submit" value="提交" />

</form>

${type }

</body>

</html>

Q5Action.jsp

<%@page language="java" contentType="text/htmlcharset=UTF-8" pageEncoding="UTF-8" %>

<%

String cashAmount = request.getParameter("cashamount")

String taxAmount = ""

if (cashAmount != null &&cashAmount.length() >0) {

int cash = Integer.parseInt(cashAmount)

if (cash >0 &&cash <= 10000) {

taxAmount += cash * 0.0205

} else if (cash >10000 &&cash <= 30000) {

taxAmount += cash * 0.0215

} else if (cash >30000) {

taxAmount += cash * 0.0250

}

}

%>

<html>

<head>

<title>ST8016 Assignment Q5</title>

<script type="text/javascript">

function verify() {

regExp = /\d+\b/

if (!(regExp.test(thisForm.cashamount.value))) {

alert("You must enter valid amount!")

return false}

return true

}

</script>

</head>

<body>

<h1>Assignment 1 - Question 5</h1>

<b>Tax Calculation</b>

<br /><br />

<table border="1" cellpadding="3">

<tr bgcolor="#EEEEEE"><td align="center">Amount</td><td align="center">Tax Rate %</td></tr>

<tr><td align="center">1 - 10,000 </td><td align="center">2.05 % (0.0205)</td></tr>

<tr><td align="center">10,001 - 30,000 </td><td align="center">2.15 % (0.0215)</td></tr>

<tr><td align="center">>30,000 </td><td align="center">2.50 % (0.0250)</td></tr>

</table>

<form name="thisForm" action="Q5Action.jsp" method=”post”

onsubmit="return verify()">

Amount: <input name=cashamount size=10 value="<%=cashAmount%>"/><br/><br/>

<input type=submit value="Calculate Tax Amount"/>

</form>

<% if (taxAmount.length() >0) {%>

<p>Tax Amount: <b><%=taxAmount%></b></p>

<% }%>

</body>

</html>

jsp代码

<form action="" method="post">

<input type="text" name="num1">

<input type="text" name="num2">

<input type="submit" name="提交">

</form>

action 代码

HttpServletRequest request=ServletActionContext.getRequest()

int num1 =Intger.parInt(request.getParameter("num1"))

int num2 =Intger.parInt(request.getParameter("num2"))

int sum=num1+num2

request.setAttribute(sum,"sum")

jsp 代码

结果是:${sum}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7960978.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存