Jsp 没有连接到 servlet
Jsp doesn't connect to the servlet
我在尝试调用新的 Servlet 时收到错误 404 not found。
我已经在 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。
我发现了错误,那是在 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);
}
}
谢谢大家。
我在尝试调用新的 Servlet 时收到错误 404 not found。
我已经在 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。
我发现了错误,那是在 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);
}
}
谢谢大家。