JSP第六次作业
题目:
1.登录
输入用户名密码,判断用户名和密码相同,登陆成功,session中保存用户的用户名,进入主页main.jsp,主页有一个退出按钮,点击,回到登陆页login.jsp。要求:退出登录后,如果在浏览器直接输入主页main.jsp,访问不了,直接跳到登陆页。
2.购物车
和上一题一起,在main.jsp中做一个购物车,里面显示3个商品名和价格 每一个后面有一个加入购物车按钮,main.jsp中有一个按钮(或者超链接)可以显示购物车。(选作:在购物车中加删除按钮删除商品)
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Login</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="login_process.jsp" method="POST" name="loginForm">
用户名: <input type="text" id="username" name="username"><br/>
密码: <input type="password" id="password" name="password"><br/>
<input type="button" name="submitButton" value="提交" 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>Logout</title>
</head>
<body>
<%
session.removeAttribute("username");
response.sendRedirect("login.jsp");
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Login Process</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username.equals(password))
{
out.println("<h1>登录成功</h1>");
session.setAttribute("username", username);
response.sendRedirect("main.jsp");
}
else
{
out.println("<h1>登录失败</h1>");
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@page import="login.Thing"%>
<%@ page language="java" import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>main</title>
</head>
<body>
<%
Object username = session.getAttribute("username");
if (username == null)
{
response.sendRedirect("login.jsp");
}
else {
out.println(username.toString() + "<br/>");
}
Thing[] things = new Thing[3];
things[0] = new Thing();
things[0].name = "商品0";
things[0].price = 1.0;
things[1] = new Thing();
things[1].name = "商品1";
things[1].price = 1.0;
things[2] = new Thing();
things[2].name = "商品2";
things[2].price = 2.0;
for (int n = 0; n < things.length; n++)
{
out.println("<a href='buy.jsp?name=" + things[n].name + "&price=" + things[n].price + "'>" + things[n].name + " " + things[n].price + "</a><br/>");
}
session.setAttribute("things", new ArrayList<Thing>());
%>
<a href="logout.jsp">退出</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page language="java" import="java.util.*"%>
<%@ page import="login.Thing"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Buy Page</title>
</head>
<body>
<%
ArrayList<Thing> things;
things = (ArrayList<Thing>)session.getAttribute("things");
Thing thing = new Thing();
thing.name = request.getParameter("name");
thing.price = Double.parseDouble(request.getParameter("price"));
things.add(thing);
session.setAttribute("things", things);
for (int n = 0; n < things.size(); n++)
{
out.println("<p>" + things.get(n).name + "</p>" + "<p>" + things.get(n).price + "元</p>" + "<br/>");
}
%>
</body>
</html>