package com.zzj.pojo;
public class User {
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;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/JspDay03/jsp/success.jsp" method="POST">
<table>
<tr>
<td>用户名:<input type="text" id="username" class="username"/></td>
</tr>
<tr>
<td>密码:<input type="password" id="password" class="password"/></td>
</tr>
<tr>
<td><input type="submit" value="登录"/></td>
</tr>
</table>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
jsp:useBean、jsp:setProperty、jsp:getProperty三个标签是配套使用的。具体到这,你的使用步骤应该如下:
1、从请求页面获取username、password参数,这种GET或POST 的参数只能用getParameter方法获取:
<%
String userName = request.getParameter("username");
String pwd = request.getParameter("password");
//浏览器端传来的数据需进行转码
userName = new String(userName.getBytes("ISO-8859-1"));
pwd = new String(pwd.getBytes("ISO-8859-1"));
%>
2、使用jsp:useBean与jsp:setProperty标签:
<!-- 实例化一个User bean-->
<jsp:useBean id="user" class = "com.zzj.pojo.User" scope="request" />
<!-- 设置该bean 的属性值-->
<jsp:setProperty name="user" property="username" value =${userName}/>
<jsp:setProperty name="user" property="password" value =${pwd}/>
3、其它地方(此处即你的返回页面中)使用jsp:getProperty获取上面User bean 的属性值
用户名为:<jsp:getProperty name="user" property="username"/>
密码为:<jsp:getProperty name="user" property="password"/>
注:jsp:useBean 默认值为page,即只是当前jsp页面有效,如果你从开始创建该bean的页面到最终返回的页面内,使该bean 都有效,应该至少
指定为request
我这上面写的3步其实都是在返回页面中,所以scope 可不用设置。
还有你的java文件中的命名最好规范些吧,采用驼峰命名法:userName,而不要写成和前端一样:username