首先是新建一个servlet,servlet中有dopost和doget方法

一般的表格提交都是用post方法,故在dopost里面写入逻辑代码

下面是其逻辑代码Check.java

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取表单提供的数据
String name=request.getParameter("username");
String pwd=request.getParameter("password");
//数据库相关参数
String url="jdbc:mysql://localhost:3306/office?useSSL=false&serverTimezone=Hongkong&characterEncoding=utf-8&autoReconnect=true";
String username="root";
String password="123456";
//执行的sql语句
String sql="select username,password from user where username=? and password=?";
//jdbc的三个接口
Connection con=null;
//预处理
PreparedStatement stmt=null;
ResultSet res=null;
try {
//加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//建立连接
con=DriverManager.getConnection(url,username,password);
//预编译
stmt=con.prepareStatement(sql);
//向sql中传入参数
stmt.setString(1,name );
stmt.setString(2, pwd);
//执行查询
res=stmt.executeQuery();
//遍历结果集
while(res.next()) {
//在结果集中查找列数据
String username1=res.getString("username");
String password1=res.getString("password");
//逻辑判断
if(name.equals(username1)&&pwd.equals(password1)) {
//重定向
RequestDispatcher rd=request.getRequestDispatcher("success.jsp");
rd.forward(request, response);
return;
}else {
RequestDispatcher rd1=request.getRequestDispatcher("faile.jsp");
System.out.println(username1);
System.out.println(password1);
rd1.forward(request, response);
return;
}
}
res.close();
stmt.close();
con.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} }

  其次,我们是通过form的action进行服务端校验的,所以需要配置servlet,让程序找到我们的servlet

<?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">
<display-name>servlet</display-name>
<!--配置Servlet-->
<servlet>
<!-- Servlet应用名 -->
<servlet-name>Check</servlet-name>
<!-- 对应的servlet的类 -->
<servlet-class>servlet.Check</servlet-class>
</servlet>
<!-- 地址映射 -->
<servlet-mapping>
<!-- 设置的servlet应用名 -->
<servlet-name>Check</servlet-name>
<!-- 地址名 -->
<url-pattern>/Check</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

  1.输入地址:http://localhost:8080/login,提交表格后,来到http://localhost:8080/Check,通过它找到映射文件内部的文件名Check

2.通过Check找到对应的<servlet-name>Check</servlet-name>

3.然后定位到这个servlet文件:servlet.Check.java

最新文章

  1. mac 10.11.6 自带apache配置记录
  2. jQuery静态方法inArray,grep,merge,makeArray方法使用和源码分析
  3. android之fragment的使用
  4. yii2嵌入微信公众号支付
  5. centos7时间同步和时区设置
  6. WPF DataGrid Control
  7. 【BZOJ1001】【BeiJing2006】狼抓兔子 最大流
  8. SQL注入小结
  9. tomcat 内存溢出
  10. ssh 或者 scp 无需输入密码的解决办法
  11. Linux下安装zookeeper集群
  12. SCALA STEP BY STEP
  13. 子窗口url调整导致父窗口刷新
  14. TypeScript教程1
  15. Elasticsearch批处理操作——bulk API
  16. A Graph Partitioning Game Theoretical Approach for the VNF Service Chaining Problem
  17. 夏令时(DST)测试
  18. SQL Server服务器CPU爆高解决
  19. 删除kafka topic
  20. Bdfproxy

热门文章

  1. ubuntu install redis
  2. More Effective C++笔记(一)(精心整理)
  3. 测试开发【提测平台】分享14-Vue图标Icon几种用法并利用其一优化菜单
  4. Go语言核心36讲(Go语言实战与应用五)--学习笔记
  5. NodeJS连接MongoDB和mongoose
  6. [bzoj2668]交换棋子
  7. Sentry 监控 - Snuba 数据中台架构(编写和测试 Snuba 查询)
  8. 多线程06.thread守护线程
  9. [NOI 2014]起床困难综合症[二进制]
  10. 洛谷 P4135 作诗(分块)