首先有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;
}
}