一.任务1
1.源代码
verify.jsp 代码,用来写登录页面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>One piece</title>
<style>
div{
background-color: lightblue;
height: 150px;
width: 300px;
border-radius: 25px;
margin-top: 20px;
padding-top: 50px;
}
#submit{
margin-left: 100px;
width: 80px;
}
</style>
</head>
<body>
<div>
<form action = "result.jsp" method = "post">
<label for="username" name="username">用户名:</label>
<input type="text" id="username" name="username"></input> <br>
<br>
<label for="password" name="password">密码: </label>
<input type="password" id="password" name="password"></input> <br>
<br>
<input type = "submit" value="提交" id="submit"></input>
</form>
</div>
</body>
</html>
result.jsp代码,用来获取数据,并且判断密码是否正确
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>One piece</title>
</head>
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username != null && password.equals("12345678"))
{
out.println("您已经成功登录");
}
else{
out.println("用户名或密码错误,请重新输入");
}
%>
</body>
</html>
2.效果:
登录页面:
密码错误:
密码正确:
二.任务2
1.request与session
session的用法是先用request.getParameter()
拿到数据,再把拿到的数据放到session里面。
举个例子,现在我们在index.jsp里面输入了一条信息,name是username, 表单将数据提交到session.jsp
那么如果要在session.jsp中获取数据,则为以下代码:
String username = request.getParameter("username");
session.setAttribute("username", username);
通过以上代码,username对于的信息就被存到了session中, 而session可以作用于多个页面。
如果session.jsp的数据被提交到了result.jsp里面, 并且想要在result.jsp中用到name=为username的信息,那么代码为:
String name = (String)session.getAttribute("username");
其实很好记,第一遍放到session里面时, 需要setAttrbute(); 以后要调用session里面的数据时, 直接getAttribute()即可。
怎么理解request和session?request就是你私发给你好基友的信息,只有他能看到,也就是被一个页面用。
session意味着你把这条信息发群里去了,好多人都看得到,因此能被好多页面用。
2.代码和效果
index.jsp代码:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>One piece1</title>
<style>
div{
background-color: lightblue;
height: 150px;
width: 300px;
border-radius: 25px;
margin-top: 20px;
padding-top: 50px;
}
#submit{
margin-left: 100px;
width: 80px;
}
</style>
</head>
<body>
<div>
<form action = "session.jsp" method = "post">
<label for="username" name="username">用户名:</label>
<input type="text" id="username" name="username"></input> <br>
<br>
<label for="password" name="password">密码: </label>
<input type="password" id="password" name="password"></input> <br>
<br>
<input type = "submit" value="提交" id="submit"></input>
</form>
</div>
</body>
</html>
session.jsp代码:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>One piece1</title>
<style>
div{
background-color: lightblue;
height: 150px;
width: 300px;
border-radius: 25px;
margin-top: 20px;
padding-top: 50px;
}
#submit{
margin-left: 100px;
width: 80px;
}
</style>
</head>
<body>
<div>
<form action = "session.jsp" method = "post">
<label for="username" name="username">用户名:</label>
<input type="text" id="username" name="username"></input> <br>
<br>
<label for="password" name="password">密码: </label>
<input type="password" id="password" name="password"></input> <br>
<br>
<input type = "submit" value="提交" id="submit"></input>
</form>
</div>
</body>
</html>
result.jsp代码
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>one piece3</title>
</head>
<body>
<%
String username = (String)session.getAttribute("username");
String password = (String)session.getAttribute("password");
String hobby = request.getParameter("hobby");
session.setAttribute("hobby", hobby);
%>
<%
if(username.equals("nor") && password.equals("12345678"))
{
out.println("欢迎您,nor"); %>
<br><%
if(hobby != null){
out.println("唱跳 rap 篮球 -------> 是不错的爱好");
}
else{
out.println("爱好不够好,需要改进");
}
}
else{
out.println("非法访问,你干嘛");
}
%>
</body>
</html>
效果如下:
如果登录成功: