<form action="login" method="post">
用户:<input type="text" name=user><br>
密码:<input type="password" name=password<br>
<input type="submit" value="登陆">
</form>
action=“要提交给服务器端在web.xml注册过的servelt的名字”
method=“提交给服务器的方法有get、post,这里选择post”
user、password:我们要提交的数据名
package servlets;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//第三步讲到bean以及DAO的实现
import com.bean.User;
import com.dao.UserDAO;
public class login extends HttpServlet {
private static final long serialVersionUID = 1L;
//客户端采用post方法传值,因此实现doPost方法
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置接收客户端的数据编码格式
request.setCharacterEncoding("UTF-8");
//用getParameter(“”)方法获取客户端传来的数据
//这里双引号的名字必须与登陆页面设置的数据名字相同
String user=request.getParameter("user");
String password=request.getParameter("password");
//实例化用户并调用bean里的方法赋值对象
User u1=new User();
u1.setUser(user);
u1.setPassword(password);
//创建DAO对象,接下调用DAO中的方法实现对数据库的访问
UserDAO dao = new UserDAO();
//创建跳转对象
RequestDispatcher dis = null;
try{
//调用DAO的find方法,传去参数要查询的对象u1
//如果用户名正确返回的值为User对象,则登陆成功,否则返回null登陆失败
if(dao.find(u1)!=null)
{
//登陆成功的处理,设置要回应给客户端的属性名以及值,还有跳转页面
request.setAttribute("loginName",dao.find(u1).getUser());
dis=request.getRequestDispatcher("/succes.jsp");
dis.forward(request, response);
}
else
{
//登陆失败跳转失败页面
dis=request.getRequestDispatcher("/error.jsp");
dis.forward(request, response);
}
}catch (Exception localException)
{
}
}
<?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"
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Doge</display-name>
<servlet>
<!-- servlet的名称必须和mapping种的相同 -->
<servlet-name>login</servlet-name>
<!-- servlst类的路径 -->
<servlet-class>servlets.login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<!-- servlet的访问地址 -->
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
package com.bean;
public class User
{
private String user;
private String password;
public String getUser(){return this.user;}
public void setUser(String user){this.user = user;}
//password的get、set的方法实现
public String getPassword(){return this.password;}
public void setPassword(String password){this.password = password;}
}
1.实现业务处理的逻辑借口,DAO通过继承这些接口的方法来实现我们想要使用的功能
package com.dao;
import com.bean.User;
public abstract interface IOUserDAO
{
//创建接口数据库的查询
public abstract User find(User user)throws Exception;
}
2.实现DAO对象数据库的查询
package com.dao;
import com.bean.User;
//我将数据库的连接和关闭用一个DbConnect类实现
import com.db.DbConnect;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//继承接口
public class UserDAO implements IOUserDAO
{
//定义我们要使用的MySql查询语句
protected static String SELECT_SQL = "SELECT *from user where username=? and passwd=?";
//实现方法,接收要处理的对象
public User find(User user)throws Exception
{
//创建数据库连接接口对象
Connection con = null;
//创建执行SQL语句的接口
PreparedStatement prepStmt = null;
//创建SQL执行完毕后的结果集接口
ResultSet rs = null;
//创建返回的对象
User u = null;
try
{
//数据库连接
con = DbConnect.getDBconnection();
//预执行SQL语句
prepStmt = con.prepareStatement(SELECT_SQL);
//初始化SQL语句,传递参数值
prepStmt.setString(1, user.getUser());
prepStmt.setString(2, user.getPassword());
//执行SQL语句,返回的结果集由rs结果集对象接收
rs = prepStmt.executeQuery();
//rs.next()返回查询结果有效true,无效fals
if (rs.next())
{
//查询数据库有该用户,赋值要返回的对象u
u=new User();
u.setUser(rs.getString("username"));
u.setPassword(rs.getString("passwd"));
}
}
catch (Exception localException) {}finally
{
DbConnect.closeDB(con, prepStmt, rs);
}
//返回对象
return u;
}
}
package com.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DbConnect
{
//驱动程序名
private static String driverName = "com.mysql.jdbc.Driver";
//数据库用户名
private static String userName = "root";
//数据库密码
private static String userPwd = "412563";
//要连接的数据库名
private static String dbName = "doge";
//数据库的连接
public static Connection getDBconnection()
{
String url1 = "jdbc:mysql://localhost:3306/" + dbName;
String url2 = "?user=" + userName + "&password=" + userPwd;
String url3 = "&useUnicode=true&characterEncoding=UTF-8";
//串联在一起的数据库连接字
String url = url1 + url2 + url3;
try
{
//加载并注册驱动
Class.forName(driverName);
return DriverManager.getConnection(url);
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
//数据库的关闭
public static void closeDB(Connection con, PreparedStatement pstm, ResultSet rs)
{
try
{
//关闭结果集
if (rs != null) {
rs.close();
}
//预处理SQL接口
if (pstm != null) {
pstm.close();
}
//关闭数据库连接
if (con != null) {
con.close();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
可以看到在填写完账户密码提交后,浏览器的地址跳转到了处理servlet的地址
1.要向项目种导入数据库驱动程序文件:mysql-connector-java-5.0.7-bin.java(这是我用的版本)
2.在MySql中创建相应的数据库名称以及表,并且确保数据库服务已经运行成功
3.想要实现更多的功能增删改查,只需要在IOUserDAO声明接口方法,并在DAO中实现,servlet中调用即可