立即学习:https://edu.csdn.net/course/play/1060/15792?utm_source=blogtoedu
-------------------------------------------------------------
实例:
在web.xml文件中设置两个WEB应用的初始化参数user,password
定义一个login,html,俩面定义两个请求字段:user,password
在创建一个LoginServelt,在其中获取请求的user,password
比对其web.xml文件中定义的请求参数是否一致
若一致,响应HELLO,若不一致,响应Sorry
-------------------------------------------------------------
步骤:
1、建立web.xml,编写配置信息
2、创建login.xml,编写表单(包含username,password,submit),method写为post
3、创建LoginServlet.java
1.获取请求参数:username,password
2.获取当前web应用的初始化参数user,password
需要使用ServletContext方法
ServletContext servletContext =servletConfig.getServletContext();
String initUser =servletContext.getInitParameter("User");
String initpassword =servletContext.getInitParameter("password");
3.比对
4.打印响应字符串
-------------------------------------------------------------
代码:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
<!-- 配置当前Web应用的初始化参数 -->
<context-param>
<param-name>User</param-name>
<param-value>KACKK</param-value>
</context-param>
<context-param>
<param-name>Password</param-name>
<param-value>123qwe</param-value>
</context-param>
<!-- 配置Servlet -->
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>com.ckk.javaweb.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
</web-app>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="loginServlet" method="post">
User:<input type="text" name="Username"/>
password:<input type="password" name="password"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>