sxt_(008_011)_servlet
一、servlet简介
Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,具有独立于平台和协议的特性,主要功能在于交互式地浏览和生成数据,生成动态Web内容。
二、编写servlet(我的第一个servlet)
2.1实现Servlet接口
2.1.1新建一个web项目
2.1.2编写servlet
package edu.aeon.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; /** * [说明]:我的第一个servlet * @author qq:1584875179 * */ public class HelloServlet implements Servlet { @Override public void destroy() { } @Override public void init(ServletConfig arg0) throws ServletException { } /** * 处理用户请求 */ @Override public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { //设置浏览器端显示的编码方式 response.setContentType("text/html;charset=utf-8"); PrintWriter printWriter=response.getWriter(); printWriter.println("<html>"); printWriter.println("<head>"); printWriter.println("<title>"); printWriter.println("我的第一个servlet"); printWriter.println("</title>"); printWriter.println("</head>"); printWriter.println("<body>"); printWriter.println("<h1>我的第一个servlet:hello servlet!</h1>"); printWriter.println("</body>"); printWriter.println("</html>"); } @Override public ServletConfig getServletConfig() { return null; } @Override public String getServletInfo() { return null; } }
2.1.3在web.xml中配置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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>helloservlet</display-name> <!-- servlet配置[start] --> <servlet> <servlet-name>helloServlet</servlet-name> <servlet-class>edu.aeon.servlet.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>helloServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <!-- servlet配置[/end] --> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
2.1.4访问servlet
通过url:http://localhost:8080/helloservlet/hello 访问、其访问结果如下:
2.2继承自GenericServlet类
2.3继承自HttpSerlvet类
三、模拟一个服务端展示集合数据的servlet
3.1编写实体类
package edu.aeon.entity; /** * [说明]:用户实体类 * @author qq:1584875179 * */ public class User { private String userName; private String userSex; private Integer userAge; private String userPhone; public User() { } public User(String userName, String userSex, Integer userAge, String userPhone) { super(); this.userName = userName; this.userSex = userSex; this.userAge = userAge; this.userPhone = userPhone; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserSex() { return userSex; } public void setUserSex(String userSex) { this.userSex = userSex; } public Integer getUserAge() { return userAge; } public void setUserAge(Integer userAge) { this.userAge = userAge; } public String getUserPhone() { return userPhone; } public void setUserPhone(String userPhone) { this.userPhone = userPhone; } }
3.2编写servlet类(还是通过实现Servlet接口的方式)
package edu.aeon.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import edu.aeon.entity.User; /** * [说明]:模拟服务器端数据展示的servlet * @author qq:1584875179 * */ public class DataServlet implements Servlet { @Override public void destroy() { } @Override public void init(ServletConfig arg0) throws ServletException { } /** * 处理用户请求 */ @Override public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { //准备数据(也可认为这些数据是来自数据库) List<User> userList=new ArrayList<User>(); userList.add(new User("aeon1", "男", 21, "1584875171")); userList.add(new User("aeon2", "女", 22, "1584875172")); userList.add(new User("aeon3", "男", 23, "1584875173")); userList.add(new User("aeon4", "女", 24, "1584875174")); userList.add(new User("aeon5", "男", 25, "1584875175")); //设置浏览器端显示的编码方式 response.setContentType("text/html;charset=utf-8"); PrintWriter printWriter=response.getWriter(); printWriter.println("<html>"); printWriter.println("<head>"); printWriter.println("<title>"); printWriter.println("模拟服务器端数据展示"); printWriter.println("</title>"); printWriter.println("</head>"); printWriter.println("<body>"); printWriter.println("<table border='1' align='center' width='60%'>"); printWriter.println("<tr>"); printWriter.println("<th>"); printWriter.println("用户名"); printWriter.println("</th>"); printWriter.println("<th>"); printWriter.println("用户性别"); printWriter.println("</th>"); printWriter.println("<th>"); printWriter.println("用户年龄"); printWriter.println("</th>"); printWriter.println("<th>"); printWriter.println("电话号码"); printWriter.println("</th>"); printWriter.println("</tr>"); for(int i=0;i<userList.size();i++){ printWriter.println("<tr>"); printWriter.println("<td>"); printWriter.println(userList.get(i).getUserName()); printWriter.println("</td>"); printWriter.println("<td>"); printWriter.println(userList.get(i).getUserSex()); printWriter.println("</td>"); printWriter.println("<td>"); printWriter.println(userList.get(i).getUserAge()); printWriter.println("</td>"); printWriter.println("<td>"); printWriter.println(userList.get(i).getUserPhone()); printWriter.println("</td>"); printWriter.println("</tr>"); } printWriter.println("</table>"); printWriter.println("</body>"); printWriter.println("</html>"); } @Override public ServletConfig getServletConfig() { return null; } @Override public String getServletInfo() { return null; } }
3.3在web.xml文件中配置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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>data</display-name> <servlet> <servlet-name>dataservlet</servlet-name> <servlet-class>edu.aeon.servlet.DataServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>dataservlet</servlet-name> <url-pattern>/dataServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
3.4访问
http://localhost:8080/data/dataServlet、其访问结果如下:
四、servlet处理流程简图
用户发起一个请求后,由服务器接受处理,根据web.xml文件中的配置信息,查找所有请求的资源是否存在,如果不存在则返回处理结果(404)。当找到资源后(相应的servlet),检查该servlet对象是否存在,如果不存在则创建该对象,如果存在,则执行相应的处理方法。处理方法执行以后将返回处理结果给web服务器,web服务器根据结果进行相关处理后,返回给浏览器。浏览器显示相应的处理结果。
五、servlet生命周期
六、servlet类的实例化时机
6.1配置了容器启动时调用(容器启动时就会实例化);
<load-on-startup>0</load-on-startup> //配置一个大于等于0的数,该数字越小启动时调用的优先级最高。
6.2当请求时,该servlet不存在时会再次进行实例化。
七、HttpServletResponse对象
7.1 HttpServletResponse对象的产生:
当服务器接受到请求后会产生两个对象:请求对象,响应对象,并且将这两个对象给Servlet.
7.2 响应数据:
getOutputStream():字节流数据
getWriter():字符流数据
getOutputStream()和getWriter互斥、不共戴天
原因:当调用getOutputStream或getWriter后,服务器接受后会将数据进行处理。处理完成后会检查流对象是否关闭,如果没有关闭,服务器会自动关闭。
setContentType("text/html;charset=utf-8"):指定响应的客户端内容的类型及编码方式,如果不指定则默认为iso-8859-1