Servlet study notes (which I remember when learning Servlet notes, only Bo Friends of reference, there is something wrong place to interact ha) ...

Original link: http://www.cnblogs.com/wat1314/p/11008822.html

A, Servlet concepts and features:


    1, concepts:
    the next refers to the Servlet interface to a Java language implementation of the Servlet broadly refers to
    any class that implements the Servlet interface, under normal circumstances, it will be understood as the latter.
    Servlet running on Java-enabled application servers. In principle, Servlet can respond to
    any type of request, but in most cases only to extend the Servlet based on the HTTP protocol
    Web server.

    2, features:
   (1) runs in a Java-enabled application servers.
   Implement (2) follows the rule server Servlet recognizable, i.e. from the server
       moving according to a request corresponding to the Servlet invocation request processed.
           (3) simple and convenient portability.

Two, Servlet running process:


          The browser sends a request to the server, the server based on the request URI URL address
    information to find the project folder in the corresponding directory, and then retrieve the corresponding in web.xml in
    the servlet, after finding and perform Servlet.

Three, Servlet use process:


  1. Create a plain java class and inherit HttpServlet.
  2, a method override service.
  3, code can be written in the service logic process.
    (1) setting request encoding format
      req.setCharacterEncoding ( "UTF-. 8");
    (2) set the response encoding format
      resp.setContentType ( "text / HTML; charset = UTF-. 8");
    (. 3) a request for information
      req. the getParameter ( "the uname");
      req.getParameter ( "pwd");
    (. 4) processing request information
    (5) in response to the processing result
        // direct response
          // request forwarding
        // redirect
  4, wEB-INF file in the web web.xml folder configuration servlet
     or directly by the program to configure the annotation servlet (Note: two can only choose one
     can not exist at the same time, whether the server-side error)

Four, Servlet life cycle:


  1, from the first call to the server is shut down.
  2, if Servlet configured load-on-startup in web.xml,
     life cycle starts off from the server to the server. (Eclipse)


  注意:
      (1)nit方法是对Servlet进行初始化的一个方法,会在
       Servlet第一次加载进行存储时进行。
   (2)destory方法是在Servlet被销毁时执行,也就是服
       务器关闭时。

五、Servlet常见的错误总结:


  1、404错误:资源未找到
    原因1:在请求地址中Servlet的别名书写错误。
    原因2:项目的名称拼写错误。
  2、500错误:内部服务器错误
    错误一:Java.lang.ClassNotFoundException: .............。
    解决:在web.xml中校验Servlet类的全限定路径是否拼写错误。

    错误二:因为service方法的代码执行导致错误。
    解决:根据错误提示对service方法体中的代码进行错误更改。
  3、405错误:请求方法不支持
    原因:请求方式和Servlet中的方法不匹配所导致的。
    解决:尽量使用service方法进行请求处理,并且不要在service方法
               里面直接调用父类的service方法。

学习资源:http://www.bjsxt.com/

转载于:https://www.cnblogs.com/wat1314/p/11008822.html

Guess you like

Origin blog.csdn.net/weixin_30642561/article/details/95268407