学习笔记(02):JavaWeb基础核心技术-11. 佟刚_JavaWEB_GenericServlet

立即学习: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>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值