【Java从零到架构师第二季】【02】Tomcat部署+Servlet入门


持续学习&持续更新中…

学习态度:守破离


客户端与服务器的交互过程

在这里插入图片描述

服务器软件(Web容器)——Apache Tomcat

在这里插入图片描述

Tomcat的启动和关闭

在这里插入图片描述

解决Tomcat控制台输出乱码

在这里插入图片描述

在这里插入图片描述

运行Servlet

注意:登录操作一般不使用get请求,因为get会将表单数据放在浏览器的地址栏中(请求路径)

post的表单数据:在浏览器中 检查->Network->下面,查看请求的Headers,就可以在Form Data中看到。(请求体中)

步骤1:新建一个LoginServlet并添加@WebServlet("/login")注解

@WebServlet("/login")
public class LoginServlet 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");
    }
}

步骤2:新建login.html

注意:此处的action是/javaeestart/login,因为form的action被识别时只会自动在前面拼接http://host:port/

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>login</title>
</head>
<body>
<form action="/javaeestart/login" method="get">

    <div>用户名:<input type="text" name="username"></div>
    <div>密码:<input type="password" name="userpwd"></div>
    <div>
        <button type="submit">登录</button>
    </div>

</form>
</body>
</html>

解决给浏览器响应导致中文乱码问题

GET请求:


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        System.out.println("login doGet");
		
		// doGet读进来时不会乱码
		// 如果乱码的话就要和doPost对req做同样的修改
        System.out.println(req.getParameter("userpwd"));
        System.out.println(req.getParameter("username"));

		// 设置编码一定要写在resp.getWriter()之前
		// text/plain 代表写出去的内容是普通文本 是一种 MIME TYPE
		// charset表示写出去的数据是utf-8格式的
        resp.setContentType("text/plain;charset=utf-8;"); // doGet写出去要修改编码
        PrintWriter writer = resp.getWriter();

        writer.write(
                "username : " + req.getParameter("username") +
                        " userpsd : " + req.getParameter("userpwd")
        );

    }

POST请求:

 @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        System.out.println("login doPost");
		
		// doGet读进来时会乱码
		// 在req读取数据之前要设置编码
        req.setCharacterEncoding("utf-8");

        System.out.println(req.getParameter("userpwd"));
        System.out.println(req.getParameter("username"));

		// doPost写出去时会乱码,因此要和doGet对resp做同样的修改
    }

综上所述:建议使用doGet和doPost时,使用如下写法:


@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    
    

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

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

	// 将get和post请求所需处理的方法单独拎出来
    private void outHTML(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        String username = req.getParameter("username");
        String userpwd = req.getParameter("userpwd");

        System.out.println("username : " + username + " , userpwd : " + userpwd);

        PrintWriter writer = resp.getWriter();
        if ("123".equals(username) && "123".equals(userpwd)) {
    
    

            writer.write("<h1 style=\"color: blue; border: 2px solid black\">这是登录成功之后的user页面。</h1>\n" +
                    "<ul>\n" +
                    "    <li>个人信息</li>\n" +
                    "    <li>修改密码</li>\n" +
                    "    <li>退出登录</li>\n" +
                    "</ul>");
        } else {
    
    
            writer.write("<h1 style=\"color: red; border: 2px solid black\">登录失败</h1> ");
        }

    }


服务器 JVM Tomcat 项目 之间的关系

服务器就是一台装有Tomcat的电脑。

Tomcat(服务器软件—Web容器)本质上是一个有main方法的Java程序,所以要想运行Tomcat必须要有Java虚拟机(JVM)。

部署项目就是告诉Tomcat自己的项目在电脑上的哪一块,让Tomcat可以找到自己的项目,以便Tomcat通过反射等技术来加载我们的项目。

在这里插入图片描述

下面来验证一下main方法处于Tomcat中,以后有时间李明杰老师会讲Tomcat源码(或者自己有时间有精力有能力可以研究研究):

1 关联Tomcat源代码

在这里插入图片描述

将下载好的apache-tomcat-9.0.34-src.zip文件关联到idea即可。(下载Tomcat时在下面记得下载Tomcat的源代码)

关联方法:设置对应module的dependencies,然后点击+,选择下载好的zip文件即可。

2 找出Tomcat的main方法

关联好Tomcat的源代码以后,就可以找到Tomcat的main方法了,步骤如下:

首先,在创建完项目并部署到Tomcat以后,在浏览器中访问该项目时,默认访问的是index.html或者index.jsp文件,那么我们就可以搜索一下,到底Tomcat在哪儿使用到了这两个文件:

在idea中按住ctrl + shift + f调出Find in Files窗口(如果搜不出来的话,在idea的Settings中,找到快捷键设置Keymap,在其中搜索find in files进行修改快捷键即可),点击scope选项,并键入index.jsp进行搜索。

在这里插入图片描述

往上滑动会发现有个Tomcat.java,点进去看看

在这里插入图片描述

在这里插入图片描述

可以发现,Tomcat会优先执行加载(欢迎文件)index.html、其次是index.htm、最后是index.jsp页面。

既然能够搜索到index.jsp在Tomcat源码中的位置,那么,我们同样可以使用该方法直接来搜索main方法:

在这里插入图片描述

最终在Bootstrap.java中可以找到真正的main方法。(boot:计算机启动相关命令、程序等;strap:带子)

在这里插入图片描述

参考

李明杰: Java从0到架构师②JavaEE技术基石.


本文完,感谢您的关注支持!


おすすめ

転載: blog.csdn.net/weixin_44018671/article/details/120659928