如何运行Spring Boot Servlet页面

时间:2016-07-21 00:12:30

标签: java maven servlets heroku spring-boot

Spring引导Maven应用程序可以在下面以粗略的方式运行servlet页面。所以我可以使用

来加载大量不同的页面,如下所示
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("</head>");
        out.println("<body>");
        out.println("</body>");
        out.println("</html>");

这可能吗

如果单击上面的链接,则可以查看其他文件

感谢大家的帮助

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);
    }

}

2 个答案:

答案 0 :(得分:0)

1.您需要基于servlet的应用程序迁移到Spring MVC(Spring Boot使用 Spring MVC

请参阅此migration guide

0.首先,请为初学者阅读此excellent Step-by-Step tutorial(Spring MVC)。

答案 1 :(得分:0)

如果你想从这个样本开始开发,我能够像这样运行:

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

然后在浏览器中打开http://localhost:8080

另一个选项是mvn spring-boot:run