我用NetBeans 11.1 + JDK 11.0.2+ tomcat 9.0.31创建了一个简单的HelloWorld项目,部署是成功了在tomcat对应的conf/Catalina/localhost/文件夹下有HelloWorld.xml生成,但是访问http://localhost:8080/HelloWorld/ServletHello 报出HTTP Status 500。这是ServletHello.java代码
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author TIM
*/
public class ServletHello extends HttpServlet {
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Set the HTTP content type in response header
response.setContentType("text/html; charset=\"UTF-8\"");
// Obtain a PrintWriter object for creating the body
// of the response
PrintWriter servletOut = response.getWriter();
// Create the body of the response
servletOut.println(
"<!DOCTYPE html \n" +
" PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \n" +
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"> \n" +
"<html xmlns='http://www.w3.org/1999/xhtml'> \n" +
" <head> \n" +
" <title> \n" +
" ServletHello.java \n" +
" </title> \n" +
" </head> \n" +
" <body> \n" +
" <p> \n" +
" Hello World! \n" +
" </p> \n" +
" </body> \n" +
"</html> ");
servletOut.close();
}
}
加上
@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
super.init(config);
}
看看