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
    评论
以下是一个简单的Web Servlet注解配置示例: 首先,我们需要在pom.xml文件中添加javax.servlet-api依赖: ```xml <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> ``` 接下来,创建一个名为HelloServlet的Java类,用于处理HTTP请求: ```java 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; @WebServlet("/hello") public class HelloServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Hello World!"); } } ``` 在上面的例子中,我们使用@WebServlet注解将URL模式/hello映射到HelloServlet类。当浏览器向/hello发送GET请求时,doGet方法将被调用,向客户端输出“Hello World!”消息。 最后,在web.xml文件中添加以下内容,将HelloServlet添加到Web应用程序中: ```xml <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app> ``` 这样,我们就完成了一个简单的Web Servlet注解配置实例
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值