几个月前我为我的大学(一个问答游戏)做过这个项目,一切都很好!现在我重新打开了eclipse项目,但我不知道为什么,它不起作用!我已经搜索了旧话题,因为我知道这是一个经常出现的问题,但我不知道如何解决它!我认为这只是一个配置问题,因为我已经在另一个工作站上编写了整个项目,而且一切都很好。现在我正在尝试使用Eclipse Mars和tomcat 8.0在我的Mac上运行servlet。
这里我报告了我的一个servlet和xml文件的代码。
package servlet;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
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 javax.servlet.http.HttpSession;
import database.DbHelper;
/**
* Servlet implementation class Login
*/
@WebServlet("/Login")
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
private DbHelper dbHelper = null;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
ServletContext context = config.getServletContext();
String driver = context.getInitParameter("dbDriver");
String path = context.getInitParameter("dbUrl");
String user = context.getInitParameter("dbUser");
String pass = context.getInitParameter("dbPassword");
try {
dbHelper = new DbHelper(driver, path, user, pass);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
@Override
public void destroy() {
super.destroy();
if(dbHelper!=null){
try {
dbHelper.destroy();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session=request.getSession();
String username=request.getParameter("username");
String password=request.getParameter("password");
try {if (dbHelper.isPlayer(username, password)){
session.setAttribute("username",username);
response.sendRedirect("Player");
} else
if (dbHelper.isAuthor(username, password)){
session.setAttribute("username",username);
response.sendRedirect("Author");
}else{
request.setAttribute("error", 1);
request.getRequestDispatcher("jsp/login.jsp").forward(request, response);
}
} catch (SQLException e) {
e.printStackTrace();
response.sendRedirect("html/error.html");
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true">
<display-name>CheckMyBrain</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<description>Database URL</description>
<param-name>dbUrl</param-name>
<param-value>jdbc:mysql://localhost:3306/Quiz</param-value>
</context-param>
<context-param>
<description>Database user</description>
<param-name>dbUser</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<description>Database password</description>
<param-name>dbPassword</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<description>Database driver</description>
<param-name>dbDriver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
<param-name>smtpHost</param-name>
<param-value>smtp.gmail.com</param-value>
</context-param>
<context-param>
<param-name>smtpPort</param-name>
<param-value>587</param-value>
</context-param>
<context-param>
<param-name>smtpUser</param-name>
<param-value>checkmybrain37@gmail.com</param-value>
</context-param>
<context-param>
<param-name>smtpPass</param-name>
<param-value>stosultomcat</param-value>
</context-param>
</web-app>
Tomcat位置不在工作区元数据上。