Servlet基础知识点整理

常用注解

官方文档:https://docs.oracle.com/javaee/7/api/toc.htm

WebServlet

@WebServlet用于定义一个Servlet,等价于下面的xml配置

<servlet>
    <servlet-name>LogServlet</servlet-name>
    <servlet-class>log.LogServlet</servlet-class>
    <init-param>
    <param-name>email</param-name>
    <param-value>965019948@qq.com</param-value>
    </init-param>
    <init-param>
    <param-name>zipcode</param-name>
    <param-value>225800</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>LogServlet</servlet-name>
    <url-pattern>/log</url-pattern>
</servlet-mapping>

WebFilter

@WebFilter用于定义一个Filter,等价于下面的xml配置

<filter>
    <filter-name>LoggingFilter</filter-name>
    <filter-class>filter.LoggingFilter</filter-class>
    <async-supported>true</async-supported>
    <init-param>
        <param-name>logFileName</param-name>
        <param-value>log.txt</param-value>
    </init-param>
    <init-param>
        <param-name>prefix</param-name>
        <param-value>URI:</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>LoggingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

WebListener

@WebListener用于定义一个Listener,等价于下面的xml配置

<listener>
    <listener-class>listener.SessionListener</listener-class>
</listener>

WebInitParam

指定Servlet或Filter的初始化参数

ServletConfig和ServletContext的区别

  • ServletConfig:

    • Servlet容器初始化Servlet时,Servlet容器会给该Servlet传入一个ServletConfig对象

      void init (ServletConfig var1) throws ServletException

    • ==该Servlet的配置==

  • ServletContext:

    • ==ServletContext表示Servlet应用程序==,每个Web应用程序只有一个上下文

Attribute

相关方法

  • void setAttribute(String name, Object o)
  • Object getAttribute(String name)
  • Enumeration getAttributeNames()
  • removeAttribute(String name)

范围

pageContext < request < session < application

  • pageContext:当前JSP页面
  • request:当前请求
  • session:一次会话
  • application:当前web应用

JSP脚本片断

<%
    for (Customer customer : customers){
    /*此处“{ }”断开,使得JSP页面能将customers遍历并以表格的方式展示*/
%>
    <tr>
        <td><%= customer.getId()%></td>
        <td><%= customer.getName()%></td>
        <td><%= customer.getAddress()%></td>
        <td><%= customer.getPhone()%></td>
        <td>
            <a href="delete.do?id=<%= customer.getId()%>" class="delete">Delete</a>
            <a href="">Update</a>
        </td>
    </tr>
<%
    }
%>

对应的Java源代码如下

for (Customer customer : customers){
            
      out.write("\n");
      out.write("            <tr>\n");
      out.write("                <td>");
      out.print( customer.getId());
      out.write("</td>\n");
      out.write("                <td>");
      out.print( customer.getName());
      out.write("</td>\n");
      out.write("                <td>");
      out.print( customer.getAddress());
      out.write("</td>\n");
      out.write("                <td>");
      out.print( customer.getPhone());
      out.write("</td>\n");
      out.write("                <td>\n");
      out.write("                    <a href=\"delete.do?id=");
      out.print( customer.getId());
      out.write("\" class=\"delete\">Delete</a>\n");
      out.write("                    <a href=\"\">Update</a>\n");
      out.write("                </td>\n");
      out.write("            </tr>\n");
      out.write("            ");

}

请求转发和重定向

转发

request.getRequestDispatcher("/index.jsp").forward(request,response);

1512851-20190408180316380-1685481195.png

重定向

response.sendRedirect("query.do");

1512851-20190408180246265-314119928.png

302 Move temporarily

​ 请求的资源临时从不同的 URI响应请求。由于这样的重定向是临时的,客户端应当继续向原有地址发送以后的请求。只有在Cache-Control或Expires中进行了指定的情况下,这个响应才是可缓存的。

​ 上文有提及。

​ 如果这不是一个 GET 或者 HEAD 请求,那么浏览器禁止自动进行重定向,除非得到用户的确认,因为请求的条件可能因此发生变化。

​ 注意:虽然RFC 1945和RFC 2068规范不允许客户端在重定向时改变请求的方法,但是很多现存的浏览器将302响应视作为303响应,并且使用 GET 方式访问在 Location 中规定的 URI,而无视原先请求的方法。状态码303和307被添加了进来,用以明确服务器期待客户端进行何种反应。

https://baike.baidu.com/item/HTTP%E7%8A%B6%E6%80%81%E7%A0%81/5053660?fr=aladdin#3_3

区别

转发重定向
地址栏是第一次发出请求的地址地址栏是最后响应的地址
在Servlet中request对象没有变化在Servlet中request对象发生了变化
只能转发给当前web应用的资源可以重定向到任意地方(比如百度)

转载于:https://www.cnblogs.com/merryituxz/p/10120509.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值