编程语言
首页 > 编程语言> > JAVA WEB2——Servlet

JAVA WEB2——Servlet

作者:互联网

JAVA WEB2——Servlet

Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,是Java语言编写 的服务器端程序,换句话说,Servlet就是运行在服务器上的Java类。 Servlet用来完成B/S架构下客户端请求的响应处理,也就是交互式地浏览和生成数据,生成动态Web内容

1、第一个servlet程序(servlet)

1.1、实现Servlet

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>

1.2、继承GenericServlet

public class HelloServlet extends GenericServlet {

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("servlet被访问了");
    }
}

1.3、继承HttpServlet

public class HelloServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("servlet被访问了");
    }
}

1.4、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");
    }
}

2、Servlet的生命周期

创建(init): 用户第一次访问servlet创建servlet的实例.

销毁(destroy): 当项目从服务器移除,或服务器关闭的时候

3、请求参数的获取

方法声明功能介绍
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的路径部分

4、响应消息

@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("我已收到请求");
    }
}

5、请求乱码处理

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");
    }
}

6、ServletConfig、ServletContext

<?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() {

    }
}

7、重定向与请求转发

7.1、重定向

首先客户浏览器发送http请求,当web服务器接受后发送302状态码响应及对应新的location给客 户浏览器,客户浏览器发现是302响应,则自动再发送一个新的http请求,请求url是新的location 地址,服务器根据此请求寻找资源并发送给客户

启动向我借钱,我没有,我让启动去找洲洲借钱

@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");
    }
}

7.2、请求转发

一个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");
    }
}

7.3、重定向与转发的区别

在这里插入图片描述

8、Servlet多线程访问问题

9、Cookie

9.1、cookie的使用

@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);
    }
}

9.2、cookie的生命周期

默认情况下,浏览器会将Cookie信息保存在内存中,只要浏览器关闭,Cookie信息就会消失,可以设置cookie存活时间(设为-1立即失效)

//最大存活时间,单位为秒
cookie.setMaxAge(60*60*24);

9.3、cookie路径问题

9.4、cookie的特点

10、Session

10.1、session的使用

@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);
    }
}

10.2、session的生命周期

方法声明功能介绍
int getMaxInactiveInterval()获取失效时间
void setMaxInactiveInterval(int interval)设置失效时间

可以配置web.xml文件修改失效时间(单位分钟)

<session-config>
    <session-timeout>60</session-timeout>
</session-config>

10.3、session的特点

标签:JAVA,void,request,System,WEB2,println,Servlet,response,out
来源: https://blog.csdn.net/gyfghh/article/details/118889061