servlet的一些细节部分

    技术2022-05-19  21

    web服务器:IIS  (微软)

              Apache      (静态页面效率高)

              Tomcat(java)

     

              websphere(IBM)

     

    web服务器(apache 装组建)、应用服务器(IIS)、容器(Tomcat)

     

    Tomcat安装:apachetomcat 

                  src.zip源文件  需要编译之后才能使用

           目录:

                  binstartup.bat开启批处理文件

                         shutdown.bat

                  conf:配置文件,所有的web应用程序的配置

                         server.xml(端口配置80->8080)

                                标签:

                                <server>容器

                                       <service>中间件

                                              <connector><>

                                              <engine>

                                                     <host><>

                                              </engine>      

                                       </service>

                                </server>

                         context.xml

                                <context reloadable="true"></context>

                                监控classes文件夹下的class文件,得到及时更新

                         web.xml

                  lib:jar

                  log:日志文件

                  temp:临时文件夹

                  webappsdocs  examples  manager  ROOT

                         部署web应用程序

                  work:缓存记录

     

     

    HTTP协议:

           超文本传输协议  明文发送方式发送数据  不安全--https

           http协议以TCP/IP为基础   现在版本http/1.1

           http1.0  http1.1区别:http1.1持久链接

          

           访问页面返回:100-199  200-299 。。。

           httplook:工具

     

     

    servletserver  letter 服务器小应用程序

           servlet  APIservlet提供统一接口

     

    WEB Applicaton

           webapps下建立根root   在输入http://127.0.0.1转到根目录下

           根下建立WEB-INF 

                  web.xml   lib(jar)   classes(字节码文件)

           根下建立静态页面index.html  用来测试http://是否成功

     

     

    javax.servlet

           servlet接口

                  实现servlet接口的类:

                         class GenericServlet  实现servlet所有方法

                         abstract class HttpServlet  继承GenericServlet   实现了seriazible接口  (javax.servlet.http包下)

          

     

    导包:property ->javax Build ->Libraries ->Add External JARs->apache tomcat ->lib ->servlet api

     

    HttpServlet类方法:8do方法

           继承HttpServlet  重写do方法即可

                  右键->source->override

     

           public void doPost(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{}

     

           HttpServletRequest是接口  tomcat在调用doPost方法时,tomcat传进实现了HttpServletRequest接口的具体类(请求)   HttpServletResponse到客户端的应答

          

    手动部署:字节码复制到tomcatwebapps下所建立工程下WEB-INF下的classes文件下     在什么时候要tomcat第调用该class:在tomcatwebapps下所建工程下WEB-INF下的web.xml 修改:

                  <servlet>

                         <servlet-name>myname</servlet-name>

                         <servlet-class>com.helloworldservlet</servlet-class>

                  </servlet>

                  <servlet-mapping>

                         <servlet-name>myname</servlet-name>

                         <url-pattern>/tigger</url-pattern> 用户触发

                  </servltmapping>

     

     

    Interface Servlet:没有考虑到httpt协议具体细节

           继承的抽象类GenericServletHttpServlet

                  GenericServlet抽象类:方法service(ServletRequest req,ServletResponse res) 不知道怎么实现  不知道具体是那种协议

                  HttpServlet抽象类:8do方法和两种service()

                         1.service(ServletRequest req,ServletResponse res)

                         2.service(HttpServletRequest req,HttpServletResponse res)

                         1方法分发到2方法,2方法分发到do方法  而系统调用1方法

     

    {

           客户请求显示:req.setCharacterEncoding("utf-8");

           服务器响应以html类型和编码:resp.setContentType("text/html;charset=utf-8")

           输出Brouse  输出流 PrintWrite out=resp.getWrite()

    }

     

    Servlet生命周期   生命周期全过程(面试)

           ClassLoader加载

           new 实例化  只有一个对象  new一次

           init()初始化  init一次

           service()处理请求  多线程方式

           destory()退出服务  加载新的类的时候  销毁前面的类 什么周期结束

    服务器端:线程池并发处理客户请求

           只有一个对象(****重点)

     

    GenericServlet抽象类中两个init()方法

           init()

           init(ServletConfig config)

           ServletConfig接口  getServletConfig().getServletName()

     

     

    cookie:

           服务器可以向客户端写内容  只能是文本  最长4K   客户端可以阻止服务器写入  

           cookie分两种:1.属于窗口  没有设置生命周期的  存活在浏览器  放在内存中的

                        2.属于文本  有生命周期    .setMaxAge(s)

           缺点:不稳定  用户可以禁用

     

    session:

           1.是服务器的一块内存(key-value)

           2.和客户端窗口对应(子窗口)(独一无二)

           3.客户端和服务器端有对应的SessionID

           4.客户端向服务器端发送SessionID有两种方式:cookie(浏览器内存cookie)URL重写

           5.浏览器禁掉cookie,就不能用session

           6.session也有生命周期  conf文件夹下的web.xml修改

           7.如果想安全的使用session(不论客户端是否禁止cookie),只能使用URL重写,大大增加了编程负担

     

    乱码:

           1.页面本身有中文的时候(即:服务器向浏览器输出内容时)

                  resp.setContentType("text/html,charset=utf-8")

                  一定写在PrintWrite out = resp.getWrite()之前

           2.解决get方式乱码问题

                  修改servlet.xml -> URIEncoding=utf-8

           3.解决post方式提交乱码问题

                  req.setCharacterEncoding("utf-8")

     

    servlet与数据库的互连:注意的地方

           把数据库的jdbc拷贝到tomcat下的bin目录下

     

     

    javabean:

           servlet利用javabeangetset方法  封装好

     

    import java.sql.*;

    public class DB{

           public static Connection getConn(){

                  Connection conn = null;

                  try{

                         Class.forName("com.microsoft.sql.jdbc.SQLServerDriver");

                         conn = DriverManager.getConnection("jdbc:sqlserver://localhost;databasename=testDB";"sa";"sasa");

                  }catch(ClassnOntFoundException e){

                         e.printStackTrace();

                  }catch(SQLException ex){

                         ex.printStackTrace();

                  }

                 

                  return conn;

           }

     

           public static Statement getStatement(Connection conn){

                  Statement stmt = null;

                  try{

                         if(conn!=null){

                                stmt = conn.createStatement();

                         }

                  }catch(SQLException e){

                         e.printStackTrace();

                  }

                 

                  return stmt;

           }

          

           public static ResultSet getResultSet(Statement stmt,String sql){

                  ResultSet rs = null;

                  try{

                         if(stmt!=null){

                                rs = stmt.executeQuery(sql);

                         }

                  }catch(SQLException e){

                         e.printStackTrace();

                  }

           }

     

           public static void closeConn(Connection conn){

                  try{

                         if(conn!=null){

                                conn.close();

                                conn=null;

                         }

                  }catch(SQLException e){

                         e.printStackTrace();

                  }

           }

     

           public static void closeStmt(Statement stmt){

                  try{

                         if(stmt!=null){

                                stmt.close();

                                stmt=null;

                         }

                  }catch(SQLException e){

                         e.printStackTrace();

                  }

           }

          

           public static void closeResultSet(ResultSet rs){

                  try{

                         if(rs!=null){

                                rs.close();

                                rs=null;

                         }

                  }catch(SQLException e){

                         e.printStackTrace();

                  }

           }

          

    }


    最新回复(0)