Servlet 直接返回web前端(代码库)

先在 web.xml 中 添加Servlet

<servlet>
    <servlet-name>bingTestServlet</servlet-name>
    <servlet-class>com.bing.servlet.BingTestServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>bingTestServlet</servlet-name>
    <url-pattern>/abcde</url-pattern>
</servlet-mapping>

 

然后是servlet 类 BingTestServlet.java

package com.bing.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class BingTestServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setStatus(200);
        response.setContentType("text/html");
        PrintWriter writer = response.getWriter();
        String htmlContent = "<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>你好</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "<label style=\"color: red\">Hello Word !</label>\n" +
                "</body>\n" +
                "</html>";
        writer.write(htmlContent);
        writer.flush();
    }

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

}

 

但是一般情况下,不会直接在java类里写html的代码,所以往往会在html文件里好,然后在servlet中读取里边的内容。如下:

1、首先在WEB-INF 的同级目录下,新建一个 bing.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>你好</title>
</head>
<body>
<label style="color: red">Hello Word !</label>
</body>
</html>

 

2、然后 将BingTestServlet.java 修改成下面代码

package com.bing.servlet;

import org.apache.commons.io.FileUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

public class BingTestServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    private String htmlContent = null;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if (htmlContent == null) {
            String path = Thread.currentThread().getContextClassLoader().getResource("").getPath().replace("WEB-INF/classes/", "bing.html");
            try {
                htmlContent = FileUtils.readFileToString(new File(path), "utf-8");
            } catch (Exception e) {
                response.setStatus(404);
                return;
            }
        }

        response.setStatus(200);
        response.setContentType("text/html");
        PrintWriter writer = response.getWriter();
        writer.write(htmlContent);
        writer.flush();
    }

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

}

 

写在最后:

1、上面的例子中, /abcde 并不是一个接口,在web.xml 中被Servlet拦住了以后,就直接返回了,没有再往程序里面走
2、如果先写一个 /abcde接口,让web.xml 里先走Spring的接口,再走Servlet,将它的response修改一下,会怎么样呢?这样是不是就可以动态修改返回参数了。(当然有很多其他方法可以修改参数,这里只说servlet)

 

PS:发现想的幼稚了,就算有个 /abcde 的接口,断点发现,它仍然是先走Servlet, 不会走到接口里去

 

原创文章,欢迎转载,转载请注明出处!

 

上一篇:servlet开发步骤


下一篇:Servlet