Servlet不适用于IntellJ

时间:2018-05-26 12:41:17

标签: java tomcat servlets java-ee intellij-idea

使用IntelliJ创建一个简单的Servlet时遇到问题。在eclipse上,上面的代码运行正常,它向我显示正确的输出。 但是在IntelliJ(以及正确设置的tomcat)上,它会在输出中显示一个简单的html页面,其中包含" $ Title $"作为标题和" $ END $"身体,我不明白为什么。 有人可以帮我吗?

package ServerTest;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

@WebServlet(name = "Servlet")
public class Servlet extends HttpServlet {
    public Servlet()
    {
        super();
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       doGet(request, response);
    }

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


        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila?serverTimezone=UTC", "root", "mypass");
            PreparedStatement pstmt=conn.prepareStatement("SELECT first_name FROM actor");


            // pstmt.setString(parameter position, value)


            ResultSet result= pstmt.executeQuery();
            response.setContentType("text/html");

            PrintWriter out= response.getWriter();
            out.println("<HTML>");
            out.println("<BODY>");
            out.println("<H1> Risultato: </H1>");
            while (result.next())
            {
                out.println("<P>");

                out.println(result.getString("first_name"));
                out.println("</P>");
            }
            out.println("</BODY>");
            out.println("</HTML>");
            result.close();
            pstmt.close();
            conn.close();
        }
        catch(SQLException e){
            System.out.println("Errore nell'esecuzione della query");
            System.out.println(e.getMessage());
        } catch (ClassNotFoundException e) {
            System.out.println("Impossibile caricare il driver");
            System.out.println(e.getMessage());
        }

    }
}

0 个答案:

没有答案