码迷,mamicode.com
首页 > 其他好文 > 详细

servlet基本用法

时间:2015-08-04 15:49:25      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:servlet

一:hello,world程序

public class Hello extends HttpServlet {

 

   public voiddoGet(HttpServletRequest req, HttpServletResponse resp)

         throws ServletException,IOException {

      PrintWriterout=resp.getWriter();

      out.println("<html>");

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

      out.println("<body>");

      out.println("<h1>hello,word</h1>");

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

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

   }

}

 

web.xml文件配置

<servlet>

   <servlet-name>Hello</servlet-name>

   <servlet-class>com.cloud.Demo1.Hello</servlet-class>

 </servlet>

 

 <servlet-mapping>

   <servlet-name>Hello</servlet-name>

   <url-pattern>/Hello</url-pattern>

  </servlet-mapping>

 

访问地址:

http://localhost:8080/servlet/Hello

 

二:处理表单提交

1.     页面

<body>

  <form action="/servlet/Input"method="get">

     <inputtype="text" name="info">

     <inputtype="submit" value="提交">

  </form>

</body>

 

2.     接收页面

public voiddoGet(HttpServletRequest req, HttpServletResponse resp)

                     throws ServletException,IOException {

              PrintWriter out=resp.getWriter();

              Stringinfo=req.getParameter("info");

              out.println("<html>");

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

              out.println("<body>");

              out.println("<h1>"+info+"</h1>");

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

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

       }

 

3.     配置web.xml

<servlet>

   <servlet-name>Input</servlet-name>

   <servlet-class>com.cloud.Demo1.Input</servlet-class>

  </servlet>

 

<servlet-mapping>

   <servlet-name>Input</servlet-name>

   <url-pattern>/Input</url-pattern>

  </servlet-mapping>

 

注意事项:

1.提交路径

/项目名/文件名à action="/servlet/Input"

2.提交方法和接收方法必须相同

post提交post接收,get提交get接收

 

三:servlet生命周期

1.     加载servlet

2.     初始化

3.     处理服务

4.     销毁

5.     卸载

serlife.java

public class serlife extends HttpServlet {

   public void init() throws ServletException {

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

   }

   public voiddoGet(HttpServletRequest request, HttpServletResponse response)

         throws ServletException,IOException {

      doPost(request,response);

      System.out.println("servlet服务:doGet,doPost");

   }

 

   public voiddoPost(HttpServletRequest request, HttpServletResponse response)

         throws ServletException,IOException {

      request.setCharacterEncoding("utf-8");

      response.setContentType("text/html;charset=utf-8");

   }

   public void destroy() {

      super.destroy();// Just puts "destroy" string inlog

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

   }

}

 

web.xml配置

 

<servlet>

   <servlet-name>serlife</servlet-name>

   <servlet-class>com.cloud.Demo1.serlife</servlet-class>

   <!-- 配置设成自动加载 -->

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

</servlet>

 

<servlet-mapping>

   <servlet-name>serlife</servlet-name>

   <url-pattern>/serlife</url-pattern>

  </servlet-mapping>

 

四:获取初始化信息

 

public classserlife extends HttpServlet {

   private String initParam=null;

   public void init(ServletConfigconfig) throwsServletException {

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

      this.initParam=config.getInitParameter("href");

   }

   public voiddoGet(HttpServletRequest request, HttpServletResponse response)

         throws ServletException,IOException {

      doPost(request,response);

      System.out.println("servlet服务:doGet,doPost"+this.initParam);

   }

 

web.xml

<servlet>

   <servlet-name>serlife</servlet-name>

   <servlet-class>com.cloud.Demo1.serlife</servlet-class>

   <!-- 配置初始化信息 -->

   <init-param>

      <param-name>href</param-name>

      <param-value>Spring</param-value>

   </init-param>

  </servlet>

 

 

版权声明:博主原创文章,转载请说明出处。http://blog.csdn.net/dzy21

servlet基本用法

标签:servlet

原文地址:http://blog.csdn.net/dzy21/article/details/47275777

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!