美文网首页
Javaweb _2_ Servlet

Javaweb _2_ Servlet

作者: 老北瓜 | 来源:发表于2020-05-12 10:46 被阅读0次
  • 啥是Servlet
  • Servlet的生命周期
  • HttpServletRequest(请求)与HttpServletResponse(响应)
  • ServletContext 与 ServletConfig
  • 重定向与转发

Servlet简介(Servlet Applet)

   是Java Servlet的简称,成为小服务程序或服务连接器。用java编写的服务器端程序,主要功能在于交互式的浏览和修改数据,生成动态web内容。

第一个Servlet程序

image.png
LoginServlet.java
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("调用doGet方法");
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("调用doPost方法");
    }
}

需要在 web.xml文件中配置虚拟定位

    <!--  LoginServlet 虚拟定位-->
    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>servlets.LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/LoginServlet</url-pattern>
    </servlet-mapping>

或者可以直接右键 new Servlet, IDE会自动生成一个带有@WebServlet("/xxxServlet",urlPatterns = "/xxxServlet")的注解,等效于 web.xml中的配置。 这里需要自己添加urlPatterns="/xxxServlet"

jsp文件是为了展示页面, servlet类是为了编写逻辑,所以配合使用。

新建 login.jsp 默认项目启动的jsp页面,可以再web.xml中配置

 <welcome-file-list>
        <welcome-file>firstservlet/login.jsp</welcome-file>
    </welcome-file-list>
<div>
    <form action="LoginServlet" method="post">
        <p> 用户名: <input type="text" name="username"></p>
        <p>密&nbsp;&nbsp;码: <input type="passowrd" name="password"></p>
        <p>
            <input type="submit" value="登录">
            <input type="reset" value="重置">
        </p>
    </form>
</div>

这里通过 post的方法提交表单到 LoginServlet, 控制台输出调用doPost方法。

Servlet路径问题
    需要设置动态Servlet地址。

Servlet生命周期

  • 初始化阶段 init()   仅在第一次被访问时调用
  • 响应客户请求阶段调用 service() 方法
  • 终止阶段调用 destroy() 方法 (把项目从tomcat中移除)

请求与响应

客户端向服务端的一次访问称为一次请求,使用HttpServletRequest对象来表示
服务器给客户端的一次反馈称呼一个响应,使用HttpServletResponse对象来表示

ServletContext与ServletConfig

  • 整个JavaWeb工程可以用一个对象来表示,就是ServletContext
  • 我们可以在web.xml文件中给某一个Servlet配置一些配置信息,当服务器被启动的时候,配置信息会被封装到一个ServletConfig对象中去,所以ServletConfig表示的是一个Servlet的配置文件
    ServletContext
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        ServletContext context = this.getServletContext();
        context.setAttribute("username", "张三");
        
        Object username = context.getAttribute("username");
        System.out.println(username);
    }

如果在其他文件中,依然可以

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //  ServletContext 代表了 整个javaweb
        ServletContext context = this.getServletContext();
        Object username = context.getAttribute("username");
        System.out.println(username);
    }

ServletConfig
先在web.xml中添加 配置

<servlet>
        <servlet-name>ServletConfigDemo</servlet-name>
        <servlet-class>servlets.ServletConfigDemo</servlet-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>ServletConfigDemo</servlet-name>
        <url-pattern>/ServletConfigDemo</url-pattern>
    </servlet-mapping>

在 java文件中可以取到配置的信息

 protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
      
        ServletConfig config = this.getServletConfig();
        String encoding = config.getInitParameter("encoding");
        System.out.println("在web.xml中给ServletConfig配置的 encoding = " + encoding);
    }

重定向与转发

javaweb _ jsp 中有提到过
区别:
    重定向不能使用request带数据到跳转的页面,转发可以.

那么重定向如何向跳转页面携带数据, 可以使用 application
ServletContext.setAttrbute("username","内马尔");
jsp中
application.getAttribute("username");

Servlet.java

@WebServlet(name = "LoginServletDemo", urlPatterns = "/LoginServletDemo")
public class LoginServletDemo extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*
            转发  服务端行为
         */
        request.setAttribute("username", "admin");
        request.getRequestDispatcher("firstservlet/login_result.jsp").forward(request, response);
        
        /**
         *  重定向 ,使用 ServletContext 携带数据
         */
        ServletContext context = getServletContext();
        context.setAttribute("goods", "瓜");
        response.sendRedirect("firstservlet/login_result.jsp");
        
    }
}

login_result.jsp

// 对应重定向的处理方法
request.getAttribute("username").toString();

// 对应转发的处理方法
String goods = application.getAttribute("goods").toString();

相关文章

网友评论

      本文标题:Javaweb _2_ Servlet

      本文链接:https://www.haomeiwen.com/subject/lxmunhtx.html