JavaWeb--Servlet1

1.什么是servlet

        servlet是javaEE规范之一(规范就是接口),servlet是javaweb的三大组件之一,servlet是运行在服务器上的一个java小程序,他接收客户端请求,响应数据给客户端。

2.手动实现servlet程序(接口方式)

1.新建类实现servlet接口

2.重写service方法

3.到web.xml中配置servlet访问地址


public class HelloServlet1 implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("第一个servlet程序");
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!--servlet标签给tomcat配置servlet程序-->
    <servlet>
    <!--别名一般和类名一样-->
        <servlet-name>helloServlet1</servlet-name>
    <!--类全路径-->
        <servlet-class>com.example.HelloServlet1</servlet-class>
    </servlet>
    <!--servlet中别名为HelloServlet1的访问地址-->
    <servlet-mapping>
        <servlet-name>helloServlet1</servlet-name>
        <!--访问地址,,/hello中/表示工程路径,/hello表示访问目录-->
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

3.servlet生命周期

1.执行servlet构造方法

2.执行init初始化方法

3.执行service方法

4.执行destroy的销毁方法

public class HelloServlet1 implements Servlet {
    public HelloServlet1() {
        System.out.println("我第一步执行且只执行一次");
    }

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("我第二步执行且只执行二次");
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        //System.out.println("第一个servlet程序");
        System.out.println("我第三步执行,只要输入网页地址我就执行");
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("我第四步执行且只执行一次,是在关闭服务器时执行");
    }
}

4.get和post方式(在form表单中点击提交就会跳转至action=“地址”中执行)

get方式:

<servlet>
        <servlet-name>helloServlet2</servlet-name>
        <servlet-class>com.example.HelloServlet2</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>helloServlet2</servlet-name>
        <url-pattern>/hello2</url-pattern>
    </servlet-mapping>
<form action="http://localhost:8080/Servlet/hello2" method="get">
    <input type="submit" value="提交">
</form>
@Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("我是通过get方式");
    }

post方式:

<form action="http://localhost:8080/Servlet/hello2" method="post">
    <input type="submit" value="提交">
</form>
@Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("我是通过post方式");
    }
<servlet>
        <servlet-name>helloServlet2</servlet-name>
        <servlet-class>com.example.HelloServlet2</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>helloServlet2</servlet-name>
        <url-pattern>/hello2</url-pattern>
    </servlet-mapping>

get方式和post方式结合

<form action="http://localhost:8080/Servlet/hello2" method="post">
    <input type="submit" value="提交">
<servlet>
        <servlet-name>helloServlet2</servlet-name>
        <servlet-class>com.example.HelloServlet2</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>helloServlet2</servlet-name>
        <url-pattern>/hello2</url-pattern>
    </servlet-mapping>
 @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        //HttpServletRequest是servletRequest子类
        HttpServletRequest httpServletRequest=(HttpServletRequest) servletRequest;
        //通过getMethod方法可以获取是get方式还是post方式
        String method = httpServletRequest.getMethod();
        System.out.println(method);
        if ("get".equalsIgnoreCase(method)){
            System.out.println("Get方式提交");
        }else if ("post".equalsIgnoreCase(method)){
            System.out.println("Post方式提交");
        }
        System.out.println(httpServletRequest.toString());
    }

5.通过继承方式实现Servlet程序

1.继承HttpServlet类

2.根据业务编写dogetpost方法

3.xml文件配置映射

<form action="http://localhost:8080/Servlet/hello3" method="post">
    <input type="submit" value="提交">
</form>
<servlet>
        <servlet-name>helloServlet3</servlet-name>
        <servlet-class>com.example.HelloServlet3</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>helloServlet3</servlet-name>
        <url-pattern>/hello3</url-pattern>
    </servlet-mapping>
public class HelloServlet3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("我是doGet方式");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("我是doPost方式");
    }
}

6.使用idea快捷生成Servlet程序

类-->右击-->create new Servlet

Name:(servlet-name)

Package:(包路径)

class:(全类名)

自己配置访问地址映射

7.ServletConfig类(配置信息类)

通过接口方式的作用(都是在init中使用):

        1.获取servlet-name得值

        2.获取初始化参数init-param值

        3.获取servletContext对象

<servlet>
        <servlet-name>helloServlet4</servlet-name>
        <servlet-class>com.example.HelloServlet4</servlet-class>
        <init-param>
            <param-name>username</param-name>
            <param-value>root</param-value>
        </init-param>
        <init-param>
            <param-name>password</param-name>
            <param-value>123456</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>helloServlet4</servlet-name>
        <url-pattern>/hello4</url-pattern>
    </servlet-mapping>
@Override
    public void init(ServletConfig servletConfig) throws ServletException {
        String servletName = servletConfig.getServletName();
        System.out.println(servletName);
        String username = servletConfig.getInitParameter("username");
        String password = servletConfig.getInitParameter("password");
        System.out.println(username + "::" + password);
        ServletContext servletContext = servletConfig.getServletContext();
        System.out.println(servletContext);
    }

通过继承方式(若是重写了init方法,需要加上父类super):

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("我是doGet方式");
        String username = getInitParameter("username");
        System.out.println(username);
    }
<servlet>
        <servlet-name>helloServlet3</servlet-name>
        <servlet-class>com.example.HelloServlet3</servlet-class>
        <init-param>
            <param-name>username</param-name>
            <param-value>root</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>helloServlet3</servlet-name>
        <url-pattern>/hello3</url-pattern>
    </servlet-mapping>

8.ServletContext类

1.servletContext类是一个接口,他表示Servlcet上下文对象

2.一个web工程只有一个ServlcetContext对象

3.servletContext对象是一个域对象

域对象可以向map一样存取数据的对象

9.servletContext的作用

1.获取web.xml中上下文参数Context-param

2.获取当前工程路径

3.获取工程部署在服务器硬盘上的绝对路径

4.像map一样存取数据

<form action="http://localhost:8080/Servlet/hello5" method="post">
    <input type="submit" value="提交">
</form>
<context-param>
        <param-name>username</param-name>
        <param-value>root</param-value>
    </context-param>
    <servlet>
        <servlet-name>helloServlet5</servlet-name>
        <servlet-class>com.example.HelloServlet5</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>helloServlet5</servlet-name>
        <url-pattern>/hello5</url-pattern>
    </servlet-mapping>
public class HelloServlet5 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = getServletConfig().getServletContext();
        System.out.println(servletContext.getInitParameter("username"));
        String contextPath = servletContext.getContextPath();
        String realPath = servletContext.getRealPath("/");
        System.out.println(contextPath);
        System.out.println(realPath);
        servletContext.setAttribute("key1","value1");
        System.out.println(servletContext.getAttribute("key1"));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object key1 = getServletContext().getAttribute("key1");
        System.out.println(key1);
    }
}

注:全上下文只有一个context对象,通过getAttribute只有一个。

10.Http协议

1.两种请求方式get和post

get:

        a.请求行:请求方式        请求资源路径        版本号

        b.请求头:key:value

post:

        a.请求行:请求方式        请求资源路径        版本号

        b.请求头:key:value

        c.请求体:发送服务器的数据

2.响应http

        a.响应行:版本号        状态码        响应描述符

        b.响应头:key:value

        c.响应体:回传给客户端的数据

3.响应码

        200        请求成功

        302        请求重定向

        404        请求地址错误

        500        服务器内部错误

11.HttpServlet常用方法

1.getRequestURI        获取请求URI

2.getRequestURL        获取请求URL

3.getRemoteHost        获取客户端ip

4.getHeader        获取请求头

5.getParameter        获取请求参数

6.getPrameterValues        获取请求参数(多选,下拉) 

7.setAttribute        设置域对象数据

8.getAttribute        获取域对象数据

9.getMethod        获取请求方式

10.getRequestDispatchert        获取转发方式     

11.getRequestDispatchert.forword(req,resp);

a.

<body>
    <a href="http://localhost:8080/Servlet/hello5">超链接</a>
</body>
 <servlet>
        <servlet-name>helloServlet6</servlet-name>
        <servlet-class>com.example.HelloServlet6</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>helloServlet6</servlet-name>
        <url-pattern>/hello6</url-pattern>
    </servlet-mapping>
public class HelloServlet6 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        System.out.println("请求URI>>" + requestURI);
        StringBuffer requestURL = req.getRequestURL();
        System.out.println("请求URL>>" + requestURL);
        String remoteHost = req.getRemoteHost();
        System.out.println("客户端ip>>" + remoteHost);
        String method = req.getMethod();
        System.out.println("请求方式>>" + method);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}

b.

<form action="http://localhost:8080/Servlet/hello7" method="get">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    权限:<input type="checkbox" name="che" value="管理员">管理员
    <input type="checkbox" name="che" value="学生">学生
    <input type="checkbox" name="che" value="教师">教师<br>
    <input type="submit" value="提交">
</form>
<servlet>
        <servlet-name>helloServlet7</servlet-name>
        <servlet-class>com.example.HelloServlet7</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>helloServlet7</servlet-name>
        <url-pattern>/hello7</url-pattern>
    </servlet-mapping>
public class HelloServlet7 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");//根据前端中的name值
        System.out.println("用户名>>" + username);
        String password = req.getParameter("password");
        System.out.println("密码>>" + password);
        String[] ches = req.getParameterValues("che");
        for (int i = 0; i < ches.length; i++) {
            System.out.print(ches[i] + ":" );
        }
    }
}

c.

请求的转发过程:

        服务器收到请求后从一个资源转移到另一个服务器资源操作

1.取出请求        getParameter("username")

2.找柜台A查证盖章        req.setAttribute("key1","value1");

3.柜台A告诉请求找柜台B怎么走            req.getRequestDispatcher("/hello9").forward(req,resp);

4.柜台B查看请求和盖章

5.执行柜台B   

特点:

        浏览器地址没有发生变化,一次请求,共享一个数据域,可以转发到WEB-INF中,不能访问工程之外的

<form action="http://localhost:8080/Servlet/hello8">
    用户名:<input type="text" name="usernaem">
    <input type="submit" value="提交">
</form>
<servlet>
        <servlet-name>helloServlet8</servlet-name>
        <servlet-class>com.example.HelloServlet8</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>helloServlet8</servlet-name>
        <url-pattern>/hello8</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>helloServlet9</servlet-name>
        <servlet-class>com.example.HelloServlet9</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>helloServlet9</servlet-name>
        <url-pattern>/hello9</url-pattern>
    </servlet-mapping>
public class HelloServlet8 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置字符集
        req.setCharacterEncoding("UTF-8");
        //取参数,获取请求参数(去机器取小票)
        String username = req.getParameter("username");
        //验证(去人工给小票盖章)
        req.setAttribute("key1","value1");
        //去找柜台二(怎么走)
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/hello9");
        requestDispatcher.forward(req,resp);
    }
}



public class HelloServlet9 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查看资料
        String username = req.getParameter("username");
        //查看之前盖章对不对
        Object key1 = req.getAttribute("key1");

        System.out.println("处理自己的业务");
    }
}

12.base标签

        可以设置当前页面中所有相对路径工作时参照这个路径进行跳转

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!--我跳转回来时参考这个路径-->
    <base href="http://localhost:8080/Servlet/">
</head>
<body>
<a href="Hello10.html">我是Hello9点我跳转hello10</a>
</body>
</html>



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<a href="Hello9.html">我是Hello10点我跳转hello9</a>
<a href="http://localhost:8080/Servlet/hello10">我通过请求转发跳转</a>
</body>
</html>
public class HelloServlet10 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("Hello9.html").forward(req,resp);
    }
}
<servlet>
        <servlet-name>helloServlet10</servlet-name>
        <servlet-class>com.example.HelloServlet10</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>helloServlet10</servlet-name>
        <url-pattern>/hello10</url-pattern>
    </servlet-mapping>

13.HttpServletResponse类作用

        返回客户端响应

两种输出说明:

字节流        getoutputStream()

字符流        getWrite()

public class HelloServlet11 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();
        writer.write("respon");
    }
}

14.请求重定向

        客户端给服务端发送请求,然后服务器告诉客户端我给你新的地址,你去新的地址访问

<form action="http://localhost:8080/Servlet/hello11" method="post">
    用户名:<input type="text" name="username">
    密码:<input type="password" name="password">
    <input type="submit" value="提交">
</form>
 <servlet>
        <servlet-name>helloServlet12</servlet-name>
        <servlet-class>com.example.HelloServlet12</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>helloServlet12</servlet-name>
        <url-pattern>/hello12</url-pattern>
    </servlet-mapping>
 @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //方式一:
        // 设置响应重定向,重定向进入即使是post方式,但是经过重定向也是get方式
        //resp.setStatus(302);
        //resp.setHeader("Location","http://localhost:8080/Servlet/hello12");
        //方式二:
        resp.sendRedirect("http://localhost:8080/Servlet/hello12");
    }


public class HelloServlet12 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("重定向进来了");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值