1、servlet介绍
1.1 概念
- Servlet它是JavaWEB三大组件(Filter/Listener)之一,也是最重要的一个
- 它是一个接口(规范),servlet是运行在 Web服务器中的小型 Java程序
- servlet 通常通过 HTTP(超文本传输协议)接收和响应来自 Web客户端的请求
1.2 servlet作用
- 获取请求数据
- 处理请求
- 完成响应
1.3 如何使用
- 编写一个类,实现servlet接口
- 在web.xml文件中,配置这个类
- 注册servlet
- 绑定路径
- 访问
2、入门案例
2.1 编写HelloServlet类
package com.itheima.web.servlet;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class HelloServlet implements Servlet {
@Override
public void init(ServletConfig config) throws ServletException {
}
/**
* 所有处理用户请求的代码都写在service方法中
*/
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
res.setContentType("text/html;charset=utf-8");
System.out.println("这是我的一个servlet程序!");
//向浏览器输出数据
res.getWriter().write("Servlet快速入门");
}
@Override
public void destroy() {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public String getServletInfo() {
return null;
}
}
2.2 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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>web_day34</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 注册servlet -->
<servlet>
<!-- servlet-name:给当前的servlet起个名字 全局唯一 -->
<servlet-name>HelloServlet</servlet-name>
<!-- servlet-class: servlet的全限定类名 -->
<servlet-class>com.itheima.web.servlet.HelloServlet</servlet-class>
</servlet>
<!-- servlet-mapping:绑定路径 -->
<servlet-mapping>
<!-- servlet-name: 使用上面起好的名字,建议复制过来 -->
<servlet-name>HelloServlet</servlet-name>
<!-- url-pattern:访问路径,要求:必须以“/”开头,唯一 -->
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>
2.3 访问路径:
http://localhost:8080/web_day34/HelloServlet
2.4 执行结果
2.5 servlet执行流程
3、servlet体系结构
4、登录案例
login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录案例</title>
</head>
<body>
<form action="http://localhost:8080/web_day34/LoginServlet">
用户名:<input type="text" name="username" /><br/>
密码:<input type="password" name="password" /><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
LoginServlet编写
package com.itheima.web.servlet;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import com.itheima.domain.User;
import com.itheima.service.LoginService;
import com.itheima.service.impl.LoginServiceImpl;
public class LoginServlet implements Servlet {
@Override
public void init(ServletConfig config) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest request, ServletResponse res) throws ServletException, IOException {
// 1.封装请求参数
String username = request.getParameter("username");
String password = request.getParameter("password");
// 2.封装Javabean
User user = new User();
user.setUsername(username);
user.setPassword(password);
// 3.调用service层方法
LoginService service = new LoginServiceImpl();
User existUser;
try {
existUser = service.login(user);
if (existUser != null) {
System.out.println("恭喜您,登录成功");
} else {
System.out.println("用户名和密码错误,请重新输入");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
LoginServiceImpl
package com.itheima.service.impl;
import java.sql.SQLException;
import com.itheima.dao.LoginDao;
import com.itheima.dao.impl.LoginDaoImpl;
import com.itheima.domain.User;
import com.itheima.service.LoginService;
public class LoginServiceImpl implements LoginService {
private LoginDao dao = new LoginDaoImpl();
@Override
public User login(User user) throws SQLException {
return dao.query(user);
}
}
LoginDaoImpl
package com.itheima.dao.impl;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import com.itheima.dao.LoginDao;
import com.itheima.domain.User;
import com.itheima.utils.C3P0Utils;
public class LoginDaoImpl implements LoginDao {
@Override
public User query(User user) throws SQLException {
//1.获得QueryRunner核心对象
QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
//2.编写SQL语句
String sql = "select * from user where username=? and password=?";
//3.设置实际参数
Object[] params = {user.getUsername(),user.getPassword()};
//4.执行查询操作
return qr.query(sql, new BeanHandler<>(User.class), params);
}
}