实验目的
- 掌握JSP内置对象的使用;
- 内置对象包括cookies\session\application\page\exception\config等。
实验内容
【1】创建四个JSP网页,文件名为login.jsp、process.jsp、index1.jsp和index2.jsp,页面需要以下功能:
- 在login.jsp,具有两个输入框和一个按钮的表单,用以输入用户名和密码,该表单的action处理页面为process.jsp,在该页面中进行用户名和密码匹配判断,假设用户名为gdpu和密码为123,即认为登录成功,运用session将成功登录信息保存;
- 当访问index1.jsp或者index2,jsp时,如果用户已登录成功,则在界面显示“登录用户:gdpu”,否则显示“游客,请登录。”。
Login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta charset=UTF-8>
<title>GDPU官网登录页面</title>
</head>
<%
String usernamex = (String)session.getAttribute("login");
if(usernamex == null){
}else{
response.sendRedirect("./index1.jsp");
}
%>
<body>
<form action ="process.jsp" method ="post">
GDPU账号:<input type = "text" style = "width220px;" name = "username"/><br><br>
账号密码 :<input type = "text" style = "width220px;" name = "password"/><br><br>
<input type = "submit" style = "width:220px;" value = "登录"/>
</form>
</body>
</html>
Process.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta charset=UTF-8>
<title>GDPU官网登录页面后台</title>
</head>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
if("gdpu".equals(username)&&"123".equals(password)){
out.print("登录成功");
session.setAttribute("login",username);
}else{
session.setAttribute("login",null);
}
%>
<body>
</body>
</html>
Index1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta charset=UTF-8>
<title>访问页面</title>
</head>
<body>
<%
String username = (String)session.getAttribute("login");
if(username == null){
out.print("游客,请登录");
}else{
out.print("当前登录用户:" + username);
}
%>
</body>
</html>
Index2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta charset=UTF-8>
<title>访问页面</title>
</head>
<body>
<%
String username = (String)session.getAttribute("login");
if(username == null){
out.print("游客,请登录");
}else{
out.print("当前登录用户:" + username);
}
%>
</body>
</html>
【2】创建两个JSP网页,文件名为shopping.jsp和cart.jsp,页面需要以下功能:
- 在shopping.jsp,通过浏览器访问该页面时,通过带参的方法,输入参数名为fruit及其对应的值(值可以自定,例如Banana)。在页面中,实现将该参数保存到session中。另外,在shopping.jsp页面上,有一个链接,可以通过这个链接访问cart.jsp;
- 在cart.jsp,将保存在shopping.jsp中的fruit读取出来并显示。
Shopping.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <% String fruit = request.getParameter("fruit"); session.setAttribute("selectedFruit", fruit); %> <!DOCTYPE html> <html> <head> <title>扑扑超市</title> </head> <body> <h1>扑扑超市</h1> <a href="cart.jsp?fruit=<%= fruit %>">前往购物车</a> </body> </html>
Cart.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <% String selectedFruit = (String) session.getAttribute("selectedFruit"); %> <!DOCTYPE html> <html> <head> <title>扑扑超市结算网页</title> </head> <body> <h1>购物车</h1> <% if (selectedFruit != null) { %> <p>购买物品: <%= selectedFruit %></p> <% } else { %> <p>购物车空空如也</p> <% } %> </body> </html>