柯侧耳倾听者 - 云代码空间
—— 翱翔在Java世界的海洋之上
一、实验目的
熟练掌握JavaBean和useBean的作用及意义、熟练使用jsp:useBean。
二、预习内容及要求(要求写出预习内容)
(1)JavaBean分类:可视化Bean和不可视化Bean。不可视化Bean又分为值javaBean和工具JavaBean。重点是后者。
(2)常用工具JavaBean有特殊字符处理、中文乱码处理、数据库操作工具等。
(3)程序中直接使用对象的创建与使用jsp:useBean的异同。
三、实验内容、操作过程及实验结果记录
(1)登陆页。使用两种方式实现(创建类的对象与jsp:useBean)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<h2>演示登录,静态网页——表单</h2> <br>
<hr/>
<form action="doLogin2.jsp" method="post">
<!-- form action="doLogin.jsp" method="post" -->
<h2>用户登录</h2>
用户名:<input type="text" name="userName"/>
<br/>
密 码:<input type="text" name="userPassword"/>
<br/> <BR/>
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
</form>
</body>
</html>
<%@ 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>
<h2>使用request.getParameter,显示Login.jsp中输入的内容</h2> <br>
<%
String userName=request.getParameter("userName");
if(userName==null) userName="空";
String userPassword=request.getParameter("userPassword");
if(userPassword==null) userPassword="空";
%>
userName:<%=userName %>
<br/>
password:<%=userPassword %>
<br/><br/>
<a href="index.html">返回重新输入</a>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="cn.edu.xfu.*" %>
<%
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 'doLogin2.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>
<jsp:useBean id="user" class="cn.edu.xfu.User">
<jsp:setProperty name="user" property="*"/>
</jsp:useBean>
user:<jsp:getProperty name="user" property="userName"/>
<br/>
pwd:<jsp:getProperty name="user" property="userPassword"/>
</body>
</html>
(2)简易留言本。包括合法性验证、特殊字符处理、中文乱码处理,使用ArrayList实现全部留言的显示。
<%@ 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">
-->
<script>
function test(){
name=document.getElementById("author").value;
title=document.getElementById("title").value;
content=document.getElementById("content").value;
//alert("xxxxx");
if(name==""||title==""||content=="") {
alert("有项目为空,请填写!");
return false;
return true;
}
}
</script>
</head>
<body>
<h1>LiuYan</h1> <hr>
<!-- 有项目为空,不能提交奥。请填写! -->
<form method="post" action="do.jsp" onsubmit="return test()">
author:<input type="text" name="author"><br/>
title:<input type="text" name="title"/><br/>
content:<textarea name="content" rows="8" cols="34"></textarea><br/>
<input type="submit" value="TiJiao" />
<input type="reset" value="Reset"/>
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="cn.edu.xfu.*" %>
<%
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 'do.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>
<jsp:useBean id="liuyan" class="cn.edu.xfu.LiuYan" scope="request">
<jsp:setProperty name="liuyan" property="*"/>
</jsp:useBean>
<%
request.setCharacterEncoding("gb2312");
ArrayList<LiuYan> liuyanList=null;
liuyanList=(ArrayList<LiuYan> )application.getAttribute("liuyanList");
if(liuyanList!=null)
for(int i=0;i<liuyanList.size();i++){
LiuYan a=(LiuYan)liuyanList.get(i); //一条留言
out.print(a.getAuthor()+"<br>");
out.print(a.getTitle()+"<br>");
out.print(a.getContent()+"<br><hr>");
}
else liuyanList=new ArrayList(); //为空说明是第一次留言
out.print(liuyan.getAuthor()+"<br>");
out.print(liuyan.getTitle()+"<br>");
out.print(liuyan.getContent()+"<br><hr>");
liuyanList.add(liuyan);
application.setAttribute("liuyanList",liuyanList);
%>
<br><a href="index.jsp">Continue</a>
<!--
LiuYan liuyan=new LiuYan();
String author=request.getParameter("author");
String title=request.getParameter("title");
String content=request.getParameter("content");
liuyan.setAuthor(author);
liuyan.setTitle(title);
liuyan.setContent(content);
-->
</body>
</html>
以下所有代码均为手工录入,不使用任何IDE进行录入、编译、运行。
(1)登陆页
程序代码:
package cn.edu.xfu;
/**
*@author wujiuqier, Xiangfan University
*/
public class User {
private String userName;
private String userPassword;
public User() {}
public User(String userName, String userPassword) {
this.userName=userName;
this.userPassword=userPassword;
}
public void setUserName(String userName) { this.userName=userName; }
public String getUserName() { return this.userName; }
public void setUserPassword(String userPassword) { this.userPassword=userPassword; }
public String getUserPassword() { return this.userPassword; }
}
运行结果:
(2)简易留言本
程序代码:
package cn.edu.xfu;
import java.util.ArrayList;
/**
*@author wujiuqier, Xiangfan University
*/
public class LiuYan extends ArrayList {
private String author;
private String content;
private String title;
public LiuYan() {}
public LiuYan(String author, String content, String title) {
this.author=author;
this.content=content;
this.title=title;
}
public void setAuthor(String author) { this.author=author; }
public String getAuthor() { return this.author; }
public void setContent(String content) { this.content=content; }
public String getContent() { return this.content; }
public void setTitle(String title) { this.title=title; }
public String getTitle() { return this.title; }
}
运行结果:
四、实验过程中所遇问题思考与讨论(可写个人体会,或相关理论知识,根据个人具体情况选做)
通过这次的学习与实验,我进一步掌握了JavaWeb中有关JSP内置对象一 的一些方法,也更加了解了JSP内置对象一的概念,为今后更好的学习JavaWeb做好铺垫,在学习的过程中,我更加深刻的体会到与老师和同学间的交流与合作是多么的重要,所以在今后的学习与实验当中,我们要善于发现问题,更要及时与老师反馈问题所在,并与老师和同学交流学习经验,共同解决问题,这样才能更好的学好JavaWeb这门课程。