今日学习成果

利用servlet实现简单登录

在新创建的web project中,webroot下创建login.jsp文件
登录界面名称为“登入界面”
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>登入界面</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
预览 <br>
<form action="login">
username:<input type="text" name="username"><br>
password:<input type="password" name="pwd"><br>
<input type="submit">
</form>
</body>
</html>


在web.xml中配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<welcome-file-list>
<welcome-file>/login.jsp</welcome-file>
</welcome-file-list>
</web-app>

报错:无效的源发行版:11
原因:
项目的jdk(Project SDK)版本和项目的语言级别(Project language level )不同。

解决方式:


打开idea的Project Structure 菜单,修改对应的 Project SDK 、Project language level 到相对于的级别即可。如 Project language level 改为8。修改之后,Modules模块的Language level也变成8.重新允许Tomcat,成功。

在登录中增加校验 

<script >
function loginverify(){
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
if (username == '') {
alert('用户名不能为空');
return;
}
if (password =='' ) {
alert('密码不能为空');
return;
}
}
</script>
利用loginverify关联到登录按钮事件

posted @ 2020-11-18 16:22  计算机语言学习日志  阅读(63)  评论(0)    收藏  举报