简体   繁体   中英

How do I run Spring Boot Servlet pages

Can Spring boot Maven applications runs servlet pages in this rough fashion below. So I can have loads of different pages that are build like below using

        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("</head>");
        out.println("<body>");
        out.println("</body>");
        out.println("</html>");

Is this possible

If you click the link above you can view the other files

Thanks people for any help

package com.example;

import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.System.out;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = {"/servlet"})
public class servlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) 
        {
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Title</title>");
            out.println("<meta charset=\"UTF-8\"><style>.inputlogout {background:transparent;border: 0px;color: white;padding: 0px;margin: 0px;font-size: 10px;transition: all ease .5s;-webkit-font-smoothing: antialiased;}.inputlogout:hover{color:#CD6600;cursor:pointer;}#loginform{position:absolute;top:6px;right:240px;}</style>");
            out.println("</head>");
            out.println("<body>");
            out.print("<div>");
                try 
                {
                        Connection connection = getConnection();
                        Statement stmt = connection.createStatement();
                        String sql;
                        sql = "SELECT id, header, desc, time FROM preview";
                        ResultSet rs = stmt.executeQuery(sql);
                        System.out.print(sql);
                        while(rs.next())
                        {
                                String id = rs.getString("id");
                                String header = rs.getString("header");
                                String desc = rs.getString("desc");
                                String time = rs.getString("time");
                                out.print("<div>");
                                out.print(id);
                                out.print("</div>");
                                out.print("<div>");
                                out.print(header);
                                out.print("</div>");
                                out.print("<div>");
                                out.print(desc);
                                out.print("</div>");
                                out.print("<div>");
                                out.print(time);
                                out.print("</div>");
                        }
                }catch(Exception e){e.printStackTrace();} 
            }

            out.println("</div>");
            out.println("</body>");
            out.println("</html>");

        }

/*Database Connection*/
    private static java.sql.Connection getConnection() throws URISyntaxException, SQLException {
        URI dbUri = null;
        if(System.getenv("DATABASE_URL") != null) {
            dbUri = new URI(System.getenv("DATABASE_URL"));
        }else {
            String DATABASE_URL;
            DATABASE_URL = "postgres://username:password@ec2-46-137-73-65.eu-west-1.compute.amazonaws.com:5432/database";
            dbUri = new URI(DATABASE_URL);
        }

        String username = dbUri.getUserInfo().split(":")[0];
        String password = dbUri.getUserInfo().split(":")[1];
        String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':'
                + dbUri.getPort() + dbUri.getPath()
                + "?sslmode=require";
                /*Connection connection = DriverManager.getConnection(
                "jdbc:postgresql://localhost:5432/userdb?sslmode=require",
                "ubuntu",
                "ubuntu");*/
        return DriverManager.getConnection(dbUrl, username, password);
    }

}

1.You need to migrate your servlet based application into Spring MVC ( Spring Boot uses Spring MVC )

Refer this migration guide

0.Prior to that , Read this excellent Step-by-Step tutorial (Spring MVC) for beginners.

If you want to base development off this sample, I was able to run like this:

mvn package
java -jar target/heroku-spring-boot-psql-0.0.1-SNAPSHOT.jar

Then open http://localhost:8080 in a browser

Another option is mvn spring-boot:run

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM