servlet使用详解

1、servlet是个java的类,可以解析和响应http请求, 对于servlet来说,接收数据的方式有两种,
如果html以超链接,表单get提交数据,就会调用servlet的doGet方法
如果html是以表单post提交数据,就会调用servlet的doPost方法,

在这两个方法中, 都有一个HttpServletRequest的参数,这就是我们http提交上来的数据包,我们未来就需要从这个数据包里面获取客户端你的所有数据。

1、通过request获取客户端提交的参数

<form action="http://localhost:8088/TestWeb1.0.1/login" method="post">
        账号:<input type="text" name="account" value="111"><br>
        密码:<input type="password" name="pwd"value="222"/><br>
        性别:<input type="radio" name="sex" value="man">男  <input type="radio" name="sex" value="woman">女<br>
        学历: <select name="xueli">
                <option>da</option>
                <option>xiao</option>
                <option>zhong</option>
            </select>
            <br>
        技能: <input type="checkbox" name="skill" value="java">JAVA
            <input type="checkbox" name="skill" value="html">HTML
            <input type="checkbox" name="skill" value="mysql">MYSQL
        <input type="submit" value="登陆">
    </form>
//获取参数
        String account = request.getParameter("account");
        System.out.println("account = " + account);
        //做一个登陆表单   账号  密码          判断账号是admin密码是123456   打印登陆成功   否则登陆失败
        String pwd = request.getParameter("pwd");
        System.out.println("pwd = " + pwd);

        //单选框
        String sex = request.getParameter("sex");
        System.out.println("sex = " + sex);

        //下啦框
        String xueli = request.getParameter("xueli");
        System.out.println("xueli = " + xueli);

        //多选框
        String skills[] = request.getParameterValues("skill");
        for(int i=0; i<skills.length; i++) {
            System.out.println(skills[i]);
        }

2、servlet利用response回传数据
要进行数据传输,就必须使用到流, 所以我们往客户端回传数据的时候是利用response的输出流
PrintWriter out = response.getWriter();
代码如下

        //处理中文乱码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        //获取参数
        String account = request.getParameter("account");
        String pwd = request.getParameter("pwd");

        //利用response回传数据
        PrintWriter out = response.getWriter();
        if(account.equals("admin") && pwd.equals("123456")) {
            //红色字体的登陆成功
            out.print("<html>");
            out.print("     <head>");
            out.print("     </head>");
            out.print("     <body>");
            out.print("         <div style='color:red'> 登陆成功 </div>");
            out.print("     </body>");
            out.print("</html>");
        }else {
            //蓝色字体的登陆失败
            out.print("<html>");
            out.print("     <head>");
            out.print("     </head>");
            out.print("     <body>");
            out.print("         <div style='color:green'> 登陆失败 </div>");
            out.print("     </body>");
            out.print("</html>");
        }


        out.flush();
        out.close();

超链接传递参数及获取参数

<a href="http://localhost:8088/TestWeb2.0.0/DeleteServlet?no=dd201806270015&name=zhangsan">删除订单</a>

服务器获取还是不变:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //处理乱码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        //获取参数
        String no = request.getParameter("no");
        System.out.println("no = " + no);

        String name = request.getParameter("name");
        System.out.println("name = " + name);

    }

注意:表单提交数据是用post,所以我们获取参数在servlet的doPost方法中,
超链接提交数据是用get方式,我们获取参数就在servlet的doGet方法中

总结:我们servlet的开发步骤,
1、新建一个servlet,里面又doget和dopost方法,
2、新建好了servlet之后,在我们的web.xml里面有该servlet的映射
3、部署到服务器,启动服务器
4、启动浏览器根据url地址进行访问

servlet的运行原理:

1、当我们吧servlet发布到服务器以后,tomcat是不会立即创建
2、当我们第一次访问该servlet,tomcat会根据项目的web.xml找到servlet的类
3、判断当前是否存在该类的对象。如果没有就new一个出来,然后调用初始化方init法,初始化servlet的资源【如果有该就直接根据进入第4步】
4、根据请求方式不同get/post去选择调用doGet或者doPost,完成数据操作,该对象进入空闲状态,如果说空闲很久GC是会回收该对象的
5、当垃圾回收器回收的时候,要先调用destory方法回收资源,然后在销毁内存
6、当我们关闭服务器的时候,也会调用destory方法回收资源,然后销毁servlet对象

这里写图片描述

由上我们可以看出,servlet是单例模式, 也是单线程,那就线程不安全,我们需要进行线程同步,所以未来铭感操作的时候就需要进行加锁或者队列【线程同步】

对于我们servlet来说,核心功能是接收参数,往客户端发送数据。,但是后面由于页面复杂程度提升,所以输出页面功能就不被广泛使用,而是跳转页面的方式来完成功能

if(“admin”.equals(account) && “123456”.equals(pwd)) {
//重新跳转到其他页面 == 重定向
// response.sendRedirect(“index.html”);
//获取其他页面的数据来回传给客户端 == 转发
request.getRequestDispatcher(“index.html”).forward(request, response);
}else {
//重新跳转到其他页面 == 重定向
response.sendRedirect(“login.html”);
}

所谓的重定向,就是告诉客户端重新去访问另外一个资源
所谓的转发,就是当前servlet去读取一个资源返回给客户端

request 对象详解,通过我们用抓包软件发现,http协议吧请求数据包分成了两块,一款是请求头,包含的是客户端的基本信息和提交给服务器的数据
即header和postDate

我们可以通过request.getHeader(“key”)来获取请求头信息,
同时我们可以可以利用response.setHeader(key, value) 回传给客户端信息,比如设置缓存,比如下载图片。
而接收和响应数据利用request.getParameter(“key”),
利用response的Writer流回传数据给客户端, 这种方式一般不用,除了APP接口
设置http的状态码: 404 500 200 300 405 等等
200表示服务器运行成功,响应成功,程序正常
404 资源找不到
500: 服务器的java代码跑出了异常
300: 重定向到某一个页面
405:参数类型不匹配

转发页面:
request.getRequestDispatcher(“login.html”).forward(request, response);
重定向页面:
response.setHeader(“Refresh”, “3;url=index.html”);

    //登陆失败利用300状态码重定向到一个页面
    response.setStatus(300);
    response.setHeader("Location", "index.html");

    response.sendRedirect("index.html");

会话技术: session、cookie

猜你喜欢

转载自blog.csdn.net/sky274548769/article/details/80832878