Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,是Java语言编写 的服务器端程序,换句话说,Servlet就是运行在服务器上的Java类。 Servlet用来完成B/S架构下客户端请求的响应处理,也就是交互式地浏览和生成数据,生成动态Web内容
public class HelloServlet implements Servlet { @Override public void init(ServletConfig servletConfig) throws ServletException { System.out.println("servlet初始化"); } @Override public ServletConfig getServletConfig() { return null; } @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { System.out.println("servlet被访问了"); } @Override public String getServletInfo() { return null; } @Override public void destroy() { System.out.println("servlet销毁"); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>hello</servlet-name> <servlet-class>HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>
public class HelloServlet extends GenericServlet { @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { System.out.println("servlet被访问了"); } }
public class HelloServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("servlet被访问了"); } }
@WebServlet("/RuoyrServlet") public class RuoyrServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("post"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("get"); } }
创建(init): 用户第一次访问servlet创建servlet的实例.
销毁(destroy): 当项目从服务器移除,或服务器关闭的时候
方法声明 | 功能介绍 |
---|---|
String getParameter(String name) | 以字符串形式返回请求参数的值,如果该参数不存在,则返回空值 |
String[] getParameterValues( String name) | 返回一个字符串对象数组,其中包含给定请求参数所具有的所有 值,如果该参数不存在,则返回空值 |
Enumeration getParameterNames() | 返回包含此请求中包含的参数名称的字符串对象的枚举。如果请 求没有参数,则方法返回空枚举 |
Map getParameterMap() | 返回请求参数的键值对,一个键可以对应多个值 |
String getRemoteAddr() | 返回发送请求的客户端或最后一个代理的IP地址 |
int getRemotePort() | 返回发送请求的客户端或最后一个代理的端口号 |
String getRequestURI() | 返回此请求的资源路径信息 |
StringBuffer getRequestURL() | 返回此请求的完整路径信息 |
String getMethod() | 返回发出此请求的HTTP方法的名称,例如GET、POST |
String getQueryString() | 返回路径后面请求中附带的参数 |
String getServletPath() | 返回此请求中调用servlet的路径部分 |
@WebServlet("/RuoyrServlet") public class RuoyrServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("post"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("get"); response.setCharacterEncoding("UTF8"); response.setContentType("text/html;charset=utf-8"); PrintWriter writer = response.getWriter(); writer.write("我已收到请求"); } }
public class RuoyrServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("post"); System.out.println(request.getParameter("name")); System.out.println(new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF8")); response.setCharacterEncoding("UTF8"); response.setContentType("text/html;charset=utf-8"); PrintWriter writer = response.getWriter(); writer.write("我已收到请求"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("get"); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>hello</servlet-name> <servlet-class>HelloServlet</servlet-class> <init-param> <param-name>usernaame</param-name> <param-value>admin</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <context-param> <param-name>username</param-name> <param-value>ruoye</param-value> </context-param> </web-app>
public class HelloServlet implements Servlet { @Override public void init(ServletConfig servletConfig) throws ServletException { System.out.println(servletConfig.getServletName()); System.out.println(servletConfig.getInitParameter("username")); ServletContext servletContext = servletConfig.getServletContext(); Enumeration<String> attributeNames = servletContext.getAttributeNames(); while (attributeNames.hasMoreElements()){ System.out.println(attributeNames.nextElement()); } } @Override public ServletConfig getServletConfig() { return null; } @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { } @Override public String getServletInfo() { return null; } @Override public void destroy() { } }
首先客户浏览器发送http请求,当web服务器接受后发送302状态码响应及对应新的location给客 户浏览器,客户浏览器发现是302响应,则自动再发送一个新的http请求,请求url是新的location 地址,服务器根据此请求寻找资源并发送给客户
启动向我借钱,我没有,我让启动去找洲洲借钱
重定向之后,浏览器地址栏的URL会发生改变
重定向过程中会将前面Request对象销毁,然后创建一个新的Request对象
重定向的URL可以是其它项目工程
@WebServlet("/RuoyrServlet") public class RuoyrServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("post"); response.sendRedirect(request.getContextPath()+"/WinkToServlet"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("get"); this.doPost(request,response); } }
@WebServlet("/WinkToServlet") public class WinkToServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("我是重定向post"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("我是重定向get"); } }
一个Web组件(Servlet/JSP)将未完成的处理通过容器转交给另外一个Web组件继续处理,转发 的各个组件会共享Request和Response对象
启动向我借钱,我没有,我去找洲洲借钱给启动
@WebServlet("/RuoyrServlet") public class RuoyrServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("post"); RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WinkToServlet"); requestDispatcher.forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("get"); this.doPost(request,response); } }
@WebServlet("/WinkToServlet") public class WinkToServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("我是请求转发post"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("我是请求转发get"); } }
@WebServlet("/RuoyrServlet") public class RuoyrServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) { //从请求中获取cookie Cookie[] cookies = request.getCookies(); for (Cookie cookie : cookies) { System.out.println(cookie.getName()+"==="+cookie.getValue()); } System.out.println("post"); //创建cookie Cookie cookie = new Cookie("name", "zhangsan"); //最大存活时间,单位为秒 cookie.setMaxAge(60*60*24); //让响应携带cookie response.addCookie(cookie); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("get"); this.doPost(request,response); } }
默认情况下,浏览器会将Cookie信息保存在内存中,只要浏览器关闭,Cookie信息就会消失,可以设置cookie存活时间(设为-1立即失效)
//最大存活时间,单位为秒 cookie.setMaxAge(60*60*24);
@WebServlet("/RuoyrServlet") public class RuoyrServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); System.out.println(session.isNew()); System.out.println(session.getId()); session.setAttribute("name","zhangsan"); //创建cookie Cookie cookie = new Cookie("JSESSIONID", session.getId()); //最大存活时间,单位为秒 cookie.setMaxAge(60*60*24); //让响应携带cookie response.addCookie(cookie); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("get"); this.doPost(request,response); } }
方法声明 | 功能介绍 |
---|---|
int getMaxInactiveInterval() | 获取失效时间 |
void setMaxInactiveInterval(int interval) | 设置失效时间 |
可以配置web.xml文件修改失效时间(单位分钟)
<session-config> <session-timeout>60</session-timeout> </session-config>