jsp登录(mysql数据库)
来源:互联网 发布:windows域 编辑:程序博客网 时间:2024/06/05 23:42
从网页上获取的用户信息连接数据库进行校验,首先要有网页进行提交表单,通过servlet来读取其中的信息,再拿来与数据库中的数据进行比较。
我们来看一下login.html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body> <form action="LoginServlet3" method="post"> user:<input type="text" name="username"/> password:<input type="password" name="password"/> <input type="submit" value="submit"/> </form></body></html>我们再来看一下servlet,我直接将数据库的连接写在了servlet立里面这样写其实是不规范的。
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 java.io.IOException;import java.io.PrintWriter;import java.sql.*;/** * Created by yuehailin on 2017/7/15. */@WebServlet(name = "LoginServlet3")public class LoginServlet3 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username =request.getParameter("username"); String password =request.getParameter("password"); PrintWriter out =response.getWriter(); Connection connection=null; PreparedStatement statement; statement = null; ResultSet resultSet =null; try { Class.forName("com.mysql.jdbc.Driver"); System.out.print("ok"); String sql ="select count(id) from user where username=?"+"AND password=?"; connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs","root","123"); statement =connection.prepareStatement(sql); statement.setString(1,username); statement.setString(2,password); resultSet =statement.executeQuery(); if(resultSet.next()){ int count=resultSet.getInt(1); if(count>0) { out.println("hello"+username); } else{ out.println("sorry"+username); } } } catch (Exception e) { e.printStackTrace(); } finally { try { if(resultSet!=null) { resultSet.close(); } }catch (SQLException e){ e.printStackTrace(); } try { if(statement!=null) { statement.close(); } }catch (SQLException e){ e.printStackTrace(); } try { if(connection!=null) { connection.close(); } }catch (SQLException e){ e.printStackTrace(); } } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }}在写javaweb项目的时候大家要注意在xml中进行配置,或者通过注入的方式,但是注入的方式仅限于3.0
之后。
希望对大家的学习有所帮助。
阅读全文
0 0
- jsp登录(mysql数据库)
- jsp+servlet+mysql 简单登录操数据库
- MyEclipse下jsp连接mysql数据库实现用户登录
- jsp注册(mysql数据库)
- 如何登录MySQL数据库
- mysql 数据库远程登录
- 登录,退出mysql数据库
- JSP连接数据库实现注册登录(附带上传头像)
- jsp实现登录注册(与数据库对接)
- jsp 简易用户自动登录(无数据库)
- JSP登录界面(连接并验证数据库)
- JSP连接mysql数据库攻略(转)
- jsp之 javabean (mysql数据库)
- JSP连接MySQL数据库例子(查询)
- 初学JSP与JDBC(mysql数据库)
- JavaWeb(jsp)连接mysql数据库
- JSP(MyEclipse)连接MySQL数据库
- JSP连接Mysql数据库
- 《自控力》 【美】凯利·麦格尼格尔 第一章读书笔记
- oracle的性能报告获取
- java日志组件介绍(common-logging,log4j,slf4j,logback )
- 关于RAC中SCN原理和机制的探索
- 结构体的大小
- jsp登录(mysql数据库)
- Scrapy-爬虫多开技能
- HEVC官方软件HM源代码简单分析-解码器TAppDecoder
- [NOIp复习计划]:二分答案
- windos 下MySQL 安装和启动步骤
- vue动态载入变量键值名,实现数据双向绑定
- 重建二叉树
- 前端代码检测利器——eslint for sublime安装指南
- VC++6.0插件系列(绝对经典)