index.jsp(登录界面)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
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">--><style type="text/css"></style></head>  <body><div><form action="loginServlet" method="post"><table align="center" ><tr><td>用户名:</td><td><input type="text" name="username"> <font color="red">${requestScope.namemsg}${requestScope.nameError}</font></td></tr><tr><td>密码:</td><td><input type="password" name="password"> <font color="red">${requestScope.pwdError}${requestScope.pwdmsg}</font></td></tr><tr><td></td><td><input type="submit" value="登录"><input type="reset" value="取消"></td></tr></table></form></div></body>
</html>

loginServlet(登录验证)

package com.dwx.servlet;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dwx.bean.user;
import com.dwx.bean.userDao;/*** 用户登录验证*/
@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {private static final long serialVersionUID = 1L;/*** @see HttpServlet#HttpServlet()*/public loginServlet() {super();// TODO Auto-generated constructor stub}/*** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String username=request.getParameter("username");String password=request.getParameter("password");//判断用户名、密码是否为空if(username==""||username.length()==0){request.setAttribute("namemsg","用户名为空!");request.getRequestDispatcher("index.jsp").forward(request, response);}if(password==""||password.length()==0){request.setAttribute("pwdmsg","密码为空!");request.getRequestDispatcher("index.jsp").forward(request, response);}//判断用户名、密码是否正确ResultSet rst=userDao.selectUser(username, password);try {if(rst.next()){if(password.equals(rst.getString("password"))){request.setAttribute("username",username);request.getRequestDispatcher("items.jsp").forward(request, response);}else{request.setAttribute("pwdError","密码不正确!");request.getRequestDispatcher("index.jsp").forward(request, response);}}else{request.setAttribute("nameError","用户名不存在!");request.getRequestDispatcher("index.jsp").forward(request, response);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

userDao.java(查询用户)

package com.dwx.bean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.dwx.util.dao;
public class userDao {public static ResultSet selectUser(String username,String password){Connection conn=dao.getConnection();String sql="select * from user where username=?";ResultSet rst=null;try{PreparedStatement prst=conn.prepareStatement(sql);prst.setString(1, username);rst=prst.executeQuery();}catch(Exception ex){ex.printStackTrace();}return rst;}
}

dao.java(建立数据库连接)

package com.dwx.util;
import java.sql.Connection;
import java.sql.DriverManager;
public class dao {public static Connection getConnection(){Connection conn=null;try{Class.forName("com.mysql.jdbc.Driver");      //加载驱动String url="jdbc:mysql://localhost:3306/db_item";String username="root";String password="root";//创建数据库连接conn=DriverManager.getConnection(url,username,password);}catch(Exception ex){ex.printStackTrace();}return conn;}
}

效果演示:

1、用户名为空

2、密码为空

3、用户名不存在

4、密码不正确

5、登录成功

servlet登录验证并返回错误信息相关推荐

  1. ios请求php接口失败,laravel,php_iOS调用Laravel接口返回错误信息,laravel,php,ios - phpStudy...

    iOS调用Laravel接口返回错误信息 iOS端代码如下: AFHTTPRequestOperationManager *manage = [[AFHTTPRequestOperationManag ...

  2. 配置错误:未能使用提供程序“RsaProtectedConfigurationProvider”进行解密。提供程序返回错误信息为: 打不开 RSA 密钥容器。...

    http://www.cnblogs.com/jiaruistone/articles/1441634.html 在asp.net2.0中新增了对web.config中的部分数据进行加密的功能,可以使 ...

  3. 拨号不成功,根据返回错误信息确定故障

    拨号不成功,根据返回错误信息确定故障: 1.ror 602 The port is already open 问题:拨号网络网络由于设备安装错误或正在使用,不能进行连接 原因:RasPPPoE没有完全 ...

  4. null?对象?异常?到底应该如何返回错误信息

    null?对象?异常?到底应该如何返回错误信息 参考文章: (1)null?对象?异常?到底应该如何返回错误信息 (2)https://www.cnblogs.com/dsj2016/p/748208 ...

  5. yapi-404-接口字段验证不通过, 错误信息:shema 验证请求参数 schema is invalid: data.properties[‘otherParams‘].required sho

    报错原因: 接口字段验证不通过, 错误信息:shema 验证请求参数 schema is invalid: data.properties['otherParams'].required should ...

  6. php如果登录失败弹出失败,登录失败或出现错误信息(无跳转、提示失败、出现错误信息)的解决方法...

    关于在QQ开放平台申请QQ帐号登陆,审核结果提示"登录失败或出现错误信息(无跳转.提示失败.出现错误信息)"的解决方法, 需要解决QQ登录平台接口 OAuth2.0 协议需要你的服 ...

  7. 发生了Post错误:错误代码40005,微信返回错误信息:invalid file type

    给客户部署 PxxCms, 使用群发功能发送图文的的时候提示: 发生了Post错误:错误代码40005,微信返回错误信息:invalid file type, 没学过php伤不起 ... Google ...

  8. 统一登录验证统一返回格式统一异常处理的实现

    统一登录验证&统一返回格式&统一异常处理的实现 一.用户登录权限效验 1.1 最初的用户登录验证 1.2 Spring AOP 用户统一登录验证的问题 1.3 Spring 拦截器 1 ...

  9. Django rest framework 返回错误信息 __str__ returned non-string (type NoneType)

    Django rest framework 返回错误信息 str returned non-string (type NoneType) 错误信息如下: 此时点击右上角的admin,把帐号退掉,然后在 ...

最新文章

  1. Orchard之模版开发
  2. 网易有道2017内推编程题
  3. SpringCloud版本说明
  4. sass和compass基础用法
  5. java if 定义变量_java – 如何分配在if else语句中定义的变量
  6. 视频直播技术(二):延迟优化
  7. python的matplotlib的函数_在Python中使用matplotlib绘制条件函数
  8. Jquery Ztree异步加载树
  9. Android源码中添加 修改应用
  10. linux驱动目录在哪里,详细讲解Linux驱动程序
  11. python时间序列预测不连续怎么办_用Python处理非平稳时间序列攻略
  12. 6步解决win7局域网内传输慢的问题
  13. 服务器字体显示繁体字怎么解决,win7繁体字乱码怎么办?win7繁体字显示乱码解决方法...
  14. Qt进程-QProcess使用总结
  15. p5.js炫酷背景动态js特效代码
  16. 华为防火墙L2TP/L2TP over IPSec
  17. 双软企业的税收优惠政策怎么样?
  18. v-chart柱状图点击改变颜色
  19. 【中文技术文档的写作规范_P03】如何书写标点符号和控制文档体系
  20. 【tk跨境电商】tk跨境电商合法吗?现在好做吗?

热门文章

  1. android 播放 优酷视频
  2. 《前端框架Vue.js》
  3. 7. 伪随机数的生成
  4. 2023最新SSM计算机毕业设计选题大全(附源码+LW)之java自助旅游平台v294n
  5. 《深入理解计算机系统》:Cache Lab
  6. 赛效:如何在线给图片加水印
  7. wordpress如何制作一个跟随滑动的文章目录?Content Index文章目录插件修改篇
  8. HighCharts生成柏拉图
  9. 技侦十所 技侦科技局
  10. Bringing up interface eth0: Device eth0 does not seem to be present, delaying initialization.