JSP第八周作业
1.登陆
输入用户名密码,判断用户名和密码相同,登陆成功,session中保存用户的用户名,进入主页main.jsp,主页有一个退出按钮,点击,回到登陆页login.jsp。要求:退出登录后,如果在浏览器直接输入主页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>
</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>
<%@ 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>
2.购物车
和上一题一起,在main.jsp中做一个购物车,里面显示3个商品名和价格 每一个后面有一个加入购物车按钮,main.jsp中有一个按钮(或者超链接)可以显示购物车。(选作:在购物车中加删除按钮删除商品)
<%Stringuname=(String) session.getAttribute("uname");if(uname==null)
response.sendRedirect("login.jsp");%>
<formaction="buy.jsp">是否加入购物车?<br>
<inputtype="checkbox"name="item"value="grape">葡萄 $1<br>
<inputtype="checkbox"name="item"value="banana">香蕉 $2<br>
<inputtype="checkbox"name="item"value="apple">苹果 $3<br>
<inputtype="submit"value="加入购物车">
</form>
<formname="form1"action="logout.jsp"method="post">
<inputtype="submit"value="退出登录">
</form>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>My JSP 'buy.jsp' starting page</title>
</head>
<body>
<h2>购物车</h2>
<%request.setCharacterEncoding("utf-8");StringitemName[]=request.getParameterValues("item");
if(itemName==null) {
out.print("购物车为空");
}else{
for(intk=0; k<itemName.length; k++) {
out.print("("+(k+1)+")"+itemName[k]+"<br>");
}
}%>
</body>
</html>