标签:
① Servlet就是JAVA 类
② Servlet是一个继承HttpServlet类的类
③ 这个在服务器端运行,用以处理客户端的请求
Servlet 加载--->实例化--->服务--->销毁--->垃圾收集。
1>init():在Servlet的生命周期中,仅执行一次init()方法。它是在服务器装入Servlet时执行的,负责初始化Servlet 对象。可以配置服务器,以在启动服务器或客户机首次访问Servlet时装入Servlet。无论有多少客户机访问Servlet,都不会重复执行 init()。
2>service():它是Servlet的核心,负责响应客户的请求。每当一个客户请求一个HttpServlet对象,该对象的 Service()方法就要调用,而且传递给这个方法一个“请求”(ServletRequest)对象和一个“响应” (ServletResponse)对象作为参数。在HttpServlet中已存在Service()方法。默认的服务功能是调用与HTTP请求的方法 相应的do功能。==HttpServletRequest和HttpServletResponse类型的对象也是由容器创建的,容器会为不同的客户端创建相应的HttpServletRequest和HttpServletResponse对象
3>destroy(): 仅执行一次,在服务器端停止且卸载Servlet时执行该方法。当Servlet对象退出生命周期时,负责释放占用的资 源。一个Servlet在运行service()方法时可能会产生其他的线程,因此需要确认在调用destroy()方法时,这些线程已经终止或完成。
依据WebRoot--->WEB-INF--->web.xml配置文件创建。。
ServletConfig接口:在初始化的过程中由Servlet容器创建ServletConfig对象,可以获取初始化参数
ServletContext接口:定义Servlet用于获取来自其容器的信息的方法,可以获取上下文对象,他是全局的。每个servlet都可以获取到
ServletRequest接口:向服务器请求信息
ServletResponse接口:响应客户端请求
其中servlet节点和servlet-mapping节点下的servlet-name中的内容必须相同,init-param节点下配置初始化参数。。
Demo1中有初始化和doGet方法以及Destory方法的执行,有ServletConfig对象的测试
package servelet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @author qq * Servlet是由容器(tomcat服务器)来创建对象的 * 当客户端请求该Servlet时,根据请求方式,容器调用Servlet的doGet()或doPost()方法 * * 容器创建Servlet的对象是依据什么?依据WebRoot--->WEB-INF--->web.xml * doGet()或doPost()方法的功能是定义给客户端发送的数据 * * 每个Servlet只被创建一个对象,处理所有的客户端请求 * 一次运行,到处服务 * * * 只创建一个Servlet对象,怎么区分每个客户端? * 容器会为不同的客户端创建相应的HttpServletRequest和HttpServletResponse对象 * 通过这两个对象来区分不同的客户端 * */ public class servlet extends HttpServlet { public servlet() { super(); } /** * 当Servelt实例从内存中消失前,容器会调用该方法 */ public void destroy() { System.out.println("=================被销毁了........."); } /** * request:封装了所有的客户端信息,代表的是客户端对象 * * response:是服务器端对客户端的响应对象,封装了对客户端的响应信息 * * 容器根据客户端的请求方式调用doGet()或doPost()方法,调用方法的同时 * 会传递HttpServletRequest和HttpServletResponse类型的对象,所以这 * 两个对象也是由容器创建的 * * 容器会为不同的客户端创建相应的HttpServletRequest和HttpServletResponse对象 */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");//这条语句指明了向客户端发送的内容格式和采用的字符编码. PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * 这个方法只在Servlet第一次被创建时调用,以后不再调用 * */ public void init() throws ServletException { System.out.println("在Servlet的整个生命周期中,该方法只被调用一次"); //得到该 Servlet的配置参数 ServletConfig config = getServletConfig();//ServletConfig对象是容器创建的,获取ServletConfig对象 String value = (String)config.getInitParameter("name"); String pwd = (String)config.getInitParameter("pwd"); System.out.println(value+pwd); } }
Demo2测试:
ServletContext的测试,先运行Demo2在运行Test 否则结构为null
package servelet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Demo2 extends HttpServlet { /** * Constructor of the object. */ public Demo2() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); // 获取上下文对象 ServletContext context = getServletContext(); // 向上下文中放入数据 context.setAttribute("name", "lipeng"); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to * post. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * Initialization of the servlet. <br> * * @throws ServletException * if an error occurs */ public void init() throws ServletException { // Put your code here } }
package servelet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Test extends HttpServlet { /** * Constructor of the object. */ public Test() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); ServletContext context = getServletContext(); String name = (String)context.getAttribute("name"); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.println("<h1>"+name+"<h1>"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }
Demo3测试:
package servlet; import java.io.IOException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginServlet extends HttpServlet { /** * Constructor of the object. */ public LoginServlet() { super(); } public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } //定义一个再编码再解码的功能 public static String deCode(String ss) { String str = null; try{ byte[] arr = ss.getBytes("ISO8859-1"); str = new String(arr,"utf-8"); }catch(Exception e) { e.printStackTrace(); } return str; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); request.setCharacterEncoding("utf-8");//当使用post方式提交数据时,防止乱码的处理方式 //获取提交过来的用户名,因为属于客户端信息,所以从 request对象中获取 String username = request.getParameter("username"); //String username = deCode(uname); //获取提交过来的密码 String psw = request.getParameter("pwd"); //验证 if(username!=null&&username!=""&&psw!=null&&psw!="") { if("李鹏".equals(username)&&"123".equals(psw)) { out.println("<h1>"+"登陆成功"+"</h1>"); } else out.println("<h1>"+"登陆失败"+"</h1>"); } out.flush(); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } public void init() throws ServletException { // Put your code here } }
<html> <head> <base href="<%=basePath%>"> <title>登陆</title> </head> <body> <form action="servlet/LoginServlet" method="post"> 用户名:<input type="text" name="username"><br> 密 码:<input type="password" name="pwd"><br> <input type="submit" value="登陆"> <input type="reset" value="重置"> </form> </body> </html>
这里涉及到了编码也解码的问题。。
如果是get请求时,一种方法解决乱码问题: 把经过ISO8859-1的解码的字符串再使用ISO8859-1编码,在使用utf-8解码
如果是post请求时,除了可以使用上面的方法外,也可以通过request.setCharacterEncoding("utf-8");设置客户端请求的字符在服务器端的解码方式
结果:
标签:
原文地址:http://www.cnblogs.com/lipeng0824/p/4722838.html