将Java代码连接到JSP

将Java代码连接到JSP,java,jsp,servlets,web.xml,Java,Jsp,Servlets,Web.xml,我有一个.jsp文件和一个.java文件。 我尝试使用注释,但这两个文件似乎没有连接 这是Java代码 @WebServlet("/WEB-INF/JSP/restaurant.htm") public class restaurant extends HttpServlet{ private static final long serialVersionUID = 1L; private static final String VIEW = "/WEB-INF/JSP/restaurant.j

我有一个.jsp文件和一个.java文件。 我尝试使用注释,但这两个文件似乎没有连接

这是Java代码

@WebServlet("/WEB-INF/JSP/restaurant.htm")
public class restaurant extends HttpServlet{
private static final long serialVersionUID = 1L;
private static final String VIEW = "/WEB-INF/JSP/restaurant.jsp";
private RestaurantService restaurantService = new RestaurantService();


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    List<Restaurant> restaurants = restaurantService.findAll();
    request.setAttribute("restaurants", restaurants);

    request.getRequestDispatcher(VIEW).forward(request, response);
}
@WebServlet(“/WEB-INF/JSP/restaurant.htm”)
公共级餐厅扩展HttpServlet{
私有静态最终长serialVersionUID=1L;
私有静态最终字符串视图=“/WEB-INF/JSP/restaurant.JSP”;
private RestaurantService RestaurantService=新RestaurantService();
受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
List restaurants=restaurantService.findAll();
request.setAttribute(“餐厅”,餐厅);
getRequestDispatcher(VIEW).forward(请求,响应);
}
这是我的JSP代码

<%@page contentType="text/html" pageEncoding="UTF-8" session="false"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<c:set var="contextPath" value="${pageContext.servletContext.contextPath}" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Selecteer uw restaurant</title>
</head>
<body>
<h1>Selecteer uw restaurant</h1>

<c:forEach var="restaurant" items="${restaurants}">
    <c:url value="/gerechten" var="restaurantURL">
        <c:param name="rest">${restuarant.restaurantId}</c:param>
    </c:url>
    <a href="restaurantURL"> <img alt="${restaurant.naam}"
        src="${restaurant.image}"> <br /> ${restaurant.naam}
    </a><br/><br/>
</c:forEach>
</body>
</html>

Selecteer uw餐厅
Selecteer uw餐厅
${resturant.restaurantId}


虽然只有几行,但即使这样也不行。。 我是否使用web.xml?如果是,我如何使用它


提前感谢!

使用Servlet 3.0,您不再需要web描述符(某些配置除外)。使用注释基本上可以替换
元素

@WebServlet
注释的默认属性是保存

servlet的URL模式

现在,您的url模式是
/WEB-INF/JSP/restaurant.htm
,因此您可以在(假设本地主机,端口8080)访问您的应用程序


我假设这不是您想要的。因此,修改
@WebServlet
属性以使用您要用于访问servlet的url。

您给
@WebServlet
的字符串值是servlet将处理的url模式。(1)您可以将
url模式
缩短为
“/restaurant.htm”
(2)确保一致地使用
html
vs
htm
http://localhost:8080/context/WEB-INF/JSP/restaurant.htm
                             ^ start of your url pattern