第三次作业

一.任务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">密码: &nbsp;&nbsp;&nbsp;</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.效果:

登录页面:

image

密码错误:

image

密码正确:

image

二.任务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">密码: &nbsp;&nbsp;&nbsp;</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">密码: &nbsp;&nbsp;&nbsp;</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>


效果如下:

image

如果登录成功:

image
image

如果登录失败:

image

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值