Servlet-4

本文介绍了Web应用中相对路径和绝对路径的使用案例,重点讲解了通过点击按钮实现页面跳转的方法。此外,还深入探讨了Servlet的配置方式及其生命周期管理,包括初始化参数的设置与读取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对于相对路径和绝对路径的使用案例
当前页面:/EmpManger/findEmp
通过点击”增加”按钮,希望跳转到/EmpManager/add_emp.html(则作为目标页面)
则”增加”按钮写的事件为 οnclick=”location.href=’add_emp.html’” 使用相对路径

这里写图片描述

重定向

重定向背景:解决2个网站之间的跳转问题。‘                           
重定向扩增:一个项目内,2个彼此独立(不依赖)的组件之间使用重定向                           
一般情况下:                          
    1.删除后重定向到查询                     
    2.增加,修改后重定向到查询                      

这里写图片描述

路径

这里写图片描述

Servlet访问路径

这里写图片描述

Servlet的生命周期

这里写图片描述

<servlet>                                   
    <servlet-name>springmvc</servlet-name>                              
    <servlet-class>                             
        org.springframework.web.servlet.DispatcherServlet                           
    </servlet-class>                                
    <!-Servlet的初始化参数如何设置-->                             
    <init-param>                                
            <param-name>contextConfigLocation</param-name>                      
            <param-value>classpath:conf/spring-*.xml</param-value>                      
    </init-param>                               
    <!--在启动tomcat时加载(创建及初始化)此Servlet,中间的数字代表加载Servlet的顺序-->                             
    <load-on-startup>1</load-on-startup>                                
</servlet>                                  

如何读取Servlet的初始化参数?
String ServletConfig.getInitParameter(“company”);

1.有时候需要给Servlet预置一些参数,如每页显示条目数(size).                                       
2.可以自己写配置文件(properties/xml),自己写工具读取;                                        
3.也可以使用ServletConfig和ServletContext,他们都能够自动获取web.xml中预置的参数,传给Servlet        

这里写图片描述
这里写图片描述

<servlet>
        <servlet-name>text</servlet-name>                       
        <servlet-class>                     
            web.logServlet                  
        </servlet-class>                        
        <init-param>                        
                <param-name>maxOnline</param-name>              
                <param-value>20000</param-value>                
        </init-param>                       
        <load-on-startup>1</load-on-startup>                        
<servlet>                               
<servlet-mapping>                               
        <servlet-name>springmvc</servlet-name>                      
        <url-pattern>*.do</url-pattern>                     
</servlet-mapping>                              

<!--此桉树可以被多个Servlet共用。在tomcat启动时,它会自动创建ServletContext对象,并且调用该对象的方法自动读取此参数.-->                               
<context-param>                             
        <param-name>size</param-name>                       
        <param-value>10</param-value>                       
</context-param>                                
public class test extends HttpServlet {                                 

    @Override                               
    public void init(ServletConfig config) throws ServletException {                                
        super.init(config);                         
        System.out.println(config.getInitParameter("maxOnline"));                           
    }                               

    @Override                               
    protected void service(                             
            HttpServletRequest req,                         
            HttpServletResponse res) throws ServletException, IOException {                     

        ServletConfig config=getServletConfig();                            
        System.out.println(config.getInitParameter("maxOnline"));                           
        System.out.println("这里进行登录");                           
        //使用context获取web.xml中预置的参数                          
        //该对象中的数据可以被所有的Servlet共用                            
        ServletContext ctx=getServletContext();                         
        System.out.println(ctx.getInitParameter("size"));                           
    }                               
}                                   

context的特殊用法:
1.可以在程序运行期间读写变量;
2.由于context是共用的对象,因此存入其内的变量是多个Servlet间共有的;

    /**                             
     *  该Servlet不是用来处理请求的,仅仅是                           
     *  用来在启动tomcat时初始化项目中所需的                           
     *  一些参数,因此只有init()。                            
     */                             
    public class InitServlet extends HttpServlet {                              

        @Override                           
        public void init(ServletConfig config) throws ServletException {                            
            super.init(config);                     
            //初始化流量参数count,默认为0。                        
            //当用户访问任意Servlet时才累加count。                      
            //该参数是共用的,存入context。                        
            ServletContext ctx = getServletContext();                       
            ctx.setAttribute("count", 0);                       
        }                           

    }   
public class FindEmpServlet extends HttpServlet {                                   

        @Override                               
        protected void service(                             
            HttpServletRequest req,                             
            HttpServletResponse res) throws ServletException, IOException {                         
            //在tomcat启动时会自动创建context。                           
            //并且只在此时创建一次,因此它是单例的。                           
            ServletContext ctx = getServletContext();                           
            String size = ctx.getInitParameter("size");                         
            System.out.println(size);                           
            //访问时将流量+1                          
            Integer count = (Integer)                           
                ctx.getAttribute("count");                      
            count++;                            
            ctx.setAttribute("count", count);                           
            System.out.println("当前流量"+count);                           
        }                               

    }                                   

这里写图片描述

Servlet线程安全问题:

1.局部变量存入栈内;                 
2.每个线程有不同的栈帧;                   
3.不同的线程数据时分开的。无线程安全问题;                  

1.成员变量存入堆中。                 
2.堆中该数据只有一份。                    
3.不同线程同时修改这一份数据有线程安全问题;                 

解决方案:                   
1.不用成员变量;                   
2.加锁;                   

这里写图片描述

    //成员变量                                  
    double sal=100.0;                                   
    @Override                                   
    protected void service(                                 
            HttpServletRequest req,                             
            HttpServletResponse res) throws ServletException, IOException {                         
        synchronized(this){                             
            //加薪                            
            sal+=100.0;                         
            //假设网络延迟 8s                         
            try{                            
                Thread.sleep(8000);                     
            }catch(InterruptedException e){                         
                e.printStackTrace();                        
            }                           
            //输出给浏览器                            
            res.setContentType("text/html;charset=utf-8");                          
            PrintWriter out=res.getWriter();                            
            out.println("<p>工资:"+sal+"</p>");                           
            out.close();                            
        }                               
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值