Servlet
1、常见状态码
200:请求成功
302:请求重定向
304:请求资源没有改变,访问本地缓存
404:请求资源不存在。通常是用户路径编写错误,也可能是服务器资源已删除。
500:服务器内部错误。通常程序抛异常。
2、Tomcat的目录结构
bin目录下有开启和关闭的命令
Webapps下是以后发布web程序的地方【打成war包放在webapps下】
3、什么是Servlet
Servlet是运行在服务端的Java小程序,是sun公司提供一套规范(接口),用来处理客户端请求、响应给浏览器的动态资源。但servlet的实质就是java代码,通过java的API动态的向客户端输出内容。
4、servlet三种创建方式
生命周期
出生:第一次请求到达,servlet出生
服务:每次请求到达,都会调用service服务
死亡:当服务器停止,容器(服务器)会调用destroy方法销毁servlet
第一种:实现servlet接口
步骤
1写一个类实现了servlet接口,重写service方法
2在web.xml中配置该servlet
package com.zy.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 MyServlet implements Servlet{ //普通的类 //要想让该类具备接收请求,完成响应的功能 @Override public void destroy() {//销毁---servlet生命结束 // TODO Auto-generated method stub } @Override public ServletConfig getServletConfig() { // TODO Auto-generated method stub return null; } @Override public String getServletInfo() { // TODO Auto-generated method stub return null; } @Override public void init(ServletConfig arg0) throws ServletException {//出生---初始化 System.out.println("servlet出生了"); } @Override //请求 //响应 public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { // ---最重要的(接收请求,完成响应) //接收请求 System.out.println("收到请求,进入servlet的service方法"); //响应 response.getWriter().write("hello world"); //生命-- //出生--服务--死亡 } }
<!-- 配置servlet --> <servlet> <servlet-name>qwer</servlet-name> <servlet-class>com.zy.servlet.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>qwer</servlet-name><!-- 与上边匹配 --> <url-pattern>/abc</url-pattern><!--url 请求规则 --> </servlet-mapping>
第二种:继承HttpServlet
package com.zy.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.sun.net.httpserver.HttpServer; public class MyServlet2 extends HttpServlet{ //重写doGet和doPost[] @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //写代码!!!!!!!!!!!!!!!! System.out.println("收到请求,进入了doGet方法"); resp.getWriter().write("hello zhiyou"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //doPost一般直接调用doGet doGet(req, resp); } }
<servlet> <servlet-name>abc</servlet-name> <servlet-class>com.zy.servlet.MyServlet2</servlet-class> </servlet> <servlet-mapping> <servlet-name>abc</servlet-name> <url-pattern>/bcd</url-pattern> </servlet-mapping>
第三种:注解版
package com.zy.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class MyServlet3 */ //基于最新注解版servlet(不用配置web.xml,用注解代替了这段配置) @WebServlet("/MyServlet3")//[这就是url-pattern] public class MyServlet3 extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("接收请求,进入doGet方法"); response.getWriter().write("hello ruhua"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
5、url-pattern
三种模式:
完全匹配: /abc
目录匹配: /aaa/bbb/*
后缀匹配:*.action
package com.zy.servlet2; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ParameterServlet */ @WebServlet("/ParameterServlet") public class ParameterServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //如果请求中含有参数,我们怎么去接收到servlet中 //url 传参方式(?好传参,参数以键值对出现) //localhost:8080/web01/ParameterServlet?name=xiaoming //中文参数 //localhost:8080/web01/ParameterServlet?name=老王 //n个参数 //localhost:8080/web01/ParameterServlet?name=老王&age=10&address=湖南 //怎么接收参数 //java面向对象的思想:xxx对象处理xxx事情 //分析事情:接收请求中的参数问题 //调用request的api String parameter = request.getParameter("name");//根据键取值 String parameter2 = request.getParameter("age"); String parameter3 = request.getParameter("address"); System.out.println(parameter); System.out.println(parameter2); System.out.println(parameter3); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }