-
题目
- JavaWeb中html、jsp、jsp之间的交互 Author
- 鲲鹏猿
第一:jsp中的html与jsp之间的交互(在html中获得n值,在jsp中求n!)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<% int rows ;
try{
rows = Integer.parseInt(request.getParameter("row"));
int fact=1;
for (int i=1;i<=rows;i++){
fact*=i;
}
out.print("n!="+fact);
}catch (Exception e){}
%>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action = "outputresult.jsp" method="post">
<p> 请输入n值:</p>
<input type="text" name = "row">
<input type = "submit" value = "提交">
<input type = "reset" value = "清除">
</form>
</body>
</html>
结果如下图:
第二:jsp与jsp中的交互
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<table border=0>
<form action="aa.jsp" method="post" name=form>
<tr><td>请输入您的姓名:</td><td><input type="text" name="uname"></td></tr>
<tr><td>请输入密码:</td><td><input type="password" name="upass"></td></tr>
<tr><td>请输入年龄:</td><td><input type="text" name="uage"></td></tr>
<tr><td>请选择您的性别:</td><td>
<input type="radio" value="男" name="usex" checked>男
<input type="radio" value=“女” name="usex">女</td></tr>
<tr><td>请选择您的爱好:</td><td>
<input type ="checkbox" value="足球" name="uhobby">足球
<input type="checkbox" value ="乒乓球" name="uhobby">乒乓球
<input type="checkbox" value ="跑步" name="uhobby">跑步
</td></tr>
<tr><td>请选择你的班级:</td><td>
<select name="uclass">
<option vlaue="1701">计科1701班
<option value="1702" selected>计科1702班
<option value="1706">计科1703班
</select></td></tr>
<tr><td>
<input type="submit" value="提交" name="n1"/></td>
<td><input type="reset" value ="重置" name="n2"/></td>
</tr>
</form>
</table>
</body>
</html>
aa.jsp文件
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
request.setCharacterEncoding("utf-8");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
String uname=request.getParameter("uname");
String upass=request.getParameter("upass");
String uage=request.getParameter("uage");
String usex=request.getParameter("usex");
String uhobby[]=request.getParameterValues("uhobby");
out.print("用户输入的用户名为:"+uname+"<br>");
out.print("用户输入的密码为:"+upass+"<br>");
out.print("用户输入的年龄为:"+uage+"<br>");
out.print("用户选择的性别为:"+usex+"<br>");
out.print("用户选择的爱好为:"+"<br>");
for(int i=0;i<uhobby.length;i++){
out.print(uhobby[i]+"  ");
}
%>
</body>
</html>
结果如下图: