JSP中的usebean用法示例
首先有3个页面,一个是login.jsp,用于输入用户名和密码,另外一个是dologin.jsp,用于接收和显示用户名和密码,还有一个是java文件
login.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="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>
<center>
<form action="dologin.jsp" method="post">
<table>
<tr>
<td>请输入你的姓名:<input type="text" name="username"></td>
</tr>
<tr>
<td>请输入你的密码:<input type="password" name="password"></td>
</tr>
<tr align="right"><td><input type="submit" value="提交" ></td></tr>
</table>
</form>
</center>
</body>
</html>
dologin.jsp
<%@ page language="java" import="java.util.*" pageEncoding="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 ‘dologin.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>
设置了属性方法<br>
<jsp:useBean id="user" class="com.xywei.bean.UserBean" scope="session">
<jsp:setProperty name="user" property="username" param="username" />
<jsp:setProperty name="user" property="password" param="password" />
<%--也可以这样做<jsp:setProperty name="user" property="*" />--%>
</jsp:useBean>
下面获取usebean里面的值<br>
用户名:<jsp:getProperty name="user" property="username"/><br>
密码: <jsp:getProperty name="user" property="password"/><br>
</body>
</html>
Java文件
UserBean.java
package com.xywei.bean;
public class UserBean {
private String username;
private String password;
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;
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。