ITPub博客

首页 > IT职业 > IT生活 > servlet详细使用步骤和代码案例说明

servlet详细使用步骤和代码案例说明

IT生活 作者:小梁124 时间:2014-11-10 16:11:43 0 删除 编辑

  发现用servlet的比较少哦,看见老一点的程序员都直接在jsp中直接写代码,比我年轻一点的程序员又直接用的框架struts等。而我呢不喜欢用框架,喜欢自己写代码,而且又有强迫症,一定要把服务器代码和html分开,所以我平时就用ajax和servlet最多,其实会熟练使用servlet还是很有必要的,提升开发效率,代码整洁,了解原理都有很大的帮助,下面就一起来学习回顾servlet的使用。

  servlet详细使用步骤和代码案例说明:

  首先唠叨一下什么是servlet,从最基础最理论的讲一下吧。

  servlet是一个java class;

  servlet是继承自HttpServlet的class;

  不在客户端运行,是在服务端运行,拦截并分发客户端的请求;

  相关介绍:

  --javax.servlet.*:存放于http协议相关的一般性servlet类

  --javax.servlet.http.*:除了继承javax.servlet.* 之外,并且还增加与HTTP协议有关的功能。

  (注意:大家有必要学习一下HTTP协议,因为WEB开发都会涉及到)

  所有的Servlet 都必须实现javax.servlet.Servlet 接口(Interface)。

  若Servlet程序和HTTP 协议无关,那么必须继承javax.servlet.GenericServlet类;

  若Servlet程序和HTTP 协议有关,那么必须继承javax.servlet.http.HttpServlet 类。

  --HttpServlet:提供了一个抽象类用来创建Http Servlet。

  public void doGet()方法:用来处理客户端发出的GET请求

  public void doPost()方法:用来处理POST请求

  --javax.servlet包的接口:

  ServletConfig接口:在初始化的过程中由Servlet容器使用

  ServletContext接口:定义Servlet用于获取来自其容器的信息的方法

  ServletRequest接口:向服务器请求信息

  ServletResponse接口:响应客户端请求

  Filter接口:

  --javax.servlet包的类:

  ServletInputStream类:用于从客户端读取二进制数据

  ServletOutputStream类:用于将二进制数据发送到客户端

  --javax.servlet.http包的接口:

  HttpServletRequest接口:提供Http请求信息

  HttpServletResponse接口:提供Http响应

  Servlet生命周期

  --Servlet生命周期就是指创建Servlet实例后,存在的时间以及何时销毁的整个过程.

  --Servlet生命周期有三个方法

  init()方法:

  service()方法:Dispatches client requests to the protected servicemethod

  destroy()方法:Called by the servlet container to indicate to a servlet that the servlet is being taken out of service.

  --Servlet生命周期的各个阶段

  ----实例化:Servlet容器创建Servlet实例

  ----初始化:调用init()方法

  ----服务:如果有请求,调用service()方法

  ----销毁:销毁实例前调用destroy()方法

  ----垃圾收集:销毁实例

  (正式开始.......)

  1、在eclipse或者Myeclipse里新建Web Project

  2、选择Generate web.xml deployment descriptor

  3、编码前段页面

  登陆页面:



  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>



  2.   
  3.    
  4.   
  5.   
  6.   
  7.     This is my first JSP page.


  8.    

  9.             用户名:


  10.             密  码:


  11.                       
  12.             
  13.    
  14.   
复制代码



成功登陆之后的页面:


  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>



  2.   
  3.   
  4.   
  5.   
  6.     This is my second JSP page.


  7.     <%
  8.     String name = (String)session.getAttribute("username");
  9.     String pass = (String)session.getAttribute("password");
  10.     %>
  11.     welcome <%=name%>!


  12.     you password is <%=pass%>
  13.   
复制代码

4、在WebRoot(eclipse中式WebCotent)/WEB-INF/web.xml中添加servlet部署




  1.   
  2.   servletTest
  3.   
  4.   
  5.     index.jsp
  6.   
  7.   
  8.   
  9.     This is the description of my J2EE component
  10.     This is the display name of my J2EE component
  11.     servletdemo
  12.     com.servlet.servletdemo
  13.   
  14.   
  15.   
  16.     servletdemo
  17.     /servletdemo//此处“/”后的名字和jsp页面中form表单的action保持一致
  18.   
  19.   
复制代码

5、编码servlet处理class


  1. package com.servlet;

  2. import java.io.IOException;
  3. import java.nio.charset.Charset;

  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import javax.servlet.http.HttpSession;

  9. public class servletdemo extends HttpServlet{

  10.     /**
  11.      *
  12.      */
  13.     private static final long serialVersionUID = 1L;

  14.     //初始化
  15.     public void init() throws ServletException {
  16.         System.out.println("我是init()方法!用来进行初始化工作");
  17.     }
  18.    
  19.     //处理GET请求
  20.     public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
  21.         doPost(request, response);
  22.     }
  23.    
  24.     //处理POST请求
  25.     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  26.         String username = new String(request.getParameter("username").getBytes("ISO-8859-1"),"utf-8");
  27.         String password = request.getParameter("password");
  28.         HttpSession session = request.getSession();
  29.         session.setAttribute("username", username);
  30.         session.setAttribute("password", password);
  31.         response.sendRedirect("/servletTest/success.jsp");
  32.         System.out.println("用户名:"+username);
  33.         System.out.println("密码:"+password);
  34.     }
  35.    
  36.     //销毁实例
  37.     public void destroy() {
  38.         super.destroy();
  39.         System.out.println("我是destroy()方法!用来进行销毁实例的工作");
  40.     }
复制代码

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>



  2.   
  3.    
  4.   
  5.   
  6.   
  7.     This is my first JSP page.


  8.    

  9.             用户名:


  10.             密  码:


  11.                       
  12.             
  13.    
  14.   
复制代码



成功登陆之后的页面:


  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>



  2.   
  3.   
  4.   
  5.   
  6.     This is my second JSP page.


  7.     <%
  8.     String name = (String)session.getAttribute("username");
  9.     String pass = (String)session.getAttribute("password");
  10.     %>
  11.     welcome <%=name%>!


  12.     you password is <%=pass%>
  13.   
复制代码

4、在WebRoot(eclipse中式WebCotent)/WEB-INF/web.xml中添加servlet部署




  1.   
  2.   servletTest
  3.   
  4.   
  5.     index.jsp
  6.   
  7.   
  8.   
  9.     This is the description of my J2EE component
  10.     This is the display name of my J2EE component
  11.     servletdemo
  12.     com.servlet.servletdemo
  13.   
  14.   
  15.   
  16.     servletdemo
  17.     /servletdemo//此处“/”后的名字和jsp页面中form表单的action保持一致
  18.   
  19.   
复制代码

5、编码servlet处理class


  1. package com.servlet;

  2. import java.io.IOException;
  3. import java.nio.charset.Charset;

  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import javax.servlet.http.HttpSession;

  9. public class servletdemo extends HttpServlet{

  10.     /**
  11.      *
  12.      */
  13.     private static final long serialVersionUID = 1L;

  14.     //初始化
  15.     public void init() throws ServletException {
  16.         System.out.println("我是init()方法!用来进行初始化工作");
  17.     }
  18.    
  19.     //处理GET请求
  20.     public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
  21.         doPost(request, response);
  22.     }
  23.    
  24.     //处理POST请求
  25.     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  26.         String username = new String(request.getParameter("username").getBytes("ISO-8859-1"),"utf-8");
  27.         String password = request.getParameter("password");
  28.         HttpSession session = request.getSession();
  29.         session.setAttribute("username", username);
  30.         session.setAttribute("password", password);
  31.         response.sendRedirect("/servletTest/success.jsp");
  32.         System.out.println("用户名:"+username);
  33.         System.out.println("密码:"+password);
  34.     }
  35.    
  36.     //销毁实例
  37.     public void destroy() {
  38.         super.destroy();
  39.         System.out.println("我是destroy()方法!用来进行销毁实例的工作");
  40.     }
复制代码

(文章来源:http://www.nei-mao.com/)


  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>



  2.   
  3.    
  4.   
  5.   
  6.   
  7.     This is my first JSP page.


  8.    

  9.             用户名:


  10.             密  码:


  11.                       
  12.             
  13.    
  14.   
复制代码



成功登陆之后的页面:


  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>



  2.   
  3.   
  4.   
  5.   
  6.     This is my second JSP page.


  7.     <%
  8.     String name = (String)session.getAttribute("username");
  9.     String pass = (String)session.getAttribute("password");
  10.     %>
  11.     welcome <%=name%>!


  12.     you password is <%=pass%>
  13.   
复制代码

4、在WebRoot(eclipse中式WebCotent)/WEB-INF/web.xml中添加servlet部署




  1.   
  2.   servletTest
  3.   
  4.   
  5.     index.jsp
  6.   
  7.   
  8.   
  9.     This is the description of my J2EE component
  10.     This is the display name of my J2EE component
  11.     servletdemo
  12.     com.servlet.servletdemo
  13.   
  14.   
  15.   
  16.     servletdemo
  17.     /servletdemo//此处“/”后的名字和jsp页面中form表单的action保持一致
  18.   
  19.   
复制代码

5、编码servlet处理class


  1. package com.servlet;

  2. import java.io.IOException;
  3. import java.nio.charset.Charset;

  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import javax.servlet.http.HttpSession;

  9. public class servletdemo extends HttpServlet{

  10.     /**
  11.      *
  12.      */
  13.     private static final long serialVersionUID = 1L;

  14.     //初始化
  15.     public void init() throws ServletException {
  16.         System.out.println("我是init()方法!用来进行初始化工作");
  17.     }
  18.    
  19.     //处理GET请求
  20.     public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
  21.         doPost(request, response);
  22.     }
  23.    
  24.     //处理POST请求
  25.     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  26.         String username = new String(request.getParameter("username").getBytes("ISO-8859-1"),"utf-8");
  27.         String password = request.getParameter("password");
  28.         HttpSession session = request.getSession();
  29.         session.setAttribute("username", username);
  30.         session.setAttribute("password", password);
  31.         response.sendRedirect("/servletTest/success.jsp");
  32.         System.out.println("用户名:"+username);
  33.         System.out.println("密码:"+password);
  34.     }
  35.    
  36.     //销毁实例
  37.     public void destroy() {
  38.         super.destroy();
  39.         System.out.println("我是destroy()方法!用来进行销毁实例的工作");
  40.     }
复制代码

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29870867/viewspace-1327385/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-09-11

  • 博文量
    71
  • 访问量
    214129