JavaEE实验二

《JavaEE开发技术》课程第二周实验题目和代码。

第一题 登录系统实验

(1) 建立 3 个 JSP 页面,第 1 个页面输入用户登录信息、第 2 个页面用于检测用户登录信息、第3 个页面用于显示登录结果。
(2) 第 2 个页面中使用 JSP 脚本、jsp:forward、request 等基础语法、动作指令和内置对象。
(3) 第 3 个页面中使用 JSP 表达式和 request 内置对象。

第一个页面Login.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<%@ 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 'Login.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>
<form action="loginCL.jsp" method="post" name=form><br>

<h1>Please input your message:</h1><br>

用户名:
<input type="text" name="username"><br><br>

密&nbsp;&nbsp;&nbsp;&nbsp;码:
<input type="password" name="password"><br><br><br><br>

<input type="submit" value="登陆" name="submit">

<input type="reset" value="重置"><br>

</form>

<%request.setAttribute("usertxt", " "); %>
<%=(String)request.getAttribute("usertxt") %>

</body>
</html>


第二个页面loginCL.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
 <%@ 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 'loginCL.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>

<%

String referer = request.getHeader("Referer");

//判断是否从Login页面跳转过来
if(referer==null||!referer.equals("http://localhost:8080/Login/Login.jsp")){

response.sendRedirect("Login.jsp");

}

//设置对客户端请求和数据库取值时的编码
request.setCharacterEncoding("utf-8");

response.setContentType("text/html;charset=utf-8");

String name=request.getParameter("username");

String password=request.getParameter("password");


//判断登陆界面的用户名和密码,根据输入的不同情况进行不同的处理
if(name.trim().length()!=0 && password.trim().length()!=0){

request.setAttribute("usertxt", "正常登录!!!");

request.getRequestDispatcher("success.jsp").forward(request, response);

}

else{

request.setAttribute("usertxt", "用户名或密码错误!!!");

request.getRequestDispatcher("Login.jsp").forward(request, response);

}



%>
</body>
</html>

```

第三个页面success.jsp
```javascript
<%@ 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 'success.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>
<%=(String)request.getAttribute("usertxt") %><br>
<%

String referer = request.getHeader("Referer");

if(referer==null||!referer.equals("http://localhost:8080/Login/Login.jsp")){

response.sendRedirect("Login.jsp");

}

request.setCharacterEncoding("utf-8");

response.setContentType("text/html;charset=utf-8");

String name=request.getParameter("username");

String password=request.getParameter("password");

out.println("usename: "+name+"<br>");

out.println("pwd: "+password+"<br>");

%>  
</body>
</html>

第二题 注册学生用户信息实验

(1) 建立 3 个 JSP 页面,第 1 个页面输入学生基本信息、第 2 个页面构建一个 JavaBean 实例,并将学生信息存入到实例中、第 3 个页面用于显示学生的注册信息。注册页面内容包括:用户名、密码、性别、年龄、国别、邮箱、电话号码。
(2) 第 2 个页面中使用 jsp:useBean 和 jsp:setProperty 动作指令、request 内置对象。
(3) 第 3 个页面中使用 jsp:getProperty 动作指令。

第一个页面register.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<%@ 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 'register.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>
<%
request.setCharacterEncoding("utf-8");
%>
<jsp:useBean id="reg" scope="request" class="test.register"/>
<form action="check.jsp" method="post" name=form><br>

<h1>Register:</h1><br>

用户名:
<input type="text" name="name" value="<jsp:getProperty name="reg" property="name"/>">
<%=reg.getErrorMsg("errname") %><br><br>

密&nbsp;&nbsp;&nbsp;&nbsp;码:
<input type="password" name="password" value="<jsp:getProperty name="reg" property="pwd"/>"><br><br>

性&nbsp;&nbsp;&nbsp;&nbsp;别:
<input type="radio" id="ids" name="sex" value="男" checked="checked" />男
<input type="radio" id="ids" name="sex" value="女" />女<br><br>


年&nbsp;&nbsp;&nbsp;&nbsp;龄:
<input type="text" name="age" value="<jsp:getProperty name="reg" property="age"/>">
<%=reg.getErrorMsg("errage") %><br><br>

国&nbsp;&nbsp;&nbsp;&nbsp;别:
<input type="text" name="nation"><br><br>

邮&nbsp;&nbsp;&nbsp;&nbsp;箱:
<input type="text" name="email" value="<jsp:getProperty name="reg" property="email"/>">
<%=reg.getErrorMsg("erremail") %><br><br>

电话号码:
<input type="text" name="phone"><br><br>
<br><br>

<input type="submit" value="注册" name="submit">

<input type="reset" value="重置"><br>

</form>
</body>
</html>
```

第二个页面check.jsp
``` javascript
<%@ 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 'check.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>
<%
request.setCharacterEncoding("utf-8");
%>

<%--将表单数据传到Javabean实例 --%>
<jsp:useBean id="reg" scope="request" class="test.register"/>
<jsp:setProperty property="*" name="reg"/>
<jsp:setProperty property="pwd" name="reg" param="password"/>

<%--JavaBean中方法调用判断是否是合法输入 --%>
if(reg.isValidate()){
request.getRequestDispatcher("success.jsp").forward(request, response);
}

else{
request.getRequestDispatcher("register.jsp").forward(request, response);
}
%>

</body>
</html>

```

第三个页面success.jsp
``` javascript
<%@ 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 'success.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="reg" scope="request" class="test.register"/>
<h2>注册信息</h2><br>

<%--显示注册信息从JavaBean获取 --%>
<table>
<tr>
<td>用户名</td>
<td><jsp:getProperty property="name" name="reg"/></td>
</tr>

<tr>
<td>密码</td>
<td><jsp:getProperty property="pwd" name="reg"/></td>
</tr>

<tr>
<td>性别</td>
<td><jsp:getProperty property="sex" name="reg"/></td>
</tr>

<tr>
<td>年&nbsp;&nbsp;&nbsp;&nbsp;龄</td>
<td><jsp:getProperty property="age" name="reg"/></td>
</tr>

<tr>
<td>国家</td>
<td><jsp:getProperty property="nation" name="reg"/></td>
</tr>

<tr>
<td>E-MAIL</td>
<td><jsp:getProperty property="email" name="reg"/></td>
</tr>

<tr>
<td>手机</td>
<td><jsp:getProperty property="phone" name="reg"/></td>
</tr>
</table>
</body>
</html>


JavaBean对应的Java类 register.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package test;
import java.util.HashMap;
import java.util.Map;

public class register {
private String name; // 名字
private String pwd; //密码
private String sex; //性别
private String age; // 年龄 为了防止用户输入的内容不是数字时,抛出NumberFormat异常,这里使用String存储
private String nation; //国籍
private String email; // email
private String phone; //手机号码
private Map<String,String> errors = null; // 保存错误信息

//初始化
public register() {
this.name = "";
this.pwd = "";
this.sex = "";
this.age = "";
this.nation = "";
this.email = "";
this.phone = "";
this.errors = new HashMap<String,String>();
}

//判断部分属性是否合法
public boolean isValidate() {
boolean flag = true;
if(!this.name.matches("\\w{6,15}")) {
flag = false;
this.name = ""; // 清空原本的name内容
this.errors.put("errname", "用户名是6~15位的字母或数字");
}
if(!this.email.matches("\\w+@\\w+\\.\\w+\\.?\\w*")) {
flag = false;
this.email = ""; // 清空原本的email内容
this.errors.put("erremail", "输入的email的地址不合法");
}
if(!this.age.matches("\\d+")) {
flag = false;
this.age = ""; // 清空原本的email内容
this.errors.put("errage", "年龄只能是数字");
}
return flag;
}

//获取错误提示信息
public String getErrorMsg(String key) {
String value = this.errors.get(key);
return value==null?"":value;
}

//姓名的获取和设置
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

//密码的获取和设置
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
System.out.println("setPWD:"+this.pwd);
}

//年龄的获取和设置
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}

//性别的获取和设置
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}

//国籍的获取和设置
public String getNation() {
return nation;
}
public void setNation(String nation) {
this.nation = nation;
}

//错误的获取和设置
public Map<String, String> getError() {
return errors;
}
public void setError(Map<String, String> error) {
this.errors = error;
}

//邮箱的获取和设置
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}

//手机号码的获取和设置
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}

打赏
  • © 2025 Aoxue
  • Hexo Theme Ayer by shenyu
    • PV:
    • UV:

请我喝杯咖啡吧~

支付宝
微信