Servlet

1.初始Servlet

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServletTest extends HttpServlet {
      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {	  	
    response.setContentType("text/html;charset=GBK");
	PrintWriter out = response.getWriter();
	out.println("<html>");
	out.println("  <head><title>Servlet</title></head>");
	out.println("  <body>");
	out.println("你好,欢迎来到Servlet世界");
	out.println("  </body>");
	out.println("</html>");
	out.close();
     }
}

        1.1在web.xml配置Servlet

<web-app>
    <servlet>
        <servlet-name>对象名</servlet-name>
        <servlet-class>配置servlet的完整类名/全限定名:包名+类名</servlet-class>
        <init-param>
            <param-name>initParam</param-name>
            <param-value>Hello Servlet</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <!-- /:localhost:8080/项目名 -->
        <servlet-name>对象名</servlet-name>
        <url-pattern>/路径.do</url-pattern>
    </servlet-mapping>
</web-app>

1.2 获取初始化参数

public class IndexServlet extends HttpServlet{
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");

		// 2.获取 out 内置对象
		PrintWriter out = resp.getWriter();
}

2. 常用的九个jsp内置对象

request:请求

response:响应

session:会话

out:输出/打印

application:应用程序

pageContext:页面上下文对象

exception:对象表示的就是JSP引擎在执行代码过程中抛出的种种异常,只有在page指令中设置isErrorPage="true"的页面中才能使用。

page:页面对象

config:配置对象

3.实现Servlet的三种方式

实现Servlet的三种方式:一个实现,两个继承

3.1第一种方法,实现Servlet接口

package com.xinqi.sevlet;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.Servlet;
 6 import javax.servlet.ServletConfig;
 7 import javax.servlet.ServletException;
 8 import javax.servlet.ServletRequest;
 9 import javax.servlet.ServletResponse;
10 
11 public class testServlet1 implements Servlet {
12 
13     /**
14      * 销毁方法
15      */
16     public void destroy() {
17         System.out.println("这是销毁方法!");
18         
19     }
20     
21     /**
22      * 初始化方法
23      */
24     public void init(ServletConfig arg0) throws ServletException {
25         System.out.println("这是初始化方法!");
26     }
27 
28     /**
29      * 服务方法
30      */
31     public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
32         
33         System.out.println("这是服务方法!");
34         
35     }
36 
37     
38     //获取servlet配置信息
39     public ServletConfig getServletConfig() {
40         
41         return null;
42     }
43 
44     //获取servlet信息
45     public String getServletInfo() {
46         
47         return null;
48     }
49 }

3.2第二种方法:继承GenericServlet抽象类

GenericServlet抽象类中只有一个抽象方法service()方法

 1 package com.xinqi.sevlet;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.GenericServlet;
 6 import javax.servlet.ServletException;
 7 import javax.servlet.ServletRequest;
 8 import javax.servlet.ServletResponse;
 9 
10 public class testServlet2 extends GenericServlet{
11 
12     @Override
13     public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
14         
15         System.out.println("这是服务方法!");
16     }
17 
18 }

3.3第三种方式:继承HttpServlet抽象类重写doPut()和doPost()方法

 1 package com.xinqi.sevlet;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 public class testServlet3 extends HttpServlet{
11 
12     @Override
13     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
14 
15         System.out.println("这是get方法提交");
16         
17         doGet(req, resp);
18     }
19     
20 
21     @Override
22     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
23         System.out.println("这是post方法提交");
24     }
25 
26 }

4.Servlet的生命周期

Servlet

上一篇:SpringBoot实现AOP


下一篇:git介绍及简单使用