使用开发工具编写Servlet,仍要手工导入servlet-api.jar文件,并与项目关联。
创建MyServlet,实现Servlet接口,覆盖5个方法
package com.hyqwsq.servlet;
import javax.servlet.*;
import java.io.IOException;
public class MyServlet implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
// 提供服务
System.out.println("My First Web Project");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
<?xml version="1 .0" encoding=,,UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/]avaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd
version="3.1">
<! --1 添加servlet节点-->
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.qf.servlet.MyServlet</servlet-class>
</servlet>
<!--2 添加 servlet-mapping 节点一>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/myservlet</url-pattern>
</servlet-mapping>
</web-app>
在Tomcat的webapps目录下,新建WebProject项目文件夹
创建WEB-INF,存放核心文件
在WEB-INF下,创建classes文件夹,将编译后的MyServlet.class文件复制至此。
问题:每当我们编写了新的Servlet或者重新编译,都需要手工将新的.class部署到Tomcat中,较为麻烦。如何实现自动部署?
前面我们是在Tomcat的webapps目录新建应用程序目录myweb,然后把静态资源和Servlet复制到相关目录下。使用IDEA不需要我们复制 了。可以通过IDEA集成Tomcat服务器,实现自动部署。
只是继承了tomcat,和项目还没有关系
二次配置:
Apply–>OK
在 WEB-INF 目录下新建lib目录
输入lib目录
右键lib目录,选择Add as Library集成
选择Project Library,完成。
项目完成后,有时候需要打成war方便部署。war包可以直接放入Tomcat的webapps目录中,启动Tomcat后自动解压,即可访问。
点击项目结构
当用户第一次访问Servlet时,由容器调用Servlet的构造器创建具体的Servlet对象。也可以在容器启动之后立刻创建实例。使用如下代 码可以设置Servlet是否在服务器启动时就创建
•注意:只执行一次
在初始化阶段,init()方法会被调用。这个方法ajavax.servlet.Servlet}g口中定义。其中,方法以一个ServletConfig类型的对象作为参 数
•注意:init方法只被执行一次
当客户端有一个请求时,容器就会将请求ServletRequest与响应ServletResponse对象转给Servlet,以参数的形式传给service方法
•此方法会执行多次
当Servlet容器停止或者重新启动都会引起销毁Servlet对象并调用destroy方法
• destroy方法执行一次
package com.hyqwsq.Life;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/lifeservlet")
public class LifeServlet extends HttpServlet {
public LifeServlet() {
super();
System.out.println("1.完成实例化");
}
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
System.out.println("2. 完成初始化");
}
//因为要不断接收请求,响应结果
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("3. 就绪中");
//
resp.getWriter().append("Served at: ").append(req.getContextPath());
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
public void destroy() {
super.destroy();
System.out.println("4.销毁了");
}
}
Servlet在访问之后,会执行实例化操作,仓腱一个Servlet对象。而我们Tomcat容器可以同时多个线程并发访问同一个Servlet,如果在 方法中对成员变量做修改操作,就会有线程安全的问题。
synchronized
实现 SingleThreadModel 接口
尽可能使用局部变量
package com.hyqwsq.servletSafe;
import com.hyqwsq.servlet1.HttpServlet;
import javax.servlet.ServletException;
import javax.servlet.SingleThreadModel;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class SafeServlet extends HttpServlet implements SingleThreadModel {
/**
* 不安全代码:
* 当A,B需要调用这个servlet,A执行的时候出发了servlet的实例化对象,
* 创建了一个servlet,然后持有一个空的字符串message,然后A发送get请求,输入用户名和密码
* A得到登录失败的结果,就在这是,A的线程时间片没了,A线程就会停留在 message = “登录失败” 这行的结果上
* 这时候B线程进来,又将message初始化为空,接着也发送get请求登录,并且登录成功,得到登录成功的结果,完成message的赋值和客户端的响应
* 等A线程醒来,发现message被改为了登录成功,并且响应了登录成功回去,但是没办法做下一步的操作
* 结果就是在一个请求的方法里做了两次对成员变量的修改操作,就会导致数据不准确
*/
private String message = "";
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String message = "";
// 假设:
// 1: 接收参数
// 2. 调用业务逻辑 得到登录结果
message = "登录成功";//或者登录失败
PrintWriter printWriter = resp.getWriter();
printWriter.println(message);
}
/**
* 方法一:
* synchronized锁
* 缺点:
* 在多线程并发访问的情况下,效率极低,因为同一时间只能处理一个线程的登录
*/
private String message = "";
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
synchronized(this){
message = "登录成功";//或者登录失败
PrintWriter printWriter = resp.getWriter();
printWriter.println(message);
}
}
/**
* 方法三:
* 创建局部变量,任何线程拿到的都是局部变量,不互相影响
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 创建局部变量
String message = "";
message = "登录成功";//或者登录失败
PrintWriter printWriter = resp.getWriter();
printWriter.println(message);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
HTTP协议是无状态的,不能保存每次提交的信息
如果用户发来一个新的请求,服务器无法知道它是否与上次的请求有联系。
对于那些需要多次提交数据才能完成的Web操作,比如登录来说,就成问题了。
将浏览器与web服务器之间多次交互当作一个整体来处理,并且将多次交互所涉及的数据(即状态)保存下来,管理每次请求与响应的数据
客户端状态管理技术:将状态保存在客户端。代表性的是Cookie技术。
服务器状态管理技术:将状态保存在服务器端。代表性的是session技术(服务器传递sessionlD时需要使用Cookie的方式)
Cookie是在浏览器访问Web服务器的某个资源时,由Web服务器在HTTP响应消息头中附带传送给浏览器的一小段数据。
一旦Web浏览器保存了某个Cookie,那么它在以后每次访问该Web服务器时,都应在HTTP请求头中将这个Cookie回传给Web服务器。
—个Cookie主要由标识该信息的名称(name)和值(value)组成。
package com.hyqwsq.Cookies;
import com.hyqwsq.servlet1.HttpServlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/cs")
public class CookieServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1. 服务器创建Cookie对象
Cookie cookie = new Cookie("username","gavin");
// 1.1 设置Cookie的访问路径
//cookie.setPath("//Servlet_code_demo1_war_exploded"); // 如果是项目名称,代表该项目下所有资源都可以使用这个cookie
cookie.setPath("//Servlet_code_demo1_war_exploded/get"); //只有该项目里的get或者get下面的资源才可以访问
// 1.2 设置Cookie的有效期
cookie.setMaxAge(60*60);
// 2. 将Cookie响应给客户端
resp.addCookie(cookie); // 添加一个cookie到resp对象里,并响应给客户端
}
}
查看创建的cookie
只需要保证 Cookie 的 key 和 路径(Path) 一致即可修改
package com.hyqwsq.Cookies;
import com.hyqwsq.servlet1.HttpServlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/get")
public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1. 通过request对象获取所有的cookie
Cookie[] cookies = req.getCookies();
/**
* 若是客户端没有cookie,则会获取一个空数组
* 所以要加上判空,避免空指针异常
*/
if(cookies != null){
// 2. 因为得到的是cookie数组,不知道有几个cookie
// 所以通过循环遍历cookie
for(Cookie cookie : cookies){
System.out.println(cookie.getName()+":"+cookie.getValue()+"路径:" +cookie .getPath());
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
package com.hyqwsq.Cookies;
import com.hyqwsq.servlet1.HttpServlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/csc")
public class ChangeCookie extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 修改cookie需要key和路径同时一致
Cookie cookie = new Cookie("username","mark");
cookie.setPath("/Servlet_code_demo1_war_exploded/get");
cookie.setMaxAge(60*24*7);
resp.addCookie(cookie);
}
}
当访问cs后通过访问getServlet得到 username = ‘gavin’
的cookie,这时候访问csc修改key为username
且Path为/Servlet_code_demo1_war_exploded/get
的cookie,最后通过访问get来打印结果:
Cookie默认不支持中文,只能包含ASCII字符,所以Cookie需要对Unicode字符进行编码,否则会出现乱码
- 编码可以使用
java.net.URLEncoder
类的encode(String str,String encoding)
方法- 解码使用
java.net.URLEncoder
类的decode(String str,String encoding)
方法
package com.hyqwsq.URLEncoder;
import com.hyqwsq.servlet1.HttpServlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/cs3")
public class URLEncoderServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie cookie = new Cookie("姓名","张三");
cookie.setPath("/Servlet_code_demo1_war_exploded/get");
cookie.setMaxAge(600);
resp.addCookie(cookie);
}
}
package com.hyqwsq.Cookies;
import com.hyqwsq.servlet1.HttpServlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
@WebServlet("/cs3")
public class URLEncoderServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie cookie = new Cookie(
URLEncoder.encode("姓名","UTF-8"),
URLEncoder.encode("何义竏","UTF-8")
);
cookie.setPath("/Servlet_code_demo1_war_exploded/get");
cookie.setMaxAge(600);
resp.addCookie(cookie);
}
}
package com.hyqwsq.Cookies;
import com.hyqwsq.servlet1.HttpServlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
@WebServlet("/get")
public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1. 通过request对象获取所有的cookie
Cookie[] cookies = req.getCookies();
/**
* 若是客户端没有cookie,则会获取一个空数组
* 所以要加上判空,避免空指针异常
*/
if(cookies != null){
// 2. 因为得到的是cookie数组,不知道有几个cookie
// 所以通过循环遍历cookie
for(Cookie cookie : cookies){
System.out.println(
URLDecoder.decode(cookie.getName(),"UTF-8")+
":"+URLDecoder.decode(cookie.getValue(),"UTF-8")+
" 路径:" +cookie.getPath()
);
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
注意:Session 是由服务器创建的
session 是服务器端自动创建的,通过request对象获取
package com.hyqwsq.sessions;
import com.hyqwsq.servlet1.HttpServlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
@WebServlet("/ss")
public class SessionServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1. 因为Seesion是自动创建的,所以通过request对象获取Seesion对象
HttpSession session = req.getSession();
//2. Session是借助cookie发送到客户端
System.out.println("ID= "+session.getId());
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
访问结果:(在同一次会话中,session都是同一个)
session是借助cookie发送给客户端:
浏览器在没有关闭的情况下,当我们再一次访问的时候,响应头没有了set-cookie,反而是出现在了请求的时候发送给服务器的请求标头里:
package com.hyqwsq.sessions;
import ...
@WebServlet("/ss")
public class SessionServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1. 因为Seesion是自动创建的,所以通过request对象获取Seesion对象
HttpSession session = req.getSession();
//2. Session是借助cookie发送到客户端
System.out.println("ID= "+session.getId());
//2. 使用session保存数据
session.setAttribute("username","gavin");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
package com.hyqwsq.sessions;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/getValue")
public class GetValueServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.通过request获取session对象
HttpSession session = req.getSession();
String s = (String)session.getAttribute("username");
System.out.println("从session中获取了:"+s);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
package com.hyqwsq.sessions;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/remove")
public class RemoveServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.removeAttribute("username");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
package com.hyqwsq.sessions;
import ...
@WebServlet("/ss")
public class SessionServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1. 因为Seesion是自动创建的,所以通过request对象获取Seesion对象
HttpSession session = req.getSession();
//2. Session是借助cookie发送到客户端
System.out.println("ID= "+session.getId());
//3. 使用session保存数据
session.setAttribute("username","gavin");
//4. 使用request保存数据
req.setAttribute("password","123456");
//重定向,二次跳转
resp.sendRedirect("/Servlet_code_demo1_war_exploded/getValue");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
package com.hyqwsq.sessions;
import ...
@WebServlet("/getValue")
public class GetValueServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.通过request获取session对象
HttpSession session = req.getSession();
String s = (String)session.getAttribute("username");
String password = (String)req.getAttribute("password");
System.out.println("从session中获取了:"+s);
System.out.println("从request中获得了:"+password);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
访问结果:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
//设置session有效期
session.setMaxInactiveInterval(20);
System.out.println("sessionID: "+session.getId());
}
public class SessionLifeGetServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
System.out.println("sessionID: "+session.getId());
//手工销毁session
session.invalidate();
}
服务器在默认情况下,会使用Cookie的方式将sessionID发送给浏览器,如果用户禁止Cookie,则sessionID不会被浏览器保存,此时,服务器可以使用像URL重写这样的方式来发送sessionID
浏览器在访问服务器上的某个地址时,不再使用原来的那个地址,而是使用经过改写的地址(即在原来的地址后面z加上了sessionID)
resp.encodeRedirectURL(String url) 生成重写的URL
HttpSession = req.getSession();
//重写URL追加SessionID
String newUrl = response.encodeRedirectURL("/Servlet_code_demo1_war_exploded/cs");
System.out.println(newUrl2);
resp.sendRedirect(newUrl2);
create table manager(
username varchar(20) primary key,
password varchar(20) not null
)charset = utf8;
insert into manager(username,password) values('tom','123');
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>管理员登录title>
head>
<body>
<form action="/Servlet_code_demo1_war_exploded/loginMgr" method="post">
用户名:<input type="text" name="username"/>br>
密码:<input type="password" name="password"/>br>
<input type="submit" value="登录">
form>
body>
html>
AdminDao:
package com.hyqwsq.servletSessionProject.Dao;
import com.hyqwsq.servletSessionProject.entity.Admin;
import java.util.List;
public interface AdminDao {
public List<Admin> selectAll();
}
ManagerDao:
package com.hyqwsq.servletSessionProject.Dao;
import com.hyqwsq.servletSessionProject.entity.Manager;
public interface ManagerDao {
// 权限登录验证,返回一个Manager
public Manager select(String username);
}
AdminDaoImpl:
package com.hyqwsq.servletSessionProject.Dao.impl;
import com.hyqwsq.servletSessionProject.Dao.AdminDao;
import com.hyqwsq.servletSessionProject.entity.Admin;
import com.hyqwsq.servletSessionProject.utils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
public class AdminDaoImpl implements AdminDao {
private QueryRunner queryRunner = new QueryRunner();
@Override
public List<Admin> selectAll() {
List<Admin> admins = null;
try {
admins = queryRunner.query(
DbUtils.getConnection(),
"select * from admin",
new BeanListHandler<Admin>(Admin.class)
);
return admins;
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return null;
}
}
ManagerDaoImpl:
package com.hyqwsq.servletSessionProject.Dao.impl;
import com.hyqwsq.servletSessionProject.Dao.ManagerDao;
import com.hyqwsq.servletSessionProject.entity.Manager;
import com.hyqwsq.servletSessionProject.utils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import java.sql.SQLException;
public class ManagerDaoImpl implements ManagerDao {
private QueryRunner queryRunner = new QueryRunner();
@Override
public Manager select(String username) {
try {
Manager manager = queryRunner.query(
DbUtils.getConnection(),
"select * from manager where username = ?",
new BeanHandler<Manager>(Manager.class),//封装成实体类对象
username);//参数
return manager;
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return null;
}
}
Admin:
package com.hyqwsq.servletSessionProject.entity;
public class Admin {
private String username;
private String password;
private String phone;
private String address;
public Admin(String username, String password, String phone, String address) {
this.username = username;
this.password = password;
this.phone = phone;
this.address = address;
}
public Admin() {
}
@Override
public String toString() {
return "Admin{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", phone='" + phone + '\'' +
", address='" + address + '\'' +
'}';
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Manager:
package com.hyqwsq.servletSessionProject.entity;
public class Manager {
private String username;
private String password;
public Manager() {
}
@Override
public String toString() {
return "manager{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
public Manager(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
AdminService:
package com.hyqwsq.servletSessionProject.service;
import com.hyqwsq.servletSessionProject.entity.Admin;
import java.util.List;
public interface AdminService {
public List<Admin> showAllAdmin();
}
ManagerService:
package com.hyqwsq.servletSessionProject.service;
import com.hyqwsq.servletSessionProject.entity.Manager;
public interface ManagerService {
public Manager login(String username, String password);
}
AdminServiceImpl:
package com.hyqwsq.servletSessionProject.service.impl;
import com.hyqwsq.servletSessionProject.Dao.AdminDao;
import com.hyqwsq.servletSessionProject.Dao.impl.AdminDaoImpl;
import com.hyqwsq.servletSessionProject.entity.Admin;
import com.hyqwsq.servletSessionProject.service.AdminService;
import com.hyqwsq.servletSessionProject.utils.DbUtils;
import java.util.List;
public class AdminServiceImpl implements AdminService {
private AdminDao adminDao = new AdminDaoImpl();
@Override
public List<Admin> showAllAdmin() {
List<Admin> admins = null;
try {
DbUtils.begin();
admins =adminDao.selectAll();
DbUtils.commit();
} catch (Exception e) {
DbUtils.rollback();
e.printStackTrace();
}
return admins;
}
}
ManagerServiceImpl:
package com.hyqwsq.servletSessionProject.service.impl;
import com.hyqwsq.servletSessionProject.Dao.ManagerDao;
import com.hyqwsq.servletSessionProject.Dao.impl.ManagerDaoImpl;
import com.hyqwsq.servletSessionProject.entity.Manager;
import com.hyqwsq.servletSessionProject.service.ManagerService;
import com.hyqwsq.servletSessionProject.utils.DbUtils;
public class ManagerServiceImpl implements ManagerService {
private ManagerDao managerDao = new ManagerDaoImpl();
@Override
public Manager login(String username, String password) {
Manager manager = new Manager();
try {
manager = null;
DbUtils.begin();
Manager temp = managerDao.select(username);
if(temp != null){
if(temp.getPassword().equals(password)){
manager = temp;
}
}
DbUtils.commit();
} catch (Exception e) {
DbUtils.rollback();
e.printStackTrace();
}
return manager;
}
}
LoginMgrController:
package com.hyqwsq.servletSessionProject.servlet.controller;
import com.hyqwsq.servletSessionProject.entity.Manager;
import com.hyqwsq.servletSessionProject.service.ManagerService;
import com.hyqwsq.servletSessionProject.service.impl.ManagerServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
* 业务内容:
* 访问查询所用用户信息的servlet需要验证是否登录
*/
@WebServlet("/loginMgr")
public class LoginMgrController extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1. 处理乱码
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
// 2. 收参
String username = req.getParameter("username");
String password = req.getParameter("password");
// 3. 调用业务逻辑方法
ManagerService managerService = new ManagerServiceImpl();
Manager mgr = managerService.login(username,password);
// 4. 处理结果,流程跳转
if(mgr!=null){
//登录成功
//将管理员信息存储在Session里
HttpSession session = req.getSession();
session.setAttribute("mgr",mgr);
session.setMaxInactiveInterval(60*60);
//跳转 目标,方式
resp.sendRedirect("/Servlet_code_demo1_war_exploded/showallcontroller");
}else{
// mgr = null,表示登录失败,跳转会登录界面
resp.sendRedirect("/Servlet_code_demo1_war_exploded/loginMgr.html");
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
ShowAllAdminController:
package com.hyqwsq.servletSessionProject.servlet.controller;
import com.hyqwsq.servletSessionProject.entity.Admin;
import com.hyqwsq.servletSessionProject.entity.Manager;
import com.hyqwsq.servletSessionProject.service.AdminService;
import com.hyqwsq.servletSessionProject.service.impl.AdminServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
@WebServlet("/showallcontroller")
public class ShowAllAdminController extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
Manager manager = (Manager) session.getAttribute("mgr");
if(manager!=null){
AdminService adminService = new AdminServiceImpl();
List<Admin> adminList = adminService.showAllAdmin();
req.setAttribute("admins",adminList);
req.getRequestDispatcher("/showalljsp").forward(req,resp);
}else{
resp.sendRedirect("/Servlet_code_demo1_war_exploded/loginMgr.html");
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
ShowAllAdminJSP:
package com.hyqwsq.servletSessionProject.servlet.jsp;
import com.hyqwsq.servletSessionProject.entity.Admin;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@WebServlet("/showalljsp")
public class ShowAllAdminJSP extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
List<Admin> adminList = (List)req.getAttribute("admins");
// 2.通过流打印结果显示
PrintWriter printWriter = resp.getWriter();
if(adminList != null){
printWriter.println("");
printWriter.println( "");
printWriter.println( "");
printWriter.println( "显示所有 ");
printWriter.println( "");
printWriter.println( "");
printWriter.println( "");
printWriter.println( "");
printWriter.println( "username ");
printWriter.println( "password ");
printWriter.println( "phone ");
printWriter.println( "address ");
printWriter.println( " ");
for(Admin admin : adminList){
printWriter.println( "");
printWriter.println( ""+admin.getUsername()+" ");
printWriter.println( ""+admin.getPassword()+" ");
printWriter.println( ""+admin.getPhone()+" ");
printWriter.println( ""+admin.getAddress()+" ");
printWriter.println( " ");
}
printWriter.println( "
");
printWriter.println( "");
printWriter.println("");
}else{
printWriter.println("");
printWriter.println( "");
printWriter.println( "");
printWriter.println( "显示所有 ");
printWriter.println( "");
printWriter.println( "");
printWriter.println( "当前没有数据
");
printWriter.println( "");
printWriter.println("");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
获取当前项目在服务器发布的真实路径
String realpath = servletContext.getRealPath("/");
获取当前项目上下文路径(应用程序名)
System.out.println(servletContext.getContextPath()); //上下文路径(应用程序名称)
System.out.println(request.getContextPath());
ServletContext拥有作用域,可以存储数据到全局容器中
- 存储数据:servletContext.setAttribute(“name”,value);
- 获取数据:servletContext.getAttribute(“name”);
- 移除数据:setvletContext.removeAttribute(“name”);
ServletContext统计当前项目访问次数
package com.hyqwsq.Context;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
@WebServlet("/cc")
public class CounterController extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1. 获取ServletContext对象
ServletContext servletContext = req.getServletContext();
// 2. 获取计数器
Integer counter = (Integer)servletContext.getAttribute("counter");
if(counter == null){
counter = 1;
servletContext.setAttribute("counter",counter);
}else{
Random random = new Random();
counter += random.nextInt(100);
servletContext.setAttribute("counter",counter);
}
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset = utf-8");
PrintWriter printWriter = resp.getWriter();
printWriter.println("网站共访问次数:" + counter);
System.out.println("counter: " + counter);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
package com.hyqwsq.Context;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/showcc")
public class ShowCounterController extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1. 获取servletContext对象
ServletContext servletContext = req.getServletContext();
//2. 获取计数器
Integer counter = (Integer) servletContext.getAttribute("counter");
if(counter == null){
counter = 1;
servletContext.setAttribute("counter",counter);
}else{
counter++;
servletContext.setAttribute("counter",counter);
}
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset = utf-8");
PrintWriter printWriter = resp.getWriter();
printWriter.println("网站共访问次数:" + counter);
System.out.println("showcounter: " + counter);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
在以往的Servlet中,有没有冗余的代码,多个Servlet都要进行编写
过滤器(Filter)是处于客户端与服务器目标资源之间的一道过滤技术
Servlet API中提供了一个Filter接口,开发人员编写一个Java类实现了这个接口即可,这个Java类称之为过滤器(Filter)
package com.qf.web.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOExceprtion;
@WebFilter("/myservlet1") //过滤路径
public class MyFilter1 implements Filter{
// 初始化过滤器
@Override
public void init(FilterConfig filterConfig) throws ServletException{
System.out.println("过滤器初始化了......init..."+filterConfig);
}
// 执行过滤
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,ServletException{
System.out.println("过滤前....doFilter");
// 放行
chain.doFilter(request,response);
System.out.println("过滤器....doFilter");
}
// 销毁
@Override
public void destroy(){
System.out.println("销毁了......destory");
}
}
15.5 过滤器配置
15.5.1 注解配置
在自定义的Filter类上使用注解@WebFilter(value = “/过滤目的资源”);
15.5.2 xml配置
我们每个碎片最终能拼成一张照片