JSP--(jsp:useBean动作标识的具体应用)


(1)创建一个名为UserInfo的javaBean,用来保存用户信息:
package com.Bean;

public class UserInfo {
	private String name;
	private String job;
	
	public UserInfo(){
		name="";
		job="";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) throws Exception {
		this.name = new String(name.getBytes("ISO-8859-1"),"gbk");
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) throws Exception {
		this.job =  new String(job.getBytes("ISO-8859-1"),"gbk");
	}
}


(2)创建提供用户输入注册信息的页面index.jsp:
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>用户注册</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
 <center>
   <form action="doreg.jsp" method="get">
   <table border="1" style="margin-top:150" width="250">
     <tr height="25" bgcolor="lightgrey">
      <td align="center" colspan="2">用户注册</td>
     </tr>
     <tr>
      <td align="right">用户名:</td>
      <td align="center"><input type="text" name="name" size="29"></td>
     </tr>
     <tr>
      <td align="right">职  务:</td>
      <td align="center"><input type="text" name="job" size="29"></td>
     </tr>
     <tr>
      <td align="center" colspan="2">
        <input type="submit" value="注册">
        <input type="reset" value="重置">      
      </td>
     </tr>
   </table>
   </form>
   <br>
 </center>
</body>
</html>


(3)创建接受form表单的jsp页面doreg.jsp,在其中判断用户输入的信息并将请求转发至指定的页面中
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<jsp:useBean id="us" class="com.Bean.UserInfo" scope="request"/>
<jsp:setProperty name="us" property="*"/>
<%
   String name=us.getName();
   String job=us.getJob();
   
   if(name.equals("")||job.equals("")){
%>
   <jsp:forward page="/false.jsp"/>
<% }else{ %>
   <jsp:forward page="/success.jsp"/>
<% } %>


(4)创建false.jsp页面,显示注册失败提示信息
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>注册失败!</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
 <center>
   <table border="1" height="100" width="250" style="margin-top:150">
     <tr bgcolor="lightgrey" height="25">
        <td align="center">注册失败!</td>
     </tr>
     <tr>
        <td align="center">请输入 <b>用户名</b> 或 <b>职务</b>!</td>
     </tr>
   </table>
   <a href="index.jsp">返回</a>
 </center>
</body>
</html>


(5)创建success.jsp页面,显示注册成功提示信息
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<%@ page import="com.Bean.UserInfo" %>
<%
  String username=((UserInfo)request.getAttribute("us")).getName();
  String userjob=((UserInfo)request.getAttribute("us")).getJob();
%>
<html>
<head>
<title>注册成功</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
 <center>
   <table border="1" width="250" height="100" style="margin-top:150" >
     <tr height="25" bgcolor="lightgrey">
       <td align="center">注册成功</td>
     </tr>
     <tr>
       <td align="center">
         <b>用户名:</b><%=username%>
           
         <b>职  务:</b><%=userjob%>
       </td>
     </tr>
   </table>
   <a href="index.jsp">返回</a>
 </center>
</body>
</html>

最终实现结果:


注册成功:



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值