(1)input.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>提交页面</title>
</head>
<body>
<h3>请输入两个整数:</h3>
<form action="show.jsp" method="post">
加数<input type="text" name="s1"><br/>
被加数<input type="text" name="s2"><br/>
<input type="submit" value="确 定">
</form>
</body>
</html>
(2)show.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="beans.Add"%>
<!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>输出界面</title>
</head>
<body>
<%request.setCharacterEncoding("UTF-8"); %>
<%
int x=Integer.parseInt(request.getParameter("s1"));
int y=Integer.parseInt(request.getParameter("s2"));
Add add=new Add(x,y);
%>
<%=add.sum() %>
</body>
</html>
(3)Add.java
package beans;
public class Add {
private int s1;
private int s2;
public Add(){};
public Add(int s1,int s2)
{
this.s1=s1;
this.s2=s2;
}
public int getS1() {
return s1;
}
public void setS1(int s1) {
this.s1 = s1;
}
public int getS2() {
return s2;
}
public void setS2(int s2) {
this.s2 = s2;
}
public int sum()
{
return s1+s2;
}
}
基于JSP标签访问JavaBean
(1)input.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>提交页面</title>
</head>
<body>
<h3>请输入两个整数:</h3>
<form action="show.jsp" method="post">
加数<input type="text" name="s1"><br/>
被加数<input type="text" name="s2"><br/>
<input type="submit" value="确 定">
</form>
</body>
</html>
(2)show.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="beans.Add"%>
<!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>输出界面</title>
</head>
<body>
<%request.setCharacterEncoding("UTF-8"); %>
<%
String x=request.getParameter("s1");
String y=request.getParameter("s2");
int x1=Integer.parseInt(x);
int y1=Integer.parseInt(y);
%>
<jsp:useBean id="add" class="beans.Add" scope="request"></jsp:useBean>
<jsp:setProperty property="*" name="add"/>
<p>调用jsp:getProperty作标签页<br/>
<jsp:getProperty property="s1" name="add"/>+<jsp:getProperty property="s2" name="add"/>=<%=add.sum()%><br/>
</p>
<p>调用使用类的方法获取数据并显示:<br/>
<%=add.getS1()%>+<%=add.getS2()%>=<%=add.sum()%><br/>
</p>
<p>
注释:<br/>
jsp:setProperty property="*" name="add"/由表单自动赋值给对象<br/>
jsp:useBean id="变量名" class="包、类" scope=""<br/>
jsp:getProperty property="属性名" name="变量名"/
</p>
</body>
</html>
(3)Add.java
Add类与上一个相同
网页计数器
(1)counter1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="beans.Counter"%>
<!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>网页数量</title>
</head>
<body>
<jsp:useBean id="c" class="beans.Counter" scope="application"></jsp:useBean>
这次访问的第一个页面:counter1.jsp!<br/>
两个页面共被访问:
<jsp:getProperty name="c" property="count"/>
<a href="counter2.jsp">下一个</a>
</body>
</html>
(2)Counter.java
package beans;
public class Counter {
private int count;
public Counter(){count=0;}
public int getCount()
{
count++;
return count;
}
public void setCount(int count) {
this.count = count;
}
}
(3)counter2.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>网页数量</title>
</head>
<body>
<jsp:useBean id="c" class="beans.Counter" scope="application"></jsp:useBean>
这次访问的第2个页面:counter1.jsp!<br/>
两个页面共被访问:
<jsp:getProperty name="c" property="count"/>
</body>
</html>