@nalan90
2018-06-29T11:16:27.000000Z
字数 7864
阅读 623
JAVAEE开发
<jsp:useBeans>
<jsp:getProperty>
<jsp:setProperty>
示例代码:
public class Students {
private String name;
private int age;
public Students() {}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
}
<jsp:useBean>
<jsp:setProperty>
<jsp:getProperty>
2、JSP1.2就开始有的基本元素,包括6个动作元素
<jsp:include>
<jsp:forward>
<jsp:param>
<jsp:plugin>
<jsp:params>
<jsp:fallback>
3、JSP2.0新增的元素,主要与JSP Document有关,包括6个元素
<jsp:root>
<jsp:declaration>
<jsp:scriptlet>
<jsp:expression>
<jsp:text>
<jsp:output>
<jsp:attribute>
<jsp:body>
<jsp:element>
<jsp:invoke>
<jsp:dobody>
2、在JSP页面中通常使用JSP动作标签使用javabean
<jsp:useBean id="标示符" class="java类名" scope="作用范围" />
<jsp:setProperty name="Javabean实例名" property="*" /> (跟表单关联)
<jsp:setProperty name="Javabean实例名" property="Javabean属性名" /> (跟表单关联)
<jsp:setProperty name="Javabean实例名" property="Javabean属性名" value="BeanValue"/> (手工设置)
<jsp:setProperty name="Javabean实例名" property="propertyName" param="request对象的参数名"/> (跟request参数关联)
<jsp:getProperty name="Javabean实例名" property="属性名"/>
示例代码
## Users.java
package com.po;
public class Users {
private String name;
private String password;
public Users() {
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
}
## login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<h1>用户登录</h1>
<hr>
<form action="dologin.jsp?mypass=999999" name="loginForm" method="post">
用户名: <input type="text" name="name"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="login">
</form>
</body>
</html>
## dologin.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
%>
<jsp:useBean id="myUsers" class="com.po.Users" scope="page"/>
<h1>setProperty动作元素</h1>
<hr>
<!-- 根据表单自动匹配所有的属性 -->
<%--
<jsp:setProperty name="myUsers" property="*"/>
--%>
<!-- 根据表单匹配部分的属性 -->
<%--
<jsp:setProperty name="myUsers" property="name" />
--%>
<!--与表单无关,通过手工赋值给属性-->
<%--
<jsp:setProperty name="myUsers" property="name" value="张爽"/>
<jsp:setProperty name="myUsers" property="password" value="888888"/>
--%>
<!--通过URL传参给属性赋值-->
<jsp:setProperty name="myUsers" property="name"/>
<jsp:setProperty name="myUsers" property="password" param="mypass"/>
<!--使用传统的表过式获取用户名跟密码-->
<%--
用户名: <%=myUsers.getName()%><br>
密码:<%=myUsers.getPassword()%><br>
--%>
<!--使用getProperty方式来获取用户名跟密码-->
用户名: <jsp:getProperty name="myUsers" property="name"/><br>
密码: <jsp:getProperty name="myUsers" property="password"/>
</body>
</html>
作用范围
示例代码
## login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<h1>用户登录</h1>
<hr>
<form action="dologin.jsp?mypass=999999" name="loginForm" method="post">
用户名: <input type="text" name="name"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="login">
</form>
</body>
</html>
## dologin.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
%>
<%--<jsp:useBean id="myUsers" class="com.po.Users" scope="application"/>--%>
<%--<jsp:useBean id="myUsers" class="com.po.Users" scope="session"/>--%>
<%--<jsp:useBean id="myUsers" class="com.po.Users" scope="request"/>--%>
<%--<jsp:useBean id="myUsers" class="com.po.Users" scope="page"/>--%>
<h1>setProperty动作元素</h1>
<hr>
<!-- 根据表单自动匹配所有的属性 -->
<%--
<jsp:setProperty name="myUsers" property="*"/>
--%>
<!-- 根据表单匹配部分的属性 -->
<%--
<jsp:setProperty name="myUsers" property="name" />
--%>
<!--与表单无关,通过手工赋值给属性-->
<%--
<jsp:setProperty name="myUsers" property="name" value="张爽"/>
<jsp:setProperty name="myUsers" property="password" value="888888"/>
--%>
<!--通过URL传参给属性赋值-->
<jsp:setProperty name="myUsers" property="name"/>
<jsp:setProperty name="myUsers" property="password" param="mypass"/>
<!--使用传统的表过式获取用户名跟密码-->
<%--
用户名: <%=myUsers.getName()%><br>
密码:<%=myUsers.getPassword()%><br>
--%>
<!--使用getProperty方式来获取用户名跟密码-->
用户名: <jsp:getProperty name="myUsers" property="name"/><br>
密码: <jsp:getProperty name="myUsers" property="password"/>
<br>
<br>
<a href="testScope.jsp">测试Javabean的四个作用域范围</a>
<%--
<%
//request的作用域无法通过超链接传递
request.getRequestDispatcher("testScope.jsp").forward(request, response);
%>
--%>
</body>
</html>
## testScope.jsp
<%@ page import="com.po.Users" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>Javabean的四种作用范围</h1>
<%--<jsp:useBean id="myUsers" class="com.po.Users" scope="application"/>--%>
<%--<jsp:useBean id="myUsers" class="com.po.Users" scope="session"/>--%>
<%--<jsp:useBean id="myUsers" class="com.po.Users" scope="request"/>--%>
<%--<jsp:useBean id="myUsers" class="com.po.Users" scope="page"/>--%>
用户名: <jsp:getProperty name="myUsers" property="name"/><br>
密码: <jsp:getProperty name="myUsers" property="password"/>
<hr>
<!-- 使用内置对象获取用户名跟密码-->
<%--
用户名: <%=((Users)application.getAttribute("myUsers")).getName()%><br>-
密码: <%=((Users)application.getAttribute("myUsers")).getPassword()%>
--%>
<%--
用户名: <%=((Users)session.getAttribute("myUsers")).getName()%><br>
密码: <%=((Users)session.getAttribute("myUsers")).getPassword()%>
--%>
<%--
用户名: <%=((Users)request.getAttribute("myUsers")).getName()%><br>
密码: <%=((Users)request.getAttribute("myUsers")).getPassword()%>
--%>
<%--
<%
String name = "";
String password = "";
if (pageContext.getAttribute("myUsers") != null) {
name = ((Users)pageContext.getAttribute("myUsers")).getName();
password = ((Users)pageContext.getAttribute("myUsers")).getPassword();
}
%>
用户名: <%=name%><br>
密码: <%=password%>
--%>
</body>
</html>
## UsersDAO.java
package com.dao;
import com.po.Users;
public class UsersDAO {
public boolean usersLogin(Users u) {
if ("admin".equals(u.getName()) && "admin".equals(u.getPassword())) {
return true;
} else {
return false;
}
}
}
## Users.java
package com.po;
public class Users {
private String name;
private String password;
public Users() {
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
}
## login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<h1>用户登录</h1>
<hr>
<form action="dologin.jsp?mypass=999999" name="loginForm" method="post">
用户名: <input type="text" name="name"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="login">
</form>
</body>
</html>
##dologin.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
request.setCharacterEncoding("utf-8");
%>
<jsp:useBean id="loginUser" class="com.po.Users" scope="page"/>
<jsp:useBean id="userDAO" class="com.dao.UsersDAO" scope="page"/>
<jsp:setProperty name="loginUser" property="*"/>
<%
if (userDAO.usersLogin(loginUser)) {
session.setAttribute("loginUser", loginUser.getName());
request.getRequestDispatcher("login_success.jsp").forward(request, response);
} else {
response.sendRedirect("login_fail.jsp");
}
%>
## login_success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录成功</title>
</head>
<body>
<%
String loginUser = "";
if (session.getAttribute("loginUser") != null) {
loginUser = session.getAttribute("loginUser").toString();
}
%>
欢迎您<%=loginUser%>
</body>
</html>
## login_fail.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录失败</title>
</head>
<body>
<h3>登录失败</h3>
<a href="login.jsp">重新登录</a>
</body>
</html>