解决的代码重复编写,
1公有类2无参公有构造方法3属性私有4getter和aetter方法
JSP动作元素
为请求阶段体
1:在JSP页面使用Javabean
---new 像普通java类
Uers.java
package com.po;
public class Users {
private String username;// 用户名
private String password;// mima
public Users() {
// 默认构造方法
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" %>
<%@page import="com.po.Users" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
Users user=new Users();
user.setUsername("admin");
user.setPassword("123456");
%>
<h1>使用普通方式使用Javabean</h1>
<hr>
用户名:<%=user.getUsername() %><br>
密码:<%=user.getPassword() %><br>
</body>
</html>
---使用jsp动作标签使用javabean
<jsp:useBeans> 在jsp页面实例化或在指定范围使用javabean;
<jsp:useBen id="标识符" class="java类名" scope="作用范围"/>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<jsp:useBean id="myUsers" class="com.po.Users" scope="page"/>
<h1>使用useBean使用Javabean</h1>
<hr>
用户名:<%=myUsers.getUsername() %><br>
密码:<%=myUsers.getPassword() %><br>
</body>
</html>
<jsp:setProperty>给已经实例化的javaBean对象属性赋值:4
login.jsp
<body>
<h1>系统登录</h1>
<hr>
<form name="loginForm" action="dologin.jsp?mypass=999999" methood="post">
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="username" value="" /></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password" value="" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="登录" /></td>
</tr>
</table>
</form>
</body>
dologin.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" %>
<%@page import="com.po.Users" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<jsp:useBean id="myUsers" class="com.po.Users" scope="page"/>
<h1>setProperty动作</h1>
<hr>
<!-- 根据表单自动匹配所有的属性 -->
<%-- <jsp:setProperty name="myUsers" property="*"/> --%>
<!-- 根据表单匹配部分的属性 -->
<%-- <jsp:setProperty property="username" name="myUsers"/> --%>
<!-- 于表单无关,手工赋值属性 -->
<%-- <jsp:setProperty name="myUsers" property="username" value="ilas"/>
<jsp:setProperty name="myUsers" property="password" value="12345"/> --%>
<!-- 通过URL传参数,给属性赋值 -->
<jsp:setProperty property="username" name="myUsers"/>
<jsp:setProperty name="myUsers" property="password" param="mypass"/>
用户名:<%=myUsers.getUsername() %><br>
密码:<%=myUsers.getPassword() %><br>
</body>
</htm l>
<jsp:getProperty>获取指定javanbean对象属性值
<jsp:getProperty name="javaBean实例名" property="属性名">
<!-- 使用getProperty来获取用户名和密码 -->
用户名:<jsp:getProperty name="myUsers" property="username"/><br>
密码:<jsp:getProperty name="myUsers" property="password"/><br>
javabean作用域
使用useBean的scope属性,来指定javabean的作用范围
page//仅在当前页面有效
request//通过HtttpRequest.getAttribute()
session//通过HttpSession.getAttribute()
applicatoin//通过applicatoin.getAttribute()---可以作用 整个WEB应用
<h1>JAVAbean的四个作用范围</h1><hr>
<jsp:useBean id="myUsers" class="com.po.Users" scope="page"/><!-- page applicatio session request -->
用户名:<jsp:getProperty name="myUsers" property="username"/><br>
密码:<jsp:getProperty name="myUsers" property="password"/><br>
<!-- 使用内置对象获取用户名和密码 -->
<hr>
<%--
用户名:<%=((Users)application.getAttribute("myUsers")).getUsername() %><br>
密码:<%=((Users)application.getAttribute("myUsers")).getPassword() %><br> --%>
<hr>
<%-- 用户名:<%=((Users)session.getAttribute("myUsers")).getUsername() %><br>
密码:<%=((Users)session.getAttribute("myUsers")).getPassword() %><br> --%>
<hr>
<%-- 用户名:<%=((Users)request.getAttribute("myUsers")).getUsername() %><br>
密码:<%=((Users)request.getAttribute("myUsers")).getPassword() %><br> --%>
Model1模式
jsp接受页面,