Servlet(Server Applet),全称Java Servlet,未有中文译文。是用Java编写的服务器端程序。其主要功能在于交互式地浏览和修改数据,生成动态Web内容。狭义的Servlet是指Java语言实现的一个接口,广义的Servlet是指任何实现了这个Servlet接口的类,一般情况下,人们将Servlet理解为后者。
Servlet运行于支持Java的应用服务器中。从原理上讲,Servlet可以响应任何类型的请求,但绝大多数情况下Servlet只用来扩展基于HTTP协议的Web服务器。
一、访问servlet
1、创建一个Dynamic Web Project项目,记得后面要选上web.xml
2、新建一个SevletHelloWord 类,继承 HttpServlet
代码:
package servletTest1; 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; public class SevletHelloWord extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub // super.doGet(req, resp); System.out.print("get"); doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub // super.doPost(req, resp); resp.setCharacterEncoding("gbk");//防止编码乱码 PrintWriter out=resp.getWriter();//PrintWriter文本输出 out.println("<html>"); out.println("<head><title>helloword</title></head>"); out.println("Servlet hello word!"); out.println("</html>"); out.close();//关闭 } }
3、修改web.xml(在webconten目录里面),用Text 格式打开,不要用默认的xml格式
新增servlet访问的选项xml代码
<servlet> <servlet-name>servletHelloWord</servlet-name> <servlet-class>servletTest1.SevletHelloWord</servlet-class> </servlet> <servlet-mapping> <servlet-name>servletHelloWord</servlet-name> <url-pattern>/helloWorld</url-pattern> </servlet-mapping>
4、启动tomcat 的Servers服务器,记得要添加该项目
5、开启成功后,在浏览器输入:http://localhost:8080/servletTest1/helloWorld,出现打印 说明成功了
说明:servlet启动顺序,输入网址:http://localhost:8080/servletTest1/helloWorld,如图
二、servlet的生命周期
简单四步:servlet类加载-》实例化-》服务-》销毁
下面只简单说明,
public class LifeServlet extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("service"); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("service"); } @Override public void destroy() { System.out.println("servlet销毁"); } @Override public void init() throws ServletException { System.out.println("servlet初始化"); } }
执行后输出:
其中初始化只会出现第一次,
destroy方法在容器移除servlet 时执行,同样只执行一次。这个方法会在所有的线程的service()方法执行完成或者超时后执行,调用这个方法后,容器不会再调用这个servlet的方法,也就是说容器不再把请求发送给这个servlet。这个方法给servlet释放占用的资源的机会,通常用来执行一些清理任务。
三、客户端跳转跟服务器端跳转
1、在Servlet中获取session,application
2、客户端跳转 :ersponse.sendRedirect("目标地址");
3、服务器跳转:RequestDispatcher rd=request.getRequestDispatcher("目标地址");
详细方法,新建一个target.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>目标地址</h1> request值:<%=request.getAttribute("requestKey") %><br/> session值:<%=session.getAttribute("sessionKey") %><br/> application值:<%=application.getAttribute("applicationKey") %><br/> </body> </html>
客户端跳转代码:
public class RedirectServlet extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setAttribute("requestKey", "request值"); HttpSession session=request.getSession(); // 获取session session.setAttribute("sessionKey", "session值"); ServletContext application=this.getServletContext(); // 获取application application.setAttribute("applicationKey", "application值"); response.sendRedirect("target.jsp"); // 客户端跳转/重定向 }
输出:
服务端跳转代码:
public class ForwardServlet extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setAttribute("requestKey", "request值"); HttpSession session=request.getSession(); // 获取session session.setAttribute("sessionKey", "session值"); ServletContext application=this.getServletContext(); // 获取application application.setAttribute("applicationKey", "application值"); RequestDispatcher rd=request.getRequestDispatcher("target.jsp"); rd.forward(request, response); // 服务器调转/转发 } }
输出:
区别:服务端就跳转 能把request、session、application的值都传过来,
但是客户端不能把erquest的值传过来,一般用服务端跳转较多
主要参考java1234的servlet教程