用IDEA进行Java后台开发(二)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_40998254/article/details/84348462

这篇博客接上一篇,用IDEA进行Java后台开发(一)

上一篇我们已经将工程创建完成了,下面我们将创建一个Servlet,然后本地启动tomcat后调用接口,返回helloWorld,过程如下:

1.在src目录下创建servlet目录并创建DemoServlet继承HttpServlet

public class DemoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

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

注:在DemoServlet中重写父类的两个方法doGet和doPost,这两个方法用于后面分别处理get请求和post请求。

2.编辑web/WEB-INF/web.xml文件,将DemoServlet进行注册   

 <servlet>
        <servlet-name>DemoServlet</servlet-name>
        <servlet-class>servlet.DemoServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>DemoServlet</servlet-name>
        <url-pattern>/DemoServlet</url-pattern>
    </servlet-mapping>

注:不用这种方式也可以在DemoServlet前使用注解方式注册,例如

@WebServlet(name="DemoServlet",urlPatterns="/DemoServlet")
public class DemoServlet extends HttpServlet{}

3.重写doGet和doPost方法,处理收到请求之后的逻辑,这里我们先直接在doGet方法中返回一个"hello world!"

public class DemoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String result = "hello world!";
        resp.setContentType("text/html;charset=utf-8"); // 设置响应报文的编码格式(否则中文乱码)
        PrintWriter pw = resp.getWriter(); // 获取 response 的输出流
        pw.println(result); // 通过输出流把业务逻辑的结果输出
        pw.flush();
    }

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

4.配置Tomcat
run->Edit SignConfigurations->"+"->tomcat service->local->配置如图


5.通过tomcat运行
可以通过快捷操作栏

可以通过Run->Run Tomcat


6.服务启动后,浏览器输入http://localhost:8080/DemoServlet可以看到我们要的"hello world!"


7.当然我们也可以写一些简单的逻辑比如模拟登录验证

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String account = request.getParameter("account"); // 从 request 中获取名为 account 的参数的值
        String password = request.getParameter("password"); // 从 request 中获取名为 password 的参数的值
        System.out.println("account:" + account + "\npassword:" + password); // 打印出来看一看

        String result = "";
        if("abc".equals(account)
                && "123".equals(password)){ // 模拟登陆账号、密码验证
            result = "Login Success!";
        }else {
            result = "Login False!";
        }
        /* 这里我们只是模拟了一个最简单的业务逻辑,当然,你的实际业务可以相当复杂 */

        response.setContentType("text/html;charset=utf-8"); // 设置响应报文的编码格式
        PrintWriter pw = response.getWriter(); // 获取 response 的输出流
        pw.println(result); // 通过输出流把业务逻辑的结果输出
        pw.flush();

    }

查看下一篇用IDEA进行Java后台开发(三)

猜你喜欢

转载自blog.csdn.net/weixin_40998254/article/details/84348462