Servlet

 

1、Servlet的开发形式

2、Servlet的声明周期

3、搞清service()和doGet或doPost之间的关系

4、可以从Servlet之中取得session或application对象

5、RequestDispatcher接口可以专递request属性范围内容

 

Servlet中操作request、response、session、application。

 

Servlet呈现是采用多线程方式的。所有的Servlet程序必须放在WEB目录中,依靠Web容器执行。Servlet与JavaBean一样,必须放在WEB-INF/classes中,一个Servlet类必须 继承HttpServlet,Servlet程序必须放在一个包中。

每一个Servlet必须继承HttpServlet实现doGet或doPost()方法:

例如:HelloServlet。Java

package org.lxh.servlet ;

import java.io.* ;

import javax.servlet.* ;

import javax.servlet.http.* ;

public class HelloServlet extends HttpServlet{

       public void doGet(HttpServletRequest req,HttpServletResponse resp)

              throws ServletException,java.io.IOException{

              PrintWriter out = null ;

              out = resp.getWriter() ;

              out.println("<html>") ;

              out.println("<head><title>hello</title></head>") ;

              out.println("<body>") ;

              out.println("<h1><font color=\"red\">");

              out.println("HELLO WORLD!") ;

              out.println("</font></h1>") ;

              out.println("</body>") ;

              out.println("</html>") ;

              out.close() ;

       }

};

/*   在web。Xml中配置

       <servlet>

              <servlet-name>hello</servlet-name>

              <servlet-class>org.lxh.servlet.HelloServlet</servlet-class>

       </servlet>

       <servlet-mapping>

              <servlet-name>hello</servlet-name>

              <url-pattern>/demo</url-pattern>

       </servlet-mapping>

      

*/

在Servlet中doGet方法只能处理get方式,如果要想Servlet能够同时能够处理get和post方式,则,我们可以用下面的方式,这也是我们以后最常用的方式

package org.lxh.servlet ;

import java.io.* ;

import javax.servlet.* ;

import javax.servlet.http.* ;

public class InputServlet extends HttpServlet{

       public void doGet(HttpServletRequest req,HttpServletResponse resp)

              throws ServletException,java.io.IOException{

              //servlet处理的代码

       }

       public void doPost(HttpServletRequest req,HttpServletResponse resp)

              throws ServletException,java.io.IOException{

              this.doGet(req,resp) ; //与doget方式一样

       }

};

 

Servlet的声明周期:

装载—>创建à初始化à服务(多次)à销毁à卸载

Servlet本身只是关心初始化、服务、销毁三个操作

1、  初始化:正常的情况下只有一次初始化,但是如果一个Servlet长时间不使用的之后又再次使用,则会重新初始化。

|- public void init()throws ServletException:如果不需要取得初始化参数话使用此方法。

|-public void init(ServletConfig config)throw ServletException:取得初始化参数

2、  服务:一个Servlet肯定会服务多次,如果一个Servlet长时间不服务,则认为没用了,等待销毁。

|-public abstract void service(ServletRequest req,ServletResponse resp)throws ServletException,java.io.IOException

|-一般情况下不会直接覆写此方法,而是覆写:

        |- public void doGet(ServletRequest req,ServletResponse resp)         

|- public void doPost(ServletRequest req,ServletResponse resp)

3、  销毁:正常的情况下也只是一次,容器关闭时候销毁,当然,如果一个Servlet长时间不使用的话也会自动销毁。Public void destroy()

范例:Servlet

package org.lxh.servlet ;

import java.io.* ;

import javax.servlet.* ;

import javax.servlet.http.* ;

public class LifeCycleServlet extends HttpServlet{

       public void init() throws ServletException{

              System.out.println("************** 初始化 **************") ;

       }

       public void init(ServletConfig config) throws ServletException{

              System.out.println("************** 初始化"+config.getInitParameter("ref1")+" **************") ;

       }

       public void service(ServletRequest req,ServletResponse res){

              System.out.println("************** service服务 **************") ;

       }

       public void doGet(HttpServletRequest req,HttpServletResponse resp)

              throws ServletException,java.io.IOException{

              System.out.println("************** 服务 **************") ;

       }

       public void doPost(HttpServletRequest req,HttpServletResponse resp)

              throws ServletException,java.io.IOException{

              this.doGet(req,resp) ;

       }

       public void destroy(){

              System.out.println("************** 销毁 **************") ;

       }

};

Web。Xml的配置

       <servlet>

              <servlet-name>life</servlet-name>

              <servlet-class>org.lxh.servlet.LifeCycleServlet</servlet-class>

              <init-param>

                     <param-name>ref1</param-name>

                     <param-value>MLDN</param-value>

              </init-param>

       </servlet>

       <servlet-mapping>

              <servlet-name>life</servlet-name>

              <url-pattern>/life</url-pattern>

       </servlet-mapping>

//通过URL:http://localhost:8080/projectname/life访问测试

 

如果一个Servlet类中,覆写了sevice()方法,则此时肯定不会在执行doGet或doPost方法,因为所有的doXxx()操作都在父类中使用了service方法进行区分的,相当于一下形式:容器-找到—>service()方法—在又service方法找到—>doXxx()方法;这是一个典型的模板方法,一般在子类中不应该写service方法,而是直接覆写doGet或doPost方法

       当然,默认情况下一个Servlet只能在第一次初始化,也可以通过配置,让其在容器启动的时候可以进行初始化操作,修改web.xml

 

<servlet>

              <servlet-name>life</servlet-name>

              <servlet-class>org.lxh.servlet.LifeCycleServlet</servlet-class>

             <load-on-startup>1</load-on-startup>

              <init-param>

                     <param-name>ref1</param-name>

                     <param-value>MLDN</param-value>

              </init-param>

       </servlet>

       <servlet-mapping>

              <servlet-name>life</servlet-name>

              <url-pattern>/life</url-pattern>

       </servlet-mapping>

 

Servlet获取session

package org.lxh.servlet ;

import java.io.* ;

import javax.servlet.* ;

import javax.servlet.http.* ;

public class OtherServlet extends HttpServlet{

       public void doGet(HttpServletRequest req,HttpServletResponse resp)

              throws ServletException,java.io.IOException{

              HttpSession ses = null ;

               ses = req.getSession() ;     // 取得Session对象

               System.out.println("**********" + ses.getId()) ;

               ServletContext app = this.getServletContext() ;

              System.out.println("*****" + app.getRealPath("/")) ;

             

       }

       public void doPost(HttpServletRequest req,HttpServletResponse resp)

              throws ServletException,java.io.IOException{

              this.doGet(req,resp);

       }

};

Servlet获取application对象

package org.lxh.servlet ;

import java.io.* ;

import javax.servlet.* ;

import javax.servlet.http.* ;

public class OtherServlet extends HttpServlet{

       public void doGet(HttpServletRequest req,HttpServletResponse resp)

              throws ServletException,java.io.IOException{

              ServletContext app = this.getServletContext() ;

              System.out.println("*****" + app.getRealPath("/")) ;

             

       }

       public void doPost(HttpServletRequest req,HttpServletResponse resp)

              throws ServletException,java.io.IOException{

              this.doGet(req,resp);

       }

};

 

JSP或html通过form等方式可以轻松地跳到Servlet中,同样Servlet也可以跳到JSP中

客户端的跳转

package org.lxh.servlet ;

import java.io.* ;

import javax.servlet.* ;

import javax.servlet.http.* ;

public class OtherServlet extends HttpServlet{

       public void doGet(HttpServletRequest req,HttpServletResponse resp)

              throws ServletException,java.io.IOException{

              req.getSession().setAttribute("name","HELLO WORLD") ;

              req.setAttribute("name","HELLO WORLD") ;

              resp.sendRedirect("demo.jsp") ;

       }

       public void doPost(HttpServletRequest req,HttpServletResponse resp)

              throws ServletException,java.io.IOException{

              this.doGet(req,resp);

       }

};

最常用服务器端的跳转,相当于forward

package org.lxh.servlet ;

import java.io.* ;

import javax.servlet.* ;

import javax.servlet.http.* ;

public class OtherServlet extends HttpServlet{

       public void doGet(HttpServletRequest req,HttpServletResponse resp)

              throws ServletException,java.io.IOException{

              RequestDispatcher rd = req.getRequestDispatcher("demo.jsp") ;

              rd.forward(req,resp) ;  // 跳转

       }

       public void doPost(HttpServletRequest req,HttpServletResponse resp)

              throws ServletException,java.io.IOException{

              this.doGet(req,resp);

       }

};

 

 

你可能感兴趣的:(session,servlet,service,application,Class,import)