码迷,mamicode.com
首页 > Web开发 > 详细

JSP中的usebean用法示例

时间:2015-02-16 23:26:55      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

首先有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>
密码:&nbsp;<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;
}

}

 

JSP中的usebean用法示例

标签:

原文地址:http://www.cnblogs.com/listentothecloud20150215/p/4294584.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!