JSP第五次作业
书P78-79, 例4-9.
<%@ page contentType = "text/html" %>
<%@ page pageEncoding = "utf-8" %>
<HTML><body bgcolor=#ffccff>
<%
double price = 98.78;
%>
<p style="font-family: 宋体; font-size: 36; color: blue">
商品编号 A1001, 价格 8765
<a href = "example4_9_receive.jsp?id=A1001&price=8765">购买</a><br>
商品编号 A1002, 价格 <%= price %>
<a href = "example4_9_receive.jsp?id=A1002&price=<%= price %>">购买</a>
</p>
</body></HTML>
<%@ page pageEncoding="utf-8" %>
<HTML><body bgcolor=#EEEEFF>
<p style = "font-family: 宋体; font-size: 36; color: blue">
<% String id = request.getParameter("id");
String price = request.getParameter("price");
%>
<b>商品编号:<%= id %></b><br>
商品价格: <%= price %>
</p></body>
</body>
</HTML>l
教材P97 实验2(计算器)
<%@ page contentType="text/html" %>
<%@ page pageEncoding="utf-8" %>
<HTML><body bgcolor=cyan>
<p style="font-family: 宋体; font-size: 18; color: black;">
<%
String numberOne = request.getParameter("numberOne");
String numberTwo = request.getParameter("numberTwo");
String operator = request.getParameter("operator");
if (numberOne == null || numberOne.length() == 0) {
response.sendRedirect("input.jsp");
return;
}
else if (numberTwo == null || numberTwo.length() == 0) {
response.sendRedirect("input.jsp");
return;
}
try {
double a = Double.parseDouble(numberOne);
double b = Double.parseDouble(numberTwo);
double r = 0;
if (operator.equals("+"))
r = a + b;
else if (operator.equals("-"))
r = a - b;
else if (operator.equals("*"))
r = a * b;
else if (operator.equals("/"))
r = a / b;
out.print(a + " " + operator + " " + b + " = " + r);
}
catch(Exception e) {
out.println("请输入数字字符");
}
%> </p></body>
</HTML>
制作一个登陆表单,输入账号和密码,如果账号密码相同,跳转到“登录成功”页面,否则跳转到“登录失败”页面。(加上JS非空验证)(选做,加验证码)
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>login page</title>
<script>
function verify() {
let user = document.getElementById("username").value;
let password = document.getElementById("password").value;
if (user == '' || password == '') {
alert("用户名与密码不能为空.");
return;
}
loginForm.submit();
}
</script>
</head>
<body>
<form action="loginRedirection.jsp" name="loginForm">
user: <input type=text id="username" name="username"/><br>
password: <input type=password id="password" name="password"/><br><%
String N = request.getParameter("N");
int number = 0;
if (N != null) {
number = Integer.parseInt(N);
}
for (int n = 0; n < number; n++) {
out.println("欢迎<br>");
}
%>
<input type="button" value="login" onclick="verify()">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Login Redirection</title>
</head>
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
String vip = request.getParameter("vip");
if (username.equals("") || password.equals("") || !password.equals(username)) {
response.sendRedirect("redirectionFailed.jsp");
}
else {
request.getRequestDispatcher("redirectionSuccessed.jsp?vip=" + vip).forward(request, response);
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Login Successed</title>
</head>
<body>
<h1>Login Successed</h1>
<%
String vip = request.getParameter("vip");
if (vip != null && vip.equals("on")) {
out.println("<h1>welcome to register to became VIP</h1>");
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
Login Failed
```html


4.
在上题的表单中增加一个checkbox,让用户选择“是否注册为会员",如果注册为会员,则在显示时增加文本“欢迎您注册为会员”
```html
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>login page</title>
<script>
function verify() {
let user = document.getElementById("username").value;
let password = document.getElementById("password").value;
if (user == '' || password == '') {
alert("用户名与密码不能为空.");
return;
}
loginForm.submit();
}
</script>
</head>
<body>
<form action="loginRedirection.jsp" name="loginForm">
user: <input type=text id="username" name="username"/><br>
password: <input type=password id="password" name="password"/><br>
<input type="checkbox" name="vip">register VIP<br/>
<input type="button" value="login" onclick="verify()">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Login Redirection</title>
</head>
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
String vip = request.getParameter("vip");
if (username.equals("") || password.equals("") || !password.equals(username)) {
response.sendRedirect("redirectionFailed.jsp");
}
else {
request.getRequestDispatcher("redirectionSuccessed.jsp?vip=" + vip).forward(request, response);
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Login Successed</title>
</head>
<body>
<h1>Login Successed</h1>
<%
String vip = request.getParameter("vip");
if (vip != null && vip.equals("on")) {
out.println("<h1>welcome to register to became VIP</h1>");
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Login Successed</title>
</head>
<body>
<h1>Login Failed</h1>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>login page</title>
<script>
</script>
</head>
<body>
<form action="work05_exercise05_page02.jsp" name="loginForm">
N: <input type=text name="N"/><br>
<input type="submit" value="submit">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>login page</title>
<script>
</script>
</head>
<body>
<form action="loginRedirection.jsp" name="loginForm">
<%
String N = request.getParameter("N");
int number = 0;
if (N != null) {
number = Integer.parseInt(N);
}
for (int n = 0; n < number; n++) {
out.println("欢迎<br>");
}
%>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>login page</title>
<script>
function verify() {
let user = document.getElementById("username").value;
let password = document.getElementById("password").value;
if (user == '' || password == '') {
alert("用户名与密码不能为空.");
return;
}
loginForm.submit();
}
</script>
</head>
<body>
<form action="week05_exercise06_01.jsp" name="loginForm">
user: <input type=text id="username" name="username"/><br>
password: <input type=password id="password" name="password"/><br><%
String N = request.getParameter("N");
int number = 0;
if (N != null) {
number = Integer.parseInt(N);
}
for (int n = 0; n < number; n++) {
out.println("欢迎<br>");
}
%>
<input type="button" value="login" onclick="verify()">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>login page</title>
</head>
<body>
账户: username<br/>
用户名: username<br/>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>login page</title>
<%
String name;
%>
</head>
<body>
</form>
</body>
</html>