cherry.png

Writing the site on pure servlets

10.08.2018
checkmark3

Java, Servlet, HTML

Known technologies of Java - JSP, JSF are based on basic servers, and finally when load on application server they are compiled into a child class ofHttpServlet,and then compiled into bytecode. This can be seen if you look at the application server, but this code, to put it mildly, is difficult to read - it is firstly not fully obvious, secondly every line of outgoing*.htmlcode forms by separate call of the methodwrite(...)from page context, and thirdly the code becomes 10 times larger.
Let's write a simple application on pure servlets with annotations. For example, let's take one servlet that will build an outgoing*.htmlcode based on the template and substitute the necessary parameters there. Writing Hello World in pure servlets.
1. Project structure:
site-on-simple-servlets
│
├── src
│   └── main
│       ├── java
│       │   └── servlets
│       │       ├── . . .
│       │       ├── . . .
│       │       ├── AbstractServlet.java
│       │       └── Index.java
│       └── resources
│           └── templates
│               ├── . . .
│               ├── . . .
│               └── Index.html
│
└── pom.xml
2. Necessary Maven dependencies:pom.xml
<?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>site-on-simple-servlets</groupId>
    <artifactId>site-on-simple-servlets</artifactId>
    <name>site-on-simple-servlets</name>
    <version>1.0</version>
    <packaging>war</packaging>

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.plugin.vercion>3.8.0</maven.compiler.plugin.vercion>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <build>
        <finalName>site-on-simple-servlets</finalName>
        <defaultGoal>package</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.vercion}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>
3. And here it is - that servlet:Index.java
package servlets;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;

@WebServlet(name = "index", urlPatterns = {"/index.html"})
public class Index extends AbstractServlet {

    protected static String page;

    public static long lastModified = System.currentTimeMillis();

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

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

    @Override
    protected void preparePage(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
        InputStream is = Index.class.getResourceAsStream("/templates/pages/index.html");
        InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);

        StringWriter sw = new StringWriter();

        int read;
        while ((read = isr.read()) != -1) {
            sw.write(read);
        }

        setPage(sw.toString().replace("<--say_hello-->", "Hello World!!"));
    }

    @Override
    protected long getLastModified(HttpServletRequest request) {
        return lastModified;
    }
}
Or it more correctly to say two servlets - we'll use inheritance.
4.AbstractServlet.java
package servlets;

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 abstract class AbstractServlet extends HttpServlet {

    protected String getPage() throws ServletException, IOException {
        return (String) this.getClass().getField("page").get(this.getClass());
    }

    protected void setPage(char[] page) {
        this.getClass().getField("page").set(this.getClass(), page);
    }

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

        PrintWriter pw = response.getWriter();

        if (this.getPage() == null) {
            this.preparePage(request, response);
        }

        pw.write(this.getPage());
    }

    protected abstract void preparePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
}
5. At the end, adding the template:index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><--say_hello--></title>
</head>
<body>
    <h1><--say_hello--></h1>
</body>
</html>
Sitedrakonoved.orgis built on this principle, but a little more complicated. There are not only pages with static content, but also dynamic pages. This is achieved by bindingJava - JavaScript.No frameworks are used - everything is written on pure servlets. After the formation of the page minifire and obfuscator starts to work, which also written inJava.

facebookvkontaktetwitterodnoklassnikimailrulivejournal

Comments

O0O0O0O0
Commentator
01.01.1970 03:00 (MSK)
There are no comments yet.. You can be the first..