Serlvet接口Sun公司有两个默认的实现类:HeepServlet
<modules> <module>servlet01</module> </modules>
<parent> <artifactId>javaweb-02-serlvet</artifactId> <groupId>com.xianyudoufu</groupId> <version>1.0-SNAPSHOT</version> </parent>
父项目中的java子项目可以直接使用
son extends father
Maven环境又换
1. 修改web.xml为罪行
2. 将maven结构搭建完整
public class HelloServlet extends HttpServlet { //由于get或者post只是请求实现的不同的方式,可以互相调用,业务逻辑都一样 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter writer = resp.getWriter();//响应流 writer.print("你好呀"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
<!-- 注册serlvet--> <servlet> <servlet-name>hello</servlet-name> <servlet-class>com.xianyudoufu.serlvet.HelloServlet</servlet-class> </servlet> <!-- Servlet的请求路径--> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping>
注意:配置项目发的路径就可以了
Servlet是由服务器调佣,web服务器在收到浏览器请求之后,会:
<servlet-mapping> <servlet-name>hehe</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping>
<servlet-mapping> <servlet-name>hehe</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>hehe</servlet-name> <url-pattern>/hello2</url-pattern> </servlet-mapping>
<servlet-mapping> <servlet-name>hehe</servlet-name> <url-pattern>/hello/*</url-pattern> </servlet-mapping>
<servlet-mapping> <servlet-name>hehe</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
<servlet-mapping> <servlet-name>hehe</servlet-name> <url-pattern>*.xianyudoufu</url-pattern> </servlet-mapping>
<!-- 404--> <servlet> <servlet-name>error</servlet-name> <servlet-class>com.xianyudoufu.servlet.ErrorServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>error</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
web容器在启动的时候,它会为每个web程序都创建一个对象的ServletContext对象,它代表了当前的web应用
public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // this.getInitParameter(); 初始化参数 // this.getServletConfig(); Servlet配置 // this.getServletContext() Servlet 上下们 // ServletContext context = this.getServletContext(); String username = "咸鱼豆腐"; context.setAttribute("username",username);//将一个数据保存在了ServletContext中,名字为:username,值username System.out.println("哈哈"); } }
读取它的类
public class GetServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context = this.getServletContext(); String uername =(String) context.getAttribute("username"); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); resp.getWriter().print("名字:"+uername); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
xml配置:
<servlet> <servlet-name>nihao</servlet-name> <servlet-class>com.xianyudoufu.servlet.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>nihao</servlet-name> <url-pattern>/nihao</url-pattern> </servlet-mapping> <servlet> <servlet-name>getc</servlet-name> <servlet-class>com.xianyudoufu.servlet.GetServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>getc</servlet-name> <url-pattern>/getc</url-pattern> </servlet-mapping>