第五课(servlet)-通过servlet对登陆页面的用户身份进行鉴权

因为还没有学习到JDBC(即服务器收到的数据连接后端数据库)的部分,所以该处的鉴权也是一种静态的、指定对象的鉴权,即提前在servlet中写好一个账户和密码,当用户提交表单时,通过直接对比是否与提前存好的数据相同,则可以对其身份进行初级的鉴权

涉及页面:

Login:登陆页面

Helloworld:会员信息显示/通过会员服务的页面(该文件为写hello world的servlet改过来的,不想创太多文件,而改名字会很麻烦,就直接继承了前面的名字)

Login源码:

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
		 <style type="text/css">
		div{
				text-align:center
				}
		input {
				 width: 100%;
				}
		input[type=text],[type=password] {
				  width: 100%;
				  padding: 12px 20px;
				  margin: 8px 0;
				  box-sizing: border-box;
				  border: none;
				  background-color: #3CBC8D;
				  color: white;
				}
		input[type=submit] {
				  width: 100%;
				  background-color: #4CAF50;
				  color: white;
				  padding: 14px 20px;
				  margin: 8px 0;
				  border: none;
				  border-radius: 4px;
				  cursor: pointer;
				}

		input[type=submit]:hover {
				  background-color: #45a049;
				}
		</style>
<div>
 <form action="<%=path %>/servlet/helloworld" name="login" method="get" ">
  		NAME: <input type="text" name="Name" maxlength="20" ><br><br>
  		PASS: <input type="password" name="Password"  maxlength="20"><br><br>
  		<input type="submit" name="Submit" value="login"><br>
  </form>
  </div>

PS:上面那段css是直接在菜鸟上扒的,用于复习css的知识,需要更多样式的同学请自行百度菜鸟教程

Helloworld源码:

package p.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/helloworld")
public class helloworld extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		//获取网页画笔
		String username = request.getParameter("Name");
		//获取login.jsp页面表单数据
		String password = request.getParameter("Password");
		if(username==null||username.equals("")||password==null||password.equals("")) {
			//判断用户名/密码是否为空
			out.print("账号或密码不能为空");
		}
		else if(username.equals("JIA")){	
			//判断用户名是否为指定的用户名,若不是直接执行else语句
			if(password.equals("123456789")) {
			//用户名合法之后判断密码是否合法
			//合法则显示下方的会员页面
		out.print("登陆成功!欢迎你");
		out.print("<br>");
		out.print("NAME:"+username);
		out.print("<br>");
		out.print("PASS:"+password);
			}else {
				out.print("请输入正确的密码");
			}
		}else {
			out.print("请输入正确的账号");
		}
	}
}

知识点:

       String username = request.getParameter("Name");

       //获取login.jsp页面表单数据

使用request.getParameter()方法来获取表单传过来的数据,参数为该参数对应的login页面中的input标签的name值

 

思考:为什么QQ上账号与密码有一个输错了,它提醒的是账号或密码错误,而不是直接告诉你账号错误还是密码错误?

这样做可能安全性更好,即非法用户不能确认哪个错了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值