目录结构
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_3_0.xsd" metadata-complete="false" version="3.0">
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/Second</url-pattern>
</servlet-mapping>
</web-app>
web.xml中配置servlet
- <servlet-name>可以随便起名,但是要在<servlet>和<servlet-map>中一致。
- <servlet-class>是以WEB-INF/classes为根路径。在写完java文件后,将产生的class文件放到WEB-INF/classes。
- <url-pattern>可以随便起名,最后表现在URL中应用名之后的部分,即http://localhost/应用名/<url-pattern>,不能少去第一个/。
dao/DBdao
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBdao{
public static Connection getConnection(){
String dbUrl = "jdbc:mysql://localhost:3306/web?characterEncoding=UTF-8";
String dbUser = "root";
String dbPassword = "jinliang";
Connection conn = null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
}
- Class.forName("com.mysql.jdbc.Driver");Driver是一个java.sql包中的接口,该方法通过当前类加载器加载实现Driver接口的mysql数据库驱动,并返回Class类。
- DriverManager.getConnection()方法获得对数据库的连接
FirstServlet/java
import java.io.IOException;
import java.io.PrintStream;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import dao.DBdao;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FirstServlet extends HttpServlet{
public void service(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintStream out = new PrintStream(response.getOutputStream());
String name = request.getParameter("userName");
String password = request.getParameter("password");
Connection conn = null;
Statement statement = null;
String sql = "select * from user where username="+"'"+name+"'";
try{
//获得数据库连接
conn = DBdao.getConnection();
//获得Statement对象用来执行sql语句
statement = conn.createStatement();
//执行sql查询,并返回结果集对象
ResultSet rs = statement.executeQuery(sql);
if(rs.next()){
if(rs.getString("password").equals(password)){
out.print("<h1>success</h1>");
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
登陆界面
callFirstServlet.jsp
<%@ page pageEncoding="UTF-8"%>
<form method="post" action="Second">
<input type="text" name="userName"/>
<input type="password" name="password"/>
<input type="submit"/>
</form>
- form表单在点击提交后,通过post方式,将表单中的数据以name:value键值对存入request,并发送request至action,即web.xml中配置的FirstServlet。
- tomcat接收到表单请求后,将其层层包装,最后递交至FirstServlet中service方法的request参数,从参数中获得姓名和密码。将该密码与从数据库通过姓名查询到的密码比对,确定是否通过,并将结果放在response中,由tomcat再次包装后返回至客户端浏览器。