Java Web程序设计

刘肃平、聂军、王华、李勇波、魏碧晴

目录

  • 1 Java Web概述
    • 1.1 任务导引
    • 1.2 动态网页技术介绍
    • 1.3 JSP的运行环境
    • 1.4 JSP的运行原理
    • 1.5 JSP程序开发模式
    • 1.6 JSP开发工具
    • 1.7 任务实施:JSP开发环境搭建及第一个Hello World程序
  • 2 JSP基本语法
    • 2.1 任务导引
    • 2.2 JSP的基本组成
    • 2.3 脚本元素
    • 2.4 指令元素
    • 2.5 动作元素
    • 2.6 任务实施
  • 3 服务器交互
    • 3.1 任务导引
    • 3.2 JSP内置对象概述
    • 3.3 out对象
    • 3.4 request对象
    • 3.5 response对象
    • 3.6 session对象
    • 3.7 application对象
    • 3.8 其他内置对象
    • 3.9 任务实施:使用JSP内置对象实现登录和注销功能
  • 4 JavaBean技术
    • 4.1 任务引导
    • 4.2 JavaBean概述
    • 4.3 JavaBean的应用
    • 4.4 JavaBean的作用域
    • 4.5 任务实施
    • 4.6 JSP中的文件操作
  • 5 Servlet技术
    • 5.1 任务引导
    • 5.2 Servlet基础
    • 5.3 Servlet开发
    • 5.4 Servlet常用API
    • 5.5 任务实施
  • 6 第6章 过滤器和监听器
    • 6.1 任务导引
    • 6.2 Servlet过滤器
    • 6.3 过滤器创建与配置
    • 6.4 Servlet监听器
    • 6.5 Servlet3.0新特性
    • 6.6 任务实施
  • 7 Java Web的数据库操作
    • 7.1 任务导引
    • 7.2 JDBC技术
    • 7.3 使用JDBC驱动程序访问数据库
    • 7.4 数据库连接的JavaBean
    • 7.5 JDBC对事务的操作
    • 7.6 连接池技术
    • 7.7 任务实施
    • 7.8 扩展阅读
  • 8 JSP高级技术
    • 8.1 任务导引
    • 8.2 EL概述
    • 8.3 JSTL标签库简介
    • 8.4 JSTL核心标签
    • 8.5 JSP与Ajax
    • 8.6 任务实施
    • 8.7 扩展阅读
  • 9 JSP实用组件技术
    • 9.1 任务导引
    • 9.2 上传与下载组件
    • 9.3 发送E-mail
    • 9.4 JSP动态图表
    • 9.5 在线编辑器
    • 9.6 任务实施
任务实施

定义用户类User的JavaBean,在登录成功的user2.jsp页面中使用JavaBean。

4.5.1 构建User的JavaBean

User.java文件:

package cn.cszyedu.po;


public class User {

private String userName;

private String userPwd;

private String eMail;

private String telephone;

private Integer loginTimes;

public User() {

super();

}

public User(String userName, String userPwd, String eMail,

String telephone, Integer loginTimes) {

super();

this.userName = userName;

this.userPwd = userPwd;

this.eMail = eMail;

this.telephone = telephone;

this.loginTimes = loginTimes;

}

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getUserPwd() {

return userPwd;

}

public void setUserPwd(String userPwd) {

this.userPwd = userPwd;

}

public String geteMail() {

return eMail;

}

public void seteMail(String eMail) {

this.eMail = eMail;

}

public String getTelephone() {

return telephone;

}

public void setTelephone(String telephone) {

this.telephone = telephone;

}

public Integer getLoginTimes() {

return loginTimes;

}

public void setLoginTimes(Integer loginTimes) {

this.loginTimes = loginTimes;

}

@Override

public String toString() {

return "User [userName=" + userName + ", userPwd=" + userPwd

+ ", eMail=" + eMail + ", telephone=" + telephone

+ ", loginTimes=" + loginTimes + "]";

}

}

login.jsp文件和4.4中的一样。

login_action.jsp文件和4.4中的基本一致,只是将登录成功页面跳转到user2.jsp:

pageContext.foward("user2.jsp");

css/user.css和4.4中的一样。

4.5.2 使用JavaBean

jsp./user2.jsp文件:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <title>员工之窗</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="../css/user.css">

  </head>

  <body >

 

     <div class="div_01">

      <h1>员工之窗</h1>

     </div>

     <jsp:useBean id="date" class="java.util.Date" /> 

     <jsp:useBean id="user" class="cn.cszyedu.po.User" scope="session"></jsp:useBean> 


<jsp:setProperty name="user"  property="userName"/>

     <jsp:setProperty name="user"  property="userPwd" />

     

     <div class="div_02">

      <span class="span_01"> <jsp:getProperty name="user" property="userName"/>

      <jsp:getProperty name="user" property="userPwd"/></span>,欢迎你来到员工之窗。

      <br/>

      当前时间:<div id="span_02"><%= date %></div>

     </div>

     <div class="div_03">

      <table border="1" cellspacing="0" align="center">

      <tr>

      <th id="th_01">文章编号</th>

      <th id="th_01">文章类型</th>

      <th id="th_01">标题</th>

      <th id="th_01">作者</th>

      <th id="th_01">发表日期</th>

      </tr>

      </table><br/>

      <a href="${pageContext.request.contextPath }/jsp/publish.jsp">【发表文章】</a>&nbsp;&nbsp;&nbsp;

      <a href="${pageContext.request.contextPath }/jsp/out.jsp">【退出登录】</a>&nbsp;&nbsp;&nbsp;

      <a href="${pageContext.request.contextPath }/index.jsp">【返回首页】</a>

     </div>

  </body>

</html>

效果图如下: