Servlet实现用户登录
一、登录
1、登录过程分析:

通过表单收集用户的数据,Servlet通过request对象获得用户提交的数据,服务器还需要从数据库中通过sql语句查询有没有表单提交的数据中的用户。有则登录成功,否则,登录失败。
2、工程结构:

3、主要代码分析:
<body bgcolor="aqua">
<center>
<h3>登录</h3>
<form action="/Login_war_exploded/log" method="get">
用户名:<input type="text" name="username" size=""><br>
密 码 :<input type="password" name="password" size="" ><br><br>
<input type="reset" value="取消">
<input type="submit" value="登录">
</form>
</center>
</body>

(1)通过定义表单为Servlet提供数据,其中的name属性的值与Servlet的request.getParameter的参数相同,实现了html页面与服务器的联系。
(2)action属性实现了页面的跳转,即提交数据之后去执行Servlet,action的属性值为工程名与Servlet名的组合。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection con=null;
login log= null;
String account=request.getParameter("username");//获得表单数据
String password=request.getParameter("password");
try {
con=C3p0Utils.getConnection();
QueryRunner qr = new QueryRunner();
String sql = "Select * from login where account=? and password=?";
Object[] select = {account,password};
log = qr.query(con, sql, new BeanHandler<login>((login.class)), select);
if(log!=null){
response.getWriter().write("nihao"+account);
}
else{
response.getWriter().write("wrong");
}
}
catch (SQLException e) {
throw new RuntimeException(e);
}
}
Servlet在html页面与数据库之间,在完成与html交流的同时,还要与数据库中的数据打交道。
二、注册
1、Servlet实现注册的思路:

2、工程结构

3、功能实现:
(1)html实现对数据的收集:
<body bgcolor="aqua">
<center>
<h3>注册</h3>
<form action="/Register_servlet_war_exploded/register" method="post">
用户名:<input type="text" name="account" size="12"><br><br>
密码:<input type="password" name="password" size="12">
<input type="submit" value="注册">
<input type="reset" value="取消">
</form>
</center>
</body>
(2)Servlet:获取表单提交的数据,并将他们封装到Map集合中(可以减少代码量)
public class ServletRegister extends javax.servlet.http.HttpServlet {
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
request.setCharacterEncoding("UTF-8");
Map<String, String[]> properties=request.getParameterMap();//将表单中的数据封装到Map中
Login log= new Login();
try {
BeanUtils.populate(log, properties);
} catch (IllegalAccessException|InvocationTargetException e) {
e.printStackTrace();
}
try {
regist(log);
} catch (SQLException e) {
e.printStackTrace();
}
response.sendRedirect(request.getContextPath()+"/log.html");//重定向
}
public void regist(Login log) throws SQLException{//数据库
Connection con=null;
try {
con = C3p0Utils.getConnection();
QueryRunner qr = new QueryRunner();
String sql = "insert into Login values(?,?)";
Object[] insert = {log.getAccount(), log.getPassword()};
qr.update(con, sql, insert);
}
catch (SQLException e){
throw new RuntimeException(e);
}
}
Servlet实现用户登录的更多相关文章
- Servlet——简单用户登录实例+http协议解析
编写项目.用户登录系统1.0版本号 登录界面Servlet: package com.gavin.view; import java.io.IOException; import java.io.Pr ...
- Servlet+jsp用户登录加上验证码
最近公司有个项目被客户拿去进行漏洞扫描,发现用户登录太简单,容易被暴力破解.当然发现的问题很多,什么反射型XSS,存储型XSS,敏感信息泄露等等.但是我们今天不讲这么多,就说说如何修复暴力破解的问题. ...
- 用servlet实现用户登录案例
以下实现登录窗口 Login.jsp <!--Login.jsp--> <%@ page language="java" import="java.ut ...
- Java EE之servlet实现用户登录
1.在连接数据库的JAVA类中添加查询功能: 在这之前有一个连接数据库的方法: Connection conn=null; PreparedStatement stat=null; ...
- jsp使用servlet实现用户登录 及动态验证码
在进行表单设计中,验证码的增加恰恰可以实现是否为“人为”操作,增加验证码可以防止网站数据库信息的冗杂等... 现在,我将讲述通过servlet实现验证码: 验证码作为一个图片,在页面中为“画”出来的, ...
- Javaweb 使用Servlet技术改写用户登录 使用Filter技术解决中文乱码
先把实验3的jsp页面复制过来: WebContent->WEB-INF->lib下面的jar包8.0版本也要记得复制: Java Resources->src下的 cn.edu.h ...
- javaweb学习总结(二十二)——基于Servlet+JSP+JavaBean开发模式的用户登录注册
一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...
- jsp&servlet初体验——用户登录功能实现
数据库准备-创建db_login数据库 t_user表 1.创建web工程 2.创建用户model user.java package com.gxy.model; public class U ...
- 基于Servlet的MVC模式用户登录实例
关于MVC模式的简单解释 M Model,模型层,例如登录实例中,用于处理登录操作的类: V View,视图层,用于展示以及与用户交互.使用html.js.css.jsp.jQuery等前端技术实现: ...
随机推荐
- Unable to load template file 'rj\ThinkPHP/Tpl/dispatch_jump.tpl'----thinkphp3.2.3
Unable to load template file 'rj\ThinkPHP/Tpl/dispatch_jump.tpl'----thinkphp3.2.3 1.报错原因:将thinkphp默认 ...
- IntelliJ IDEA 从入门到上瘾教程,2019图文版!
前言:IntelliJ IDEA 如果说IntelliJ IDEA是一款现代化智能开发工具的话,Eclipse则称得上是石器时代的东西了. 其实笔者也是一枚从Eclipse转IDEA的探索者,随着近期 ...
- 线程学习oneday
进程:执行中的程序叫做进程(Process),是一个动态的概念. 线程:一个进程可以产生多个线程.同多个进程可以共享操作系统的某些资源一样,同一进程的多个线程也可以共享此进程的某些资源(比如:代码.数 ...
- 浅析runtime包中的三个方法Gosched、Goexit、GOMAXPROCS
Gosched 暂停当前goroutine,使其他goroutine先行运算.只是暂停,不是挂起,当时间片轮转到该协程时,Gosched()后面的操作将自动恢复 未使用Gosched的代码 packa ...
- nuget cli常用命令简介
起因:使用nuget,但是部分同事用的mac,不能用vs的包管理器查看私有nuget库里面的包,所以,就总结了几个常用的 nuget cli 命令,方便全平台使用nuget 废话不多,直入主题 准备: ...
- ubuntu安装后的基本配置及常用软件的安装
文章作者:foochane 原文链接:https://foochane.cn/article/2019061501.html 内容简介 当前Ubuntu版本:ubuntu 18.04,具体操作如下: ...
- 设计模式(C#)——12责任链模式
推荐阅读: 我的CSDN 我的博客园 QQ群:704621321 前言 在开发游戏过程中,当玩家合成一种道具的时候,对于不痛的道具,需要的碎片个数,类型是不同的.用传统的写法,就是 ...
- C#数据结构_排序
/** * 冒泡排序 * * @param array * @return */ public static int[] bubbleSort(int[] array) { if (array.len ...
- Linux 下安装 mysql8
1.下载mysql wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.13-linux-glibc2.12-x86_64.tar 2. ...
- ScrollView中页面显示自动滑到最后问题的解决
转载:https://blog.csdn.net/a644904088/article/details/80241176 原因:ScrollView中包含其余控件,但控件显示不全,此时会存在焦点问题, ...