当我尝试调用新的Servlet时遇到错误404。
我认为我已经在servlet上设置了urlPatterns,并且我正确地链接了JSP文件。在档案下面查看。
Servlet:
package controller;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.DAOFactory;
import dao.DiagnosisDAO;
import model.Category;
import model.Diagnosis;
/**
* Servlet implementation class DiagnosisController
*/
@MultipartConfig
@WebServlet(name = "/DiagnosisController", urlPatterns = {
"/diagnosis",
"/diagnosis/all"
})
public class DiagnosisController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DiagnosisController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
RequestDispatcher dispatcher;
switch(request.getServletPath()) {
case "/diagnosis":
DiagnosisDAO diagDao;
try(DAOFactory daoFactory = new DAOFactory()){
diagDao = daoFactory.getDiagnosisDAO();
Diagnosis diag = diagDao.get_DMFSteam();
request.setAttribute("DSteam", diag.getDMFSteamList());
} catch (ClassNotFoundException e1) {
request.getSession().setAttribute("error", e1.getMessage());
} catch (SQLException e1) {
request.getSession().setAttribute("error", e1.getMessage());
} catch (Exception e1) {
request.getSession().setAttribute("error", e1.getMessage());
}
dispatcher = request.getRequestDispatcher("/view/diagnosis/charts.jsp");
dispatcher.forward(request, response);
break;
}
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
这是JSP:
<%--
Document : navbar
Created on : 03/07/2018, 11:55:04
Author : dskaster
--%>
<%@page contentType="text/html" pageEncoding="UTF-8" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<div class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<span class="navbar-brand"><a href="${pageContext.servletContext.contextPath}/view/menuInicial.jsp">Banco de Dados</a></span>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="${pageContext.servletContext.contextPath}/view/menuInicial.jsp">Home</a>
</li>
<li class="dropdown">
<a class="dropdown-toggle" href="javascript:void(0)" data-toggle="dropdown">
Jogos<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="${pageContext.servletContext.contextPath}/game">Listar jogos</a></li>
<li><a href="${pageContext.servletContext.contextPath}/game/create/first">Cadastrar jogo</a></li>
</ul>
</li>
<li>
<a href="${pageContext.servletContext.contextPath}/developer">Desenvolvedoras</a>
</li>
<li>
<a href="${pageContext.servletContext.contextPath}/publisher">Publicadoras</a>
</li>
<li>
<a href="${pageContext.servletContext.contextPath}/view/category/create.jsp">Nova Categoria</a>
</li>
<li>
<a href="${pageContext.servletContext.contextPath}/view/json/import.jsp">Upload Json</a>
</li>
<li>
<a class="btn btn-lg btn-warning" href="${pageContext.servletContext.contextPath}/diagnosis/all">Diagnósticos</a>
</li>
</ul>
</div>
</div>
</div>
JSP文件的最后一个“ li”是“ href =“ $ {pageContext.servletContext.contextPath} / diagnosis / all ”,它应该调用上面的Servlet,但是不起作用。我已经尝试创建一个新的Controller,但是仍然无法正常工作。
在这个问题上我真的需要帮助,这个错误已经花了很多时间了,我放弃了这个项目。
我正在将Eclipse IDE Photon与apache tomcat 8.0和9.0一起使用。
答案 0 :(得分:0)
我发现了错误,那是在Servlet上,我删除了标签“ @MultipartConfig”,并且一切正常。 Servlet就是这样:
package controller;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.DAOFactory;
import dao.DiagnosisDAO;
import model.Category;
import model.Diagnosis;
/**
* Servlet implementation class DiagnosisController
*/
@WebServlet(name = "/DiagnosisController", urlPatterns = {
"/diagnosis",
"/diagnosis/all"
})
public class DiagnosisController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DiagnosisController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
RequestDispatcher dispatcher;
switch(request.getServletPath()) {
case "/diagnosis":
DiagnosisDAO diagDao;
try(DAOFactory daoFactory = new DAOFactory()){
diagDao = daoFactory.getDiagnosisDAO();
Diagnosis diag = diagDao.get_DMFSteam();
request.setAttribute("DSteam", diag.getDMFSteamList());
} catch (ClassNotFoundException e1) {
request.getSession().setAttribute("error", e1.getMessage());
} catch (SQLException e1) {
request.getSession().setAttribute("error", e1.getMessage());
} catch (Exception e1) {
request.getSession().setAttribute("error", e1.getMessage());
}
dispatcher = request.getRequestDispatcher("/view/diagnosis/charts.jsp");
dispatcher.forward(request, response);
break;
}
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
谢谢你们。