我正在开发基于 JSP 的Web应用程序,并尝试使用Tomcat 8连接到 Servlet 。显然我的所有代码都是正确的,我是&#39 ;我已经在谷歌搜索了但是没有任何好的结果,我已经添加到我的代码中了。
拜托,我需要帮助,因为我无法看出错误在哪里。谢谢!
我的 Index.jsp 页面:
<%@ page import="java.util.ArrayList"%>
<%@ page import="app.negocio.GestionLibreria"%>
<%@ page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles/miestilo.css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script type="text/javascript">
</script>
</head>
<body>
<div id="header">
<div id="header-inner">
<div id="logo">
<a href="/" class>
<img src="http://image.casadellibro.com/t1/i/logo-img.png" id="logo-img"
</a>
<a href="/" class>
<img src="http://image.casadellibro.com/t1/i/logo-txt.png" id="logo-txt"
</a>
</div>
<div id="nav-a">
<div id="datos-registro">
</div>
</div>
<div id="nav-b">
<div id="nav-b-inner">
<ul id="nav03" class="delayed-hover">
<li class="dropdown" id="item01"><a href="controlador?" title="Libros">Libros</li>
<li class="dropdown" id="item02"><a href="/editoriales" title="Ebooks">Editoriales</li>
</ul>
</div>
</div>
<div id="header sub">
</div>
</div>
</div>
<div id="body">
<jsp:forward page="/Servlet?">
<jsp:param name="option" value="1"/>
</jsp:forward>
<% List<String> lista = new ArrayList<>();
lista = (List<String>) request.getAttribute("list2");%>
<div id="div-table">
<table id="gridLibros-table">
<tr>
<td><%= lista.size()%></td>
<td>Jackson</td>
<td>94</td>
<td>94</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
<td>94</td>
</tr>
</table>
</div>
</div>
</body>
</html>
Servlet.java
package app.web;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet extends HttpServlet {
public Servlet() {
super();
// TODO Auto-generated constructor stub
}
private void procesar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String op = request.getParameter("option");
if ("1".equals(op)) {
List<String> lista = new ArrayList<>();
lista.add("number1");
lista.add("number2");
request.setAttribute("todos", list);
RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");
rd.forward(request, response);
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
procesar(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
procesar(request, response);
}
}
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<display-name>Web_project3</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>
<servlet>
<servlet-name>Servlet</servlet-name>
<servlet-class>app.web.Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/Servlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
错误回复页面:
Estado HTTP 500 - Ha sucedido una excepción al procesar la página JSP /index.jsp en línea 49
type Informe de Excepción
mensaje Ha sucedido una excepción al procesar la página JSP /index.jsp en línea 49
descripción El servidor encontró un error interno que hizo que no pudiera rellenar este requerimiento.
excepción
org.apache.jasper.JasperException: Ha sucedido una excepción al procesar la página JSP /index.jsp en línea 49
46: </div>
47: </div>
48: <div id="body">
49: <jsp:forward page="/ServletLibreria?">
50: <jsp:param name="option" value="1"/>
51: </jsp:forward>
52: <% List<String> lista = new ArrayList<>();
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:567)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:454)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
causa raíz
org.apache.jasper.JasperException: Ha sucedido una excepción al procesar la página JSP /index.jsp en línea 49
46: </div>
47: </div>
48: <div id="body">
49: <jsp:forward page="/Servlet?">
50: <jsp:param name="option" value="1"/>
51: </jsp:forward>
52: <% List<String> lista = new ArrayList<>();
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:567)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:454)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
app.web.ServletLibreria.procesar(ServletLibreria.java:49)
app.web.ServletLibreria.doGet(ServletLibreria.java:60)
javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:741)
org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:711)
org.apache.jsp.index_jsp._jspService(index_jsp.java:144)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:431)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
答案 0 :(得分:0)
您的页面index.jsp将使用参数选项equals 1和forwrd to index.jsp来调用servlet。 我不认为你在index.jsp中使用jsp:forward,而是一个简单的链接。 试试这段代码:
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles/miestilo.css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script type="text/javascript">
</script>
</head>
<body>
<div id="header">
<div id="header-inner">
<div id="logo">
<a href="/" class>
<img src="http://image.casadellibro.com/t1/i/logo-img.png" id="logo-img"
</a>
<a href="/" class>
<img src="http://image.casadellibro.com/t1/i/logo-txt.png" id="logo-txt"
</a>
</div>
<div id="nav-a">
<div id="datos-registro">
</div>
</div>
<div id="nav-b">
<div id="nav-b-inner">
<ul id="nav03" class="delayed-hover">
<li class="dropdown" id="item01"><a href="controlador?" title="Libros">Libros</li>
<li class="dropdown" id="item02"><a href="/editoriales" title="Ebooks">Editoriales</li>
</ul>
</div>
</div>
<div id="header sub">
</div>
</div>
</div>
<div id="body">
<a href="Servlet?option=1">link to servlet with Option 1</a>
<%--<jsp:forward page="/Servlet?">
<jsp:param name="option" value="1"/>
</jsp:forward>--%>
<% List<String> lista = (List<String>) request.getAttribute("lista");
if (lista == null) {
lista = new ArrayList<String>();
}
%>
<div id="div-table">
<table id="gridLibros-table">
<tr>
<td><%= lista.size()%>
</td>
<td>Jackson</td>
<td>94</td>
<td>94</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
<td>94</td>
</tr>
</table>
</div>
</div>
</body>
</html>
package app.web;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet extends HttpServlet {
public Servlet() {
super();
// TODO Auto-generated constructor stub
}
private void procesar(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
String op = request.getParameter("option");
if ("1".equals(op)) {
List<String> lista = new ArrayList<String>();
lista.add("number1");
lista.add("number2");
request.setAttribute("lista", lista);
RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");
rd.forward(request, response);
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
procesar(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
procesar(request, response);
}
}
答案 1 :(得分:0)
最后我已经解决了这个问题,并且因为没有明确的信号表明它的主要原因是什么。 我已经创建了一个新项目,并逐步仔细部署相同的代码。我正在使用Netbeans 8.02和Tomcat 8.01。
非常感谢大家!