Eclipse新建web项目,实现登录验证功能
来源:互联网 发布:正规贵金属行情软件 编辑:程序博客网 时间:2024/05/16 06:55
安装好MySQl数据库
新建JavaWeb项目
项目名称为test3
在Web-Content目录下新建login.jsp文件 在浏览器中,需要提交的东西,使用表单form标签来进行提交,form中的action的值是表单数据提交到的jsp或Servlet的地址
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Login Page</title> </head><body><form action="LoginServlet"> //此处的LoginServlet为相对位置username:<input type ="text" name="username"/><br>password:<input type = "password" name="password"/><br><input type ="submit" name ="submit"/></form></body></html>
新建名为MySQL的类来进行数据库操作
package com.lx.uestc;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class MySQL { private Connection connection = null;public MySQL(){ /*加载JdBC驱动 * */ try { Class.forName("com.mysql.jdbc.Driver"); System.out.println("load Driver successfully"); } catch (Exception e) { System.out.println("load Driver error"); e.printStackTrace(); } /*连接数据库 * */ try { connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","liangxi333"); //jdbc:mysql://localhost:端口号/数据库名称","用户名","密码" System.out.println("connect sql successfully"); } catch (Exception e) { System.out.println("connect sql erro"); e.printStackTrace(); } /*检查用户账号密码正否 * */}/*连接数据库后要记得关闭 * */private void CloseSqlConnect(Connection connection ){ if (connection != null) try{ connection.close(); }catch (Exception e) { e.printStackTrace(); }}private ResultSet execSQL(String sql,Object... args) throws SQLException{ //建立PreparedStatement对象 PreparedStatement pStmt = connection.prepareStatement(sql); //为pStmt对象设置SQL参数值 for(int i = 0; i < args.length; i++){ pStmt.setObject(i+1, args[i]); } //执行SQL语句 pStmt.execute(); //返回结果集,如果执行的SQL语句不返回结果集,则返回null return pStmt.getResultSet();}public String checkUser(String username,String password){ boolean has_username = false; boolean password_correct = false; ResultSet rs = null; try { rs = execSQL("select * from user"); } catch (SQLException e) { System.err.println("查询数据库出错"); e.printStackTrace(); return null; } try { while(rs.next()){ String temp_username = rs.getString("name").trim(); String temp_password = rs.getString("password").trim(); if(username.equals(temp_username)){ has_username = true; if(password.equals(temp_password)){ password_correct = true; return "hasUserNameAndPasswordCorrect"; } return "hasUserNameButPasswordInCorrect"; } } } catch (SQLException e) { System.err.println("操作ResultSet出错"); e.printStackTrace(); } return "hasNoUserName";}}
新建LoginServlet来响应浏览器请求
import java.io.IOException;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 javax.servlet.http.HttpSession;import java.io.OutputStream;import java.io.PrintWriter;public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; public LoginServlet() { super(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); String username = request.getParameter("username"); String password = request.getParameter("password"); PrintWriter output = response.getWriter(); output.println("<html><head><title> hello</title></head>"); MySQL mss = new MySQL(); //实例化数据库对象 String result = mss.checkUser(username,password); if (result.equals("hasUserNameAndPasswordCorrect")) { output.println("<body>user and password correct</body></html>"); System.out.println("用户名和密码均正确"); } else if (result.equals("hasUserNameButPasswordInCorrect")) { output.println("<body>user correct but password wrong</body></html>"); System.out.println("用户名正确,密码不正确"); } else if (result.equals("hasNoUserName")) { output.println("<body>no user</body></html>"); System.out.println("没有此用户"); } output.flush(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); }}
测试工程
今天在测试中遇到一个问题,浪费很多时间,在测试的过程中,发现MySQL在实例化的过程中出错,原因是找不到 com.mysql.jdbc.Driver ,但是我确实是导入到工程中的。 最后发现原因:tomcat在加载JDBC驱动时,Class.forName("com.mysql.jdbc.Driver");不会在项目的lib目录下寻找,而是在Tomcat—>lib目录下寻找,解决方法就是直接拷贝mysql-connector-java-5.0.8.jar到该目录下就可以 最后附上测试结果
0 0
- Eclipse新建web项目,实现登录验证功能
- eclipse怎么样新建web项目,eclipse新建web项目
- eclipse怎么样新建web项目,eclipse新建web项目
- Eclipse新建Maven web项目
- eclipse新建maven web项目
- Eclipse新建maven web项目
- eclipse 新建maven web项目
- Eclipse 新建maven web项目
- Eclipse中新建web项目
- eclipse新建JAVA项目导入web项目
- Django实现登录验证功能
- 实现登录注册验证功能
- WCF广州本田整车销售系统技术解析(一) 新建项目与登陆验证功能实现
- Eclipse中新建动态web项目
- 在eclipse中新建一个web项目
- eclipse新建dynamic Web project项目
- ubuntu eclipse 如何新建web项目
- Eclipse中使用Maven新建Web项目
- Java8 简洁的匿名内部类使用Demo
- 微信投票管理系统(三)E-R图
- 网络爬虫代码(数据筛选情是有jsoup)
- 51单片之LED的点亮
- cpu、内存、硬盘和指令那些事
- Eclipse新建web项目,实现登录验证功能
- iOS 导航栏返回按钮自定义
- 黑盒测试基本方法
- 重温jvm中的happen-before
- 初学反射
- 分享一个微信公众号id转换为biz的接口
- NSCache
- Android笔记2016_04_01
- Eilxir解决哲学家就餐问题