Servlet简易验证实例

利用Jdbc获取oracle数据库里面数据与用户在网页提交的数据进行匹配


一个超级简单的登陆页面 html写的

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>login.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
   <form action="helloworld" name="form" method="get">
    用户名:<input type="text" name="username"/> <br>
    密码啊:<input type="password" name="psd"/> <br>
    <input type="submit" value="login"/>
    
  </form>
  </body>
</html>

java代码主要分两部分

part1连接数据库部分(oracle)

package com.wenming.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;


public class dbcon {
	private static String driver="oracle.jdbc.driver.OracleDriver";
	private static String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
	private static String name="sys as sysdba";
	private static String psd="wenming";
	private static PreparedStatement ps=null;
	private static Connection con=null;
	/**
	 * 获得连接对象
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public static Connection getcon() throws ClassNotFoundException, SQLException{
		Class.forName(driver);
		
		con=DriverManager.getConnection(url, name, psd);
	
		return con;
		
	}
	/**
	 * 关闭数据库连接
	 * @throws SQLException
	 */
	void close() throws SQLException{
		if (con!=null) {
			con.close();
			con=null;
		}
	}
	
}

part2处理部分,该类需要继承httpservlet类

package com.wenming.test;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wenming.dao.dbcon;

public class helloworld extends HttpServlet {
	PreparedStatement ps=null;
	Connection connection=null;
	dbcon dbcon=null;
	ResultSet rs=null;
	String name=null;
	String psd=null;
	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		dbcon=new dbcon();
		PrintWriter out=response.getWriter();
		try {
			connection=dbcon.getcon();
			ps=connection.prepareStatement("select * from users");
			rs=ps.executeQuery();
			rs.next();
			name=rs.getString(2);
			psd=rs.getString(3);
			System.out.println(name);
			System.out.println(psd);
			rs.close();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String username=request.getParameter("username");
		String password=request.getParameter("psd");
		System.out.println(username);
		System.out.println(password);
		if (username.equals(name)&&psd.equals(password)) {
			out.println("success");
		}else {
			out.println("fail");
		}
		
	
	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值