servlet登录练习

xiaoxiao2021-02-28  26

title: servlet登录页面提交数据,从数据库核对练习 date: 2015-12-25 16:24:07 categories: servlet tags: servlet


xl_echo编辑整理,欢迎转载,转载请声明文章来源。更多案例、资料请联系QQ:1280023003

servlet练习,登录

通过servlet获取登录页面的用户名,用户密码实现登录,如果数据库中有该用户名和用户密码,则返回登录成功,如果没有则返回登录失败

目录结构: - java Resources - src - DBUtils

BeanUtils.java C3P0_Utils.java 链接数据库 - Service Control_Service.java 调用Dao层,对数据库进行查询 - Servlet Servlet_Login.java 接收表单提交的参数 封装到BeanUtils中 调用业务层Service业务层处理数据 根据处理的结果显示信息(页面跳转) - Servlet_Dao Servlet_Login_Dao.java 从数据库中进行查询 - WebContent - Login_Html index.html:表单页面(登录页面)

- WEB-INF > web.xml:配置文件

BeanUtils

package DBUtils; import java.io.Serializable; public class BeanUtils implements Serializable{ private static final long serialVersionUID = -4057850299304233093L; private String id; private String uname; private String gender; private String score; private String address; private String password; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getScore() { return score; } public void setScore(String score) { this.score = score; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "BeanUtils [id=" + id + ", uname=" + uname + ", gender=" + gender + ", score=" + score + ", address=" + address + ", password=" + password + "]"; } }

C3P0_Utils

package DBUtils; import java.sql.Connection; import java.sql.SQLException; import org.junit.Test; import com.mchange.v2.c3p0.ComboPooledDataSource; public class C3P0_Utils { @Test public Connection getConnection(){ Connection conn = null; ComboPooledDataSource DATA_SORUCE = new ComboPooledDataSource(); try { conn = DATA_SORUCE.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return conn; } }

Control_Service

package Service; import java.sql.SQLException; import DBUtils.BeanUtils; import Servlet_Dao.Servlet_Login_Dao; public class Control_Service { public BeanUtils Login(BeanUtils bu) throws SQLException { Servlet_Login_Dao s = new Servlet_Login_Dao(); BeanUtils b = s.Login_Dao(bu); return b; } }

Servlet_Login

package Servlet; import java.io.IOException; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import DBUtils.BeanUtils; import Service.Control_Service; /** * Servlet implementation class Servlet_Login */ public class Servlet_Login extends HttpServlet { private static final long serialVersionUID = 1L; public Servlet_Login() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取页面请求信息 String name = request.getParameter("name"); String password = request.getParameter("password"); //将接收到的信息封装进入一个对象中 BeanUtils bu = new BeanUtils(); bu.setUname(name); bu.setPassword(password); //调用处理层的方法进行验证,如果数据库中有值就返回一个对象 Control_Service cs = new Control_Service(); BeanUtils b = null; try { b = cs.Login(bu); } catch (SQLException e) { e.printStackTrace(); } //解决乱码 response.getCharacterEncoding(); //判断从处理层返回的对象是否为空如果为空直接在页面上显示登录失败,不为空显示登录成功 if(b == null){ response.getWriter().println("Login-load-fail"); }else{ response.getWriter().println("Login-success"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

Servlet_Login_Dao

package Servlet_Dao; import java.sql.Connection; import java.sql.SQLException; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import DBUtils.BeanUtils; import DBUtils.C3P0_Utils; public class Servlet_Login_Dao { public BeanUtils Login_Dao(BeanUtils bu) throws SQLException { //获取数据库链接 Connection conn = null; C3P0_Utils c = new C3P0_Utils(); conn = c.getConnection(); System.out.println(conn); //获取数据库操作对象 QueryRunner qr = new QueryRunner(); //去数据库查询数据 String sql = "select * from newtable where uname = ? and password = ?"; System.out.println(bu); String password = bu.getPassword(); int a = Integer.parseInt(password); System.out.println(a); BeanUtils b = qr.query(conn, sql, new BeanHandler<BeanUtils>(BeanUtils.class),bu.getUname(),a); conn.close(); System.out.println(b); return b; } }

c3p0-config

<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/myxlsorry</property> <property name="user">root</property> <property name="password">password</property> </default-config> </c3p0-config>

index.html

<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>注册页面</title> <script> //判断用户名和密码是否为空,如果是则提示用户名和密码怎么组成 function tips(id,content){ var temp = document.getElementById(id).value; if(temp == ""){ document.getElementById(id + "Span").innerHTML = "<font color='purple'>"+content+"</font>"; }else{ document.getElementById(id + "Span").innerHTML = "<font color='purple'></font>"; } } //点击提交之后如果为空显示不能为空 function buttonSubmit(){ //点击提交的时候如果用户名值为空,那么不跳转,并提示 var username = document.getElementById("username").value; if(username == ""){ document.getElementById("usernameSpan").innerHTML = "<font color='purple'>用户名不能为空</font>"; return false; } //点击提交的时候密码如果值为空,那么不跳转,并提示 var password = document.getElementById("password").value; if(password == ""){ document.getElementById("passwordSpan").innerHTML = "<font color='purple'>密码不能为空</font>"; return false; } //判断两次密码是否一致 var password = document.getElementById("password").value; var rePassword = document.getElementById("rePassword").value; if(password != rePassword){ document.getElementById("rePasswordSpan").innerHTML = "<font color='purple'>两个密码不一致</font>"; return false; } } //定义数组:二维数组 var arrs = new Array(4); arrs[0] = new Array("北京","广州","深圳","上海","湛江"); arrs[1] = new Array("华盛顿","纽约","洛杉矶","西雅图","费城"); arrs[2] = new Array("莫斯科","海参崴","列宁格勒","北京","北京"); arrs[3] = new Array("伦敦","伯明翰","英国","不列颠","斯里兰卡"); //下拉列表联动 function getCity(){ //获取下拉联动框点击到的值 var opEl = document.getElementById("selectValue").value; var REopEl = document.getElementById("REselectValue"); for(var i=REopEl.options.length;i>0;i--){ REopEl.options[i] = null; } //遍历二维数组,判断拿到的值刚好是二维数组中的哪一个城市数组 opEl = opEl - 1; for(var i=0; i<arrs[opEl].length; i++){ var CreateOption = document.createElement("option");//<option></option>创建一个元素 var CreadteNode = document.createTextNode(arrs[opEl][i]);//城市名称_创建一个城市名称文本节点 CreateOption.appendChild(CreadteNode);//<option>城市名</option> var REselectValue = document.getElementById("REselectValue");//获取联动下拉框的控制权 REselectValue.appendChild(CreateOption);//将创建的元素添加到联动框下 } } </script> </head> <body> <div style="width: 90%; border: 5px solid purple; height: 800px;"> <br /> <br /> <br /> <h1>欢迎新用户注册</h1> <br /> <div style="width: 500px; height: 400px; border: 5px solid purple;"> <br /> <br /> <form action="/Day67/Servlet_Login" method="post" onsubmit="return buttonSubmit()"> 用户名   :<input id="username" type="text" name="name" onfocus="tips('username','用户名需要以字母或_开头')" onblur=""/><span id="usernameSpan"></span> <br /> <br /> 密       码:<input id="password" type="text" name="password" onfocus="tips('password','密码至少为六位')"/><span id="passwordSpan"></span> <br /> <br /> 确认密码:<input id="rePassword" type="text" name="确认密码"/><span id="rePasswordSpan"></span> <br /> <br /> 性       别: <input type="radio" name="sex" value="man"/>man        <input type="radio" name="sex" value="woman"/>woman <br /> <br /> 籍       贯: <select id="selectValue" name="selectName" onchange="getCity()"> <option value="0"> --请选择-- </option> <option value="1"> 中国 </option> <option value="2"> 美国 </option> <option value="3"> 俄罗斯 </option> <option value="4"> 英国 </option> </select> <select id="REselectValue" name="selectName"> <option> --请选择-- </option> </select> <br /> <br /> 爱     好: <input type="checkbox" name="hobby" value="跆拳道"/>跆拳道 <input type="checkbox" name="hobby" value="柔道"/>柔道 <input type="checkbox" name="hobby" value="散打"/>散打 <br /> <br /> Email:<input type="text" /> <br /> <br /> <input type="submit" name="提交" value="提交"/> </form> </div> </div> </body> </html>

web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Day67</display-name> <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> <servlet> <description></description> <display-name>Servlet_Login</display-name> <servlet-name>Servlet_Login</servlet-name> <servlet-class>Servlet.Servlet_Login</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet_Login</servlet-name> <url-pattern>/Servlet_Login</url-pattern> </servlet-mapping> </web-app>
转载请注明原文地址: https://www.6miu.com/read-1650378.html

最新回复(0)