一.增加员工
/EmpManager/src/main/webapp/add_emp.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>增加员工</title>
</head>
<body>
<!--
当前: /EmpManager/add_emp.html
目标: /EmpManager/add.emp
-->
<form action="add.emp" method="post">
<p>
姓名:<input type="text" name="ename"/>
</p>
<p>
职位:<input type="text" name="job"/>
</p>
<p>
薪资:<input type="text" name="sal"/>
</p>
<p>
<input type="submit" value="保存"/>
</p>
</form>
</body>
</html>
/EmpManager/src/main/java/web/AddEmpServlet.java
package web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.EmpDao;
import entity.Emp;
public class AddEmpServlet extends HttpServlet {
@Override
protected void service(
HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
//1.接收参数
req.setCharacterEncoding("utf-8");
String ename = req.getParameter("ename");
String job = req.getParameter("job");
String sal = req.getParameter("sal");
//2.保存员工数据
Emp e = new Emp();
e.setEname(ename);
e.setJob(job);
e.setSal(Double.valueOf(sal));
new EmpDao().save(e);
//3.发送响应
// res.setContentType(
// "text/html;charset=utf-8");
// PrintWriter w = res.getWriter();
// w.println("<p>保存成功</p>");
// w.close();
//重定向到查询页面,即
//建议浏览器自己去访问查询页面.
//当前: /EmpManager/addEmp
//目标: /EmpManager/findEmp
res.sendRedirect("findEmp");
}
}
/EmpManager/src/main/webapp/WEB-INF/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" version="2.5">
<display-name>EmpManager</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-name>findEmp</servlet-name>
<servlet-class>web.FindEmpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>findEmp</servlet-name>
<url-pattern>/findEmp</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>addEmp</servlet-name>
<servlet-class>web.AddEmpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>addEmp</servlet-name>
<url-pattern>/addEmp</url-pattern>
</servlet-mapping> -->
<servlet>
<servlet-name>emp</servlet-name>
<servlet-class>web.EmpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>emp</servlet-name>
<url-pattern>*.emp</url-pattern>
</servlet-mapping>
</web-app>
二.重定向
三.访问路径
1.项目部署的过程
2.如何获取访问路径
- 项目名: getContextPath()
- Servlet访问路径: getServletPath()
- URI: getRequestURI()
- URL: getRequestURL()
3.URI和URL的区别
1)狭义的理解(Java WEB 项目)
- URI: 绝对路径
- URL: 完整路径
2)广义的理解(任意 WEB 项目) *
- URI: 资源的名字
- URL: 资源的真名
URI包含URL
4.如何配置Servlet访问路径?
1)精确匹配
- /hello
- 必须通过”/hello”才能访问此Servlet
- 此Servlet只能处理”/hello”这一个请求
2)通配符
- /*
- 通过任何路径都能访问此Servlet
- 此Servlet能处理一切请求
3)后缀
- *.abc
- 以”.abc”为后缀的请求都能访问此Servlet
- 此Servlet能够处理多个请求
package web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AbcServlet extends HttpServlet {
@Override
protected void service(
HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
//获取访问路径
System.out.println(
req.getContextPath());
System.out.println(
req.getServletPath());
System.out.println(
req.getRequestURI());
System.out.println(
req.getRequestURL());
//若没有设置响应信息的话,服务器
//依然会向浏览器做出默认的响应,
//但响应的内容为空.
}
}
5.如何使用一个Servlet处理多个请求?
<?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" version="2.5">
<display-name>servlet4</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-name>demo</servlet-name>
<servlet-class>web.DemoServlet</servlet-class>
<!-- 启动时第2个加载此Servlet -->
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>demo</servlet-name>
<url-pattern>/demo</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>web.LoginServlet</servlet-class>
<init-param>
<param-name>maxonline</param-name>
<param-value>3000</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>findDept</servlet-name>
<servlet-class>web.FindDeptServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>findDept</servlet-name>
<url-pattern>/findDept</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>findEmp</servlet-name>
<servlet-class>web.FindEmpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>findEmp</servlet-name>
<url-pattern>/findEmp</url-pattern>
</servlet-mapping>
<!--
给所有的Servlet预置参数,该参数由context
对象自动读取.
Tomcat启动时首先就会创建context对象,它会
给每个项目创建唯一的一个context.
context内部有静态块,首次调用时就执行,
此块内加载了web.xml中的参数.
-->
<context-param>
<param-name>size</param-name>
<param-value>10</param-value>
</context-param>
<servlet>
<servlet-name>init</servlet-name>
<servlet-class>web.InitServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>up</servlet-name>
<servlet-class>web.UpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>up</servlet-name>
<url-pattern>/up</url-pattern>
</servlet-mapping>
</web-app>
四.Servlet生命周期
五.ServletConfig和ServletContext
1.它们的作用
- 都能够读取web.xml中为Servlet预置的参数
2.它们的区别
- config和Servlet是1对1的关系
- context和Servlet是1对多的关系
- 若数据只给某个Servlet使用,则用config
- 若数据给多个Servlet使用,则用context
它们的关系由服务器来保障
3.config使用场景
- 假设要开发一个网页游戏,若超过人数上限则要排队
- 开发登录功能LoginServlet
- 人数上限应该是一个可配置的参数maxOnline
- 该参数由LoginServlet使用
由于该参数只是LoginServlet使用,由config读取即可
ServletConfig,一对一 和 init 调用时机讲解:
package web;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
/**
* 1.config对象由tomcat创建及传入
*
* LoginServlet servlet = new LoginServlet();
* //Tomcat每创建一个Servlet后,就会立刻给它
* //创建一个config对象.
* //config内有静态块,调用时加载参数(首次访问)
* ServletConfig cfg = new ServletConfig();
* //通过init方法传入此config对象
* servlet.init(cfg);
* //父类的init(cfg)方法内部调用了init()
*
* 2.使用config不是必须要重写init(),
* 若在service()中调用config,通过父类
* 提供的getServletCofig()就可以获取它.
* 该方法返回的config就是init()所传入
* 的那个config.
*
* */
@Override
public void init(ServletConfig config) throws ServletException {
System.out.println("调用有参的init()");
super.init(config);
}
@Override
public void init() throws ServletException {
System.out.println("调用无参的init()");
super.init();
}
@Override
protected void service(
HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
//获取在线人数最大值
ServletConfig cfg = getServletConfig();
String maxonline =
cfg.getInitParameter("maxonline");
System.out.println(maxonline);
}
}
ServletContext,一对多:
package web;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FindDeptServlet extends HttpServlet {
@Override
protected void service(
HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
//获取每页显示的行数
ServletContext ctx = getServletContext();
String size = ctx.getInitParameter("size");
System.out.println(size);
//统计流量
Integer count = (Integer)
ctx.getAttribute("count");
ctx.setAttribute("count", ++count);
System.out.println("流量:"+count);
}
}
package web;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FindEmpServlet extends HttpServlet {
@Override
protected void service(
HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
//获取每页显示的行数
ServletContext ctx = getServletContext();
String size = ctx.getInitParameter("size");
System.out.println(size);
//统计流量
Integer count = (Integer)
ctx.getAttribute("count");
ctx.setAttribute("count", ++count);
System.out.println("流量:"+count);
}
}